From 4bc98b58812cd2aebbec7cce2bfac929e8cb5f05 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 11:48:30 +0200 Subject: [PATCH 1/6] feat: add `@jest/globals` package for importing globals explicitly --- CHANGELOG.md | 2 + docs/GlobalAPI.md | 2 +- docs/JestObjectAPI.md | 2 +- e2e/__tests__/importedGlobals.test.ts | 13 +++++ e2e/imported-globals/__tests__/env.test.js | 22 +++++++ e2e/imported-globals/babel.config.js | 8 +++ e2e/imported-globals/package.json | 5 ++ .../legacy-code-todo-rewrite/jestAdapter.ts | 15 +++-- packages/jest-globals/.npmignore | 5 ++ packages/jest-globals/package.json | 30 ++++++++++ packages/jest-globals/src/__tests__/index.ts | 12 ++++ packages/jest-globals/src/index.ts | 27 +++++++++ packages/jest-globals/tsconfig.json | 14 +++++ packages/jest-runtime/package.json | 1 + packages/jest-runtime/src/index.ts | 58 +++++++++++++++++-- packages/jest-runtime/tsconfig.json | 1 + packages/jest-types/src/Circus.ts | 4 +- scripts/buildTs.js | 36 ++++++------ 18 files changed, 224 insertions(+), 33 deletions(-) create mode 100644 e2e/__tests__/importedGlobals.test.ts create mode 100644 e2e/imported-globals/__tests__/env.test.js create mode 100644 e2e/imported-globals/babel.config.js create mode 100644 e2e/imported-globals/package.json create mode 100644 packages/jest-globals/.npmignore create mode 100644 packages/jest-globals/package.json create mode 100644 packages/jest-globals/src/__tests__/index.ts create mode 100644 packages/jest-globals/src/index.ts create mode 100644 packages/jest-globals/tsconfig.json diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2445e8f8b4..4a417f0fdc10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Features +- `[@jest/globals]` New package so Jest's globals can be explicitly imported ([#9801](https://github.com/facebook/jest/pull/9801)) + ### Fixes - `[expect]` Restore support for passing functions to `toHaveLength` matcher ([#9796](https://github.com/facebook/jest/pull/9796)) diff --git a/docs/GlobalAPI.md b/docs/GlobalAPI.md index 2c26605f422d..0df036013794 100644 --- a/docs/GlobalAPI.md +++ b/docs/GlobalAPI.md @@ -3,7 +3,7 @@ id: api title: Globals --- -In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. +In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do `import {describe, expect, it} from '@jest/globals'`. ## Methods diff --git a/docs/JestObjectAPI.md b/docs/JestObjectAPI.md index 638e16f38550..784c5c3370f1 100644 --- a/docs/JestObjectAPI.md +++ b/docs/JestObjectAPI.md @@ -3,7 +3,7 @@ id: jest-object title: The Jest Object --- -The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. +The `jest` object is automatically in scope within every test file. The methods in the `jest` object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via `import {jest} from '@jest/globals'`. ## Mock Modules diff --git a/e2e/__tests__/importedGlobals.test.ts b/e2e/__tests__/importedGlobals.test.ts new file mode 100644 index 000000000000..146ed6544715 --- /dev/null +++ b/e2e/__tests__/importedGlobals.test.ts @@ -0,0 +1,13 @@ +/** + * 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. + */ + +import runJest from '../runJest'; + +test('imported globals', () => { + const result = runJest('imported-globals'); + expect(result.exitCode).toBe(0); +}); diff --git a/e2e/imported-globals/__tests__/env.test.js b/e2e/imported-globals/__tests__/env.test.js new file mode 100644 index 000000000000..e81a1db5a6d5 --- /dev/null +++ b/e2e/imported-globals/__tests__/env.test.js @@ -0,0 +1,22 @@ +/** + * 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. + */ + +import { + expect as importedExpect, + jest as importedJest, + test as importedTest, +} from '@jest/globals'; + +importedTest('they match the globals', () => { + importedExpect(importedExpect).toBe(expect); + importedExpect(importedJest).toBe(jest); + importedExpect(importedTest).toBe(test); + + expect(importedExpect).toBe(expect); + expect(importedJest).toBe(jest); + expect(importedTest).toBe(test); +}); diff --git a/e2e/imported-globals/babel.config.js b/e2e/imported-globals/babel.config.js new file mode 100644 index 000000000000..9be036106918 --- /dev/null +++ b/e2e/imported-globals/babel.config.js @@ -0,0 +1,8 @@ +/** + * 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. + */ + +module.exports = require('../../babel.config'); diff --git a/e2e/imported-globals/package.json b/e2e/imported-globals/package.json new file mode 100644 index 000000000000..148788b25446 --- /dev/null +++ b/e2e/imported-globals/package.json @@ -0,0 +1,5 @@ +{ + "jest": { + "testEnvironment": "node" + } +} diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts index 239060e6dcab..026ba3f54c7c 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts @@ -12,7 +12,8 @@ import type {TestResult} from '@jest/test-result'; import type {RuntimeType as Runtime} from 'jest-runtime'; import type {SnapshotStateType} from 'jest-snapshot'; -const FRAMEWORK_INITIALIZER = require.resolve('./jestAdapterInit'); +const FRAMEWORK_INITIALIZER = path.resolve(__dirname, './jestAdapterInit.js'); +const EXPECT_INITIALIZER = path.resolve(__dirname, './jestExpect.js'); const jestAdapter = async ( globalConfig: Config.GlobalConfig, @@ -24,15 +25,13 @@ const jestAdapter = async ( const { initialize, runAndTransformResultsToJestFormat, - } = runtime.requireInternalModule(FRAMEWORK_INITIALIZER); + } = runtime.requireInternalModule( + FRAMEWORK_INITIALIZER, + ); runtime - .requireInternalModule( - path.resolve(__dirname, './jestExpect.js'), - ) - .default({ - expand: globalConfig.expand, - }); + .requireInternalModule(EXPECT_INITIALIZER) + .default({expand: globalConfig.expand}); const getPrettier = () => config.prettierPath ? require(config.prettierPath) : null; diff --git a/packages/jest-globals/.npmignore b/packages/jest-globals/.npmignore new file mode 100644 index 000000000000..3ee5d55b0b89 --- /dev/null +++ b/packages/jest-globals/.npmignore @@ -0,0 +1,5 @@ +**/__mocks__/** +**/__tests__/** +src +tsconfig.json +tsconfig.tsbuildinfo diff --git a/packages/jest-globals/package.json b/packages/jest-globals/package.json new file mode 100644 index 000000000000..80ac807c888e --- /dev/null +++ b/packages/jest-globals/package.json @@ -0,0 +1,30 @@ +{ + "name": "@jest/globals", + "version": "25.3.0", + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest.git", + "directory": "packages/jest-globals" + }, + "engines": { + "node": ">= 8.3" + }, + "license": "MIT", + "main": "build/index.js", + "types": "build/index.d.ts", + "typesVersions": { + "<3.8": { + "build/*": [ + "build/ts3.4/*" + ] + } + }, + "dependencies": { + "@jest/environment": "^25.3.0", + "@jest/types": "^25.3.0", + "expect": "^25.3.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/jest-globals/src/__tests__/index.ts b/packages/jest-globals/src/__tests__/index.ts new file mode 100644 index 000000000000..cb0af7398581 --- /dev/null +++ b/packages/jest-globals/src/__tests__/index.ts @@ -0,0 +1,12 @@ +/** + * 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. + */ + +test('throw when directly imported', () => { + expect(() => require('../')).toThrowError( + 'Do not import `@jest/globals` outside of the Jest test environment', + ); +}); diff --git a/packages/jest-globals/src/index.ts b/packages/jest-globals/src/index.ts new file mode 100644 index 000000000000..fbdbac0eeec7 --- /dev/null +++ b/packages/jest-globals/src/index.ts @@ -0,0 +1,27 @@ +/** + * 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. + */ + +import importedExpect = require('expect'); +import type {Jest} from '@jest/environment'; +import type {Global} from '@jest/types'; + +export declare type jest = Jest; + +export declare type expect = typeof importedExpect; + +export declare type it = Global.GlobalAdditions['it']; +export declare type test = Global.GlobalAdditions['test']; +export declare type fit = Global.GlobalAdditions['fit']; +export declare type xit = Global.GlobalAdditions['xit']; +export declare type xtest = Global.GlobalAdditions['xtest']; +export declare type describe = Global.GlobalAdditions['describe']; +export declare type xdescribe = Global.GlobalAdditions['xdescribe']; +export declare type fdescribe = Global.GlobalAdditions['fdescribe']; + +throw new Error( + 'Do not import `@jest/globals` outside of the Jest test environment', +); diff --git a/packages/jest-globals/tsconfig.json b/packages/jest-globals/tsconfig.json new file mode 100644 index 000000000000..b6678e5f0aed --- /dev/null +++ b/packages/jest-globals/tsconfig.json @@ -0,0 +1,14 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + // we don't want `@types/jest` to be referenced + "types": ["node"], + "rootDir": "src", + "outDir": "build" + }, + "references": [ + {"path": "../expect"}, + {"path": "../jest-environment"}, + {"path": "../jest-types"} + ] +} diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index bf389a1ef25f..822778a4f389 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -19,6 +19,7 @@ "dependencies": { "@jest/console": "^25.3.0", "@jest/environment": "^25.3.0", + "@jest/globals": "^25.3.0", "@jest/source-map": "^25.2.6", "@jest/test-result": "^25.3.0", "@jest/transform": "^25.3.0", diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 3566ac18cafa..f6bbbfc42a1a 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -17,6 +17,7 @@ import type { Module, ModuleWrapper, } from '@jest/environment'; +import type * as JestGlobals from '@jest/globals'; import type {SourceMapRegistry} from '@jest/source-map'; import {formatStackTrace, separateMessageFromStack} from 'jest-message-util'; import {createDirectory, deepCyclicCopy} from 'jest-util'; @@ -42,6 +43,19 @@ import Resolver = require('jest-resolve'); import Snapshot = require('jest-snapshot'); import stripBOM = require('strip-bom'); +type JestGlobalsValues = { + jest: JestGlobals.jest; + expect: JestGlobals.expect; + it: JestGlobals.it; + test: JestGlobals.test; + fit: JestGlobals.fit; + xit: JestGlobals.xit; + xtest: JestGlobals.xtest; + describe: JestGlobals.describe; + xdescribe: JestGlobals.xdescribe; + fdescribe: JestGlobals.fdescribe; +}; + type HasteMapOptions = { console?: Console; maxWorkers: number; @@ -133,6 +147,7 @@ class Runtime { private _unmockList: RegExp | undefined; private _virtualMocks: BooleanObject; private _moduleImplementation?: typeof nativeModule.Module; + private jestObjectCaches: Map; constructor( config: Config.ProjectConfig, @@ -169,6 +184,7 @@ class Runtime { this._sourceMapRegistry = Object.create(null); this._fileTransforms = new Map(); this._virtualMocks = Object.create(null); + this.jestObjectCaches = new Map(); this._mockMetaDataCache = Object.create(null); this._shouldMockModuleCache = Object.create(null); @@ -323,6 +339,11 @@ class Runtime { modulePath = manualMock; } + if (moduleName === '@jest/globals') { + // @ts-ignore: we don't care that it's not assignable to T + return this.getGlobalsForFile(from); + } + if (moduleName && this._resolver.isCoreModule(moduleName)) { return this._requireCoreModule(moduleName); } @@ -859,6 +880,13 @@ class Runtime { return; } + const jestObject = this._createJestObjectFor( + filename, + localModule.require as LocalModuleRequire, + ); + + this.jestObjectCaches.set(filename, jestObject); + try { compiledFunction.call( localModule.exports, @@ -868,10 +896,7 @@ class Runtime { dirname, // __dirname filename, // __filename this._environment.global, // global object - this._createJestObjectFor( - filename, - localModule.require as LocalModuleRequire, - ), // jest object + jestObject, // jest object ...this._config.extraGlobals.map(globalVariable => { if (this._environment.global[globalVariable]) { return this._environment.global[globalVariable]; @@ -1337,6 +1362,31 @@ class Runtime { throw e; } + + private getGlobalsForFile(from: Config.Path): JestGlobalsValues { + const jest = this.jestObjectCaches.get(from); + + invariant(jest, 'There should always be a Jest object already'); + + return { + describe: this._environment.global.describe, + expect: this._environment.global.expect, + fdescribe: this._environment.global.fdescribe, + fit: this._environment.global.fit, + it: this._environment.global.it, + jest, + test: this._environment.global.test, + xdescribe: this._environment.global.xdescribe, + xit: this._environment.global.xit, + xtest: this._environment.global.xtest, + }; + } +} + +function invariant(condition: unknown, message?: string): asserts condition { + if (!condition) { + throw new Error(message); + } } export = Runtime; diff --git a/packages/jest-runtime/tsconfig.json b/packages/jest-runtime/tsconfig.json index 08abc7a9ddfc..2955d779d6b1 100644 --- a/packages/jest-runtime/tsconfig.json +++ b/packages/jest-runtime/tsconfig.json @@ -9,6 +9,7 @@ {"path": "../jest-console"}, {"path": "../jest-environment"}, {"path": "../jest-environment-node"}, + {"path": "../jest-globals"}, {"path": "../jest-haste-map"}, {"path": "../jest-message-util"}, {"path": "../jest-mock"}, diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index a3db95a90715..6b6adbadab05 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -16,7 +16,9 @@ export type BlockMode = void | 'skip' | 'only' | 'todo'; export type TestMode = BlockMode; export type TestName = Global.TestName; export type TestFn = Global.TestFn; -export type HookFn = (done?: DoneFn) => Promise | null | undefined; +export type HookFn = ( + done?: DoneFn, +) => Promise | null | undefined | void; export type AsyncFn = TestFn | HookFn; export type SharedHookType = 'afterAll' | 'beforeAll'; export type HookType = SharedHookType | 'afterEach' | 'beforeEach'; diff --git a/scripts/buildTs.js b/scripts/buildTs.js index 4d72b27f5aec..cf734188267e 100644 --- a/scripts/buildTs.js +++ b/scripts/buildTs.js @@ -23,6 +23,24 @@ const packagesWithTs = packages.filter(p => fs.existsSync(path.resolve(p, 'tsconfig.json')) ); +packagesWithTs.forEach(pkgDir => { + const pkg = require(pkgDir + '/package.json'); + + if (!pkg.types) { + throw new Error(`Package ${pkg.name} is missing \`types\` field`); + } + + if (!pkg.typesVersions) { + throw new Error(`Package ${pkg.name} is missing \`typesVersions\` field`); + } + + if (pkg.main.replace(/\.js$/, '.d.ts') !== pkg.types) { + throw new Error( + `\`main\` and \`types\` field of ${pkg.name} does not match` + ); + } +}); + const args = [ '--silent', 'tsc', @@ -51,24 +69,6 @@ const downlevelArgs = ['--silent', 'downlevel-dts', 'build', 'build/ts3.4']; console.log(chalk.inverse(' Downleveling TypeScript definition files ')); -packagesWithTs.forEach(pkgDir => { - const pkg = require(pkgDir + '/package.json'); - - if (!pkg.types) { - throw new Error(`Package ${pkg.name} is missing \`types\` field`); - } - - if (!pkg.typesVersions) { - throw new Error(`Package ${pkg.name} is missing \`typesVersions\` field`); - } - - if (pkg.main.replace(/\.js$/, '.d.ts') !== pkg.types) { - throw new Error( - `\`main\` and \`types\` field of ${pkg.name} does not match` - ); - } -}); - // we want to limit the number of processes we spawn const cpus = Math.max(1, os.cpus().length - 1); From 96cef21f6684adc563688d7324beb3ca45ce9203 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 13:58:01 +0200 Subject: [PATCH 2/6] add missing hooks --- packages/jest-globals/src/index.ts | 4 ++++ packages/jest-runtime/src/index.ts | 8 ++++++++ packages/jest-types/src/Global.ts | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/jest-globals/src/index.ts b/packages/jest-globals/src/index.ts index fbdbac0eeec7..82bc15a3b522 100644 --- a/packages/jest-globals/src/index.ts +++ b/packages/jest-globals/src/index.ts @@ -21,6 +21,10 @@ export declare type xtest = Global.GlobalAdditions['xtest']; export declare type describe = Global.GlobalAdditions['describe']; export declare type xdescribe = Global.GlobalAdditions['xdescribe']; export declare type fdescribe = Global.GlobalAdditions['fdescribe']; +export declare type beforeAll = Global.GlobalAdditions['beforeAll']; +export declare type beforeEach = Global.GlobalAdditions['beforeEach']; +export declare type afterEach = Global.GlobalAdditions['afterEach']; +export declare type afterAll = Global.GlobalAdditions['afterAll']; throw new Error( 'Do not import `@jest/globals` outside of the Jest test environment', diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f6bbbfc42a1a..fbdecca72f97 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -54,6 +54,10 @@ type JestGlobalsValues = { describe: JestGlobals.describe; xdescribe: JestGlobals.xdescribe; fdescribe: JestGlobals.fdescribe; + beforeAll: JestGlobals.beforeAll; + beforeEach: JestGlobals.beforeEach; + afterEach: JestGlobals.afterEach; + afterAll: JestGlobals.afterAll; }; type HasteMapOptions = { @@ -1369,6 +1373,10 @@ class Runtime { invariant(jest, 'There should always be a Jest object already'); return { + afterAll: this._environment.global.afterAll, + afterEach: this._environment.global.afterEach, + beforeAll: this._environment.global.beforeAll, + beforeEach: this._environment.global.beforeEach, describe: this._environment.global.describe, expect: this._environment.global.expect, fdescribe: this._environment.global.fdescribe, diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 9142674edc7a..6ad3062a4c3c 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -66,7 +66,6 @@ export interface Describe extends DescribeBase { skip: DescribeBase; } -// TODO: Maybe add `| Window` in the future? export interface GlobalAdditions { it: ItConcurrent; test: ItConcurrent; @@ -76,6 +75,10 @@ export interface GlobalAdditions { describe: Describe; xdescribe: DescribeBase; fdescribe: DescribeBase; + beforeAll: TestFn; + beforeEach: TestFn; + afterEach: TestFn; + afterAll: TestFn; __coverage__: CoverageMapData; jasmine: Jasmine; fail: () => void; @@ -84,6 +87,7 @@ export interface GlobalAdditions { spyOnProperty: () => void; } +// TODO: Maybe add `| Window` in the future? // extends directly after https://github.com/sandersn/downlevel-dts/issues/33 is fixed type NodeGlobalWithoutAdditions = Omit; From 3aef5f6c7590bc4d3c3d7cc0d01ce3442ae36186 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 14:06:41 +0200 Subject: [PATCH 3/6] use dedicated type for framework globals --- packages/jest-runtime/src/index.ts | 18 +++--------------- packages/jest-types/src/Global.ts | 14 +++++++++----- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index fbdecca72f97..d80cfd780b8e 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -9,7 +9,7 @@ import {URL, fileURLToPath} from 'url'; import * as path from 'path'; import {Script, compileFunction} from 'vm'; import * as nativeModule from 'module'; -import type {Config} from '@jest/types'; +import type {Config, Global} from '@jest/types'; import type { Jest, JestEnvironment, @@ -43,22 +43,10 @@ import Resolver = require('jest-resolve'); import Snapshot = require('jest-snapshot'); import stripBOM = require('strip-bom'); -type JestGlobalsValues = { +interface JestGlobalsValues extends Global.TestFrameworkGlobals { jest: JestGlobals.jest; expect: JestGlobals.expect; - it: JestGlobals.it; - test: JestGlobals.test; - fit: JestGlobals.fit; - xit: JestGlobals.xit; - xtest: JestGlobals.xtest; - describe: JestGlobals.describe; - xdescribe: JestGlobals.xdescribe; - fdescribe: JestGlobals.fdescribe; - beforeAll: JestGlobals.beforeAll; - beforeEach: JestGlobals.beforeEach; - afterEach: JestGlobals.afterEach; - afterAll: JestGlobals.afterAll; -}; +} type HasteMapOptions = { console?: Console; diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 6ad3062a4c3c..a7a2959ed1df 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -12,6 +12,7 @@ export type TestName = string; export type TestFn = (done?: DoneFn) => Promise | void | undefined; export type BlockFn = () => void; export type BlockName = string; +export type HookFn = TestFn; export type Col = unknown; export type Row = Array; @@ -66,7 +67,7 @@ export interface Describe extends DescribeBase { skip: DescribeBase; } -export interface GlobalAdditions { +export interface TestFrameworkGlobals { it: ItConcurrent; test: ItConcurrent; fit: ItBase & {concurrent?: ItConcurrentBase}; @@ -75,10 +76,13 @@ export interface GlobalAdditions { describe: Describe; xdescribe: DescribeBase; fdescribe: DescribeBase; - beforeAll: TestFn; - beforeEach: TestFn; - afterEach: TestFn; - afterAll: TestFn; + beforeAll: HookFn; + beforeEach: HookFn; + afterEach: HookFn; + afterAll: HookFn; +} + +export interface GlobalAdditions extends TestFrameworkGlobals { __coverage__: CoverageMapData; jasmine: Jasmine; fail: () => void; From 7b82687e796d8107daa221e23ec4c14daaa7ff1c Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 17:09:25 +0200 Subject: [PATCH 4/6] move HookFn type to Global, and correct signature --- packages/jest-types/src/Circus.ts | 4 +--- packages/jest-types/src/Global.ts | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/jest-types/src/Circus.ts b/packages/jest-types/src/Circus.ts index 6b6adbadab05..1ffc9cf4673c 100644 --- a/packages/jest-types/src/Circus.ts +++ b/packages/jest-types/src/Circus.ts @@ -16,9 +16,7 @@ export type BlockMode = void | 'skip' | 'only' | 'todo'; export type TestMode = BlockMode; export type TestName = Global.TestName; export type TestFn = Global.TestFn; -export type HookFn = ( - done?: DoneFn, -) => Promise | null | undefined | void; +export type HookFn = Global.HookFn; export type AsyncFn = TestFn | HookFn; export type SharedHookType = 'afterAll' | 'beforeAll'; export type HookType = SharedHookType | 'afterEach' | 'beforeEach'; diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index a7a2959ed1df..c227645128ba 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -9,7 +9,7 @@ import type {CoverageMapData} from 'istanbul-lib-coverage'; export type DoneFn = (reason?: string | Error) => void; export type TestName = string; -export type TestFn = (done?: DoneFn) => Promise | void | undefined; +export type TestFn = (done?: DoneFn) => Promise | undefined | void; export type BlockFn = () => void; export type BlockName = string; export type HookFn = TestFn; From bb5e1fe7f361c6f95603409f524c9decf86cd49f Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 17:38:34 +0200 Subject: [PATCH 5/6] no explicit undefined --- packages/jest-types/src/Global.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index c227645128ba..57da5a8389fa 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -9,7 +9,7 @@ import type {CoverageMapData} from 'istanbul-lib-coverage'; export type DoneFn = (reason?: string | Error) => void; export type TestName = string; -export type TestFn = (done?: DoneFn) => Promise | undefined | void; +export type TestFn = (done?: DoneFn) => Promise | void; export type BlockFn = () => void; export type BlockName = string; export type HookFn = TestFn; From 69302ac9d24690e2c423e19a6bc35d8a5df47d58 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 12 Apr 2020 17:45:12 +0200 Subject: [PATCH 6/6] bring back undefined --- packages/jest-types/src/Global.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 57da5a8389fa..f4c0c327ead4 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -9,7 +9,9 @@ import type {CoverageMapData} from 'istanbul-lib-coverage'; export type DoneFn = (reason?: string | Error) => void; export type TestName = string; -export type TestFn = (done?: DoneFn) => Promise | void; +export type TestFn = ( + done?: DoneFn, +) => Promise | void | undefined; export type BlockFn = () => void; export type BlockName = string; export type HookFn = TestFn;