From 5b8382b9a18012729dbe657317c59099c14e453c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Fri, 17 Apr 2020 13:14:51 +0200 Subject: [PATCH] fix: disallow hook defintions in tests --- CHANGELOG.md | 1 + e2e/__tests__/nestedTestDefinitions.test.ts | 14 ++++++++++++++ .../__tests__/nestedHookInTest.js | 18 ++++++++++++++++++ packages/jest-circus/src/eventHandler.ts | 9 ++++++++- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 e2e/nested-test-definitions/__tests__/nestedHookInTest.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bef176537ff..904d4e425402 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-circus, jest-console, jest-jasmine2, jest-reporters, jest-util, pretty-format]` Fix time durating formatting and consolidate time formatting code ([#9765](https://github.com/facebook/jest/pull/9765)) - `[jest-circus]` [**BREAKING**] Fail tests if a test takes a done callback and have return values ([#9129](https://github.com/facebook/jest/pull/9129)) - `[jest-circus]` [**BREAKING**] Throw a proper error if a test / hook is defined asynchronously ([#8096](https://github.com/facebook/jest/pull/8096)) +- `[jest-circus]` Throw more descriptive error if hook is defined inside test - `[jest-config, jest-resolve]` [**BREAKING**] Remove support for `browser` field ([#9943](https://github.com/facebook/jest/pull/9943)) ### Chore & Maintenance diff --git a/e2e/__tests__/nestedTestDefinitions.test.ts b/e2e/__tests__/nestedTestDefinitions.test.ts index 0a26d4fc14c5..4eaa8f8ae0ac 100644 --- a/e2e/__tests__/nestedTestDefinitions.test.ts +++ b/e2e/__tests__/nestedTestDefinitions.test.ts @@ -55,3 +55,17 @@ test('print correct message when nesting describe inside it', () => { 'Cannot nest a describe inside a test. Describe block "inner describe" cannot run because it is nested within "test".', ); }); + +test('print correct message when nesting a hook inside it', () => { + if (!isJestCircusRun()) { + return; + } + + const result = runJest('nested-test-definitions', ['nestedHookInTest']); + + expect(result.exitCode).toBe(1); + + expect(result.stderr).toContain( + 'Hooks cannot be defined inside tests. Hook of type "beforeEach" is nested within "test".', + ); +}); diff --git a/e2e/nested-test-definitions/__tests__/nestedHookInTest.js b/e2e/nested-test-definitions/__tests__/nestedHookInTest.js new file mode 100644 index 000000000000..d7b403d9842c --- /dev/null +++ b/e2e/nested-test-definitions/__tests__/nestedHookInTest.js @@ -0,0 +1,18 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +'use strict'; + +const {getTruthy} = require('../index'); + +test('test', () => { + expect(getTruthy()).toBeTruthy(); + + beforeEach(() => { + // nothing to see here + }); +}); diff --git a/packages/jest-circus/src/eventHandler.ts b/packages/jest-circus/src/eventHandler.ts index 87a2061a649f..fbe74caea584 100644 --- a/packages/jest-circus/src/eventHandler.ts +++ b/packages/jest-circus/src/eventHandler.ts @@ -88,8 +88,15 @@ const eventHandler: Circus.EventHandler = ( break; } case 'add_hook': { - const {currentDescribeBlock, hasStarted} = state; + const {currentDescribeBlock, currentlyRunningTest, hasStarted} = state; const {asyncError, fn, hookType: type, timeout} = event; + + if (currentlyRunningTest) { + throw new Error( + `Hooks cannot be defined inside tests. Hook of type "${type}" is nested within "${currentlyRunningTest.name}".`, + ); + } + const parent = currentDescribeBlock; if (hasStarted) {