From e23c5c7bf874cbcc711c9f84d43f085f938d9fc3 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 6 Oct 2022 12:13:39 +0300 Subject: [PATCH] chore: add typechecks for tests of `expect-utils` and `jest-circus` (#13387) --- package.json | 2 +- packages/expect-utils/src/__tests__/isError.test.ts | 11 +++++++---- .../jest-circus/src/__mocks__/testEventHandler.ts | 3 ++- .../src/__tests__/circusItFailingTestError.test.ts | 3 ++- .../src/__tests__/circusItTestError.test.ts | 6 ++---- .../src/__tests__/circusItTodoTestError.test.ts | 6 ++---- packages/jest-circus/src/__tests__/tsconfig.json | 3 ++- 7 files changed, 18 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 6bd46905d8f4..fc0c6275a94a 100644 --- a/package.json +++ b/package.json @@ -113,7 +113,7 @@ "test": "yarn lint && yarn jest", "typecheck": "yarn typecheck:examples && yarn typecheck:tests", "typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit", - "typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect}/src/__tests__", + "typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences,expect,expect-utils,jest-circus}/src/__tests__", "verify-old-ts": "node ./scripts/verifyOldTs.mjs", "verify-pnp": "node ./scripts/verifyPnP.mjs", "watch": "yarn build:js && node ./scripts/watch.mjs", diff --git a/packages/expect-utils/src/__tests__/isError.test.ts b/packages/expect-utils/src/__tests__/isError.test.ts index 90dbf6a9c634..0ab6657689f9 100644 --- a/packages/expect-utils/src/__tests__/isError.test.ts +++ b/packages/expect-utils/src/__tests__/isError.test.ts @@ -16,7 +16,7 @@ import {isError} from '../utils'; // Copied from https://github.com/graingert/angular.js/blob/a43574052e9775cbc1d7dd8a086752c979b0f020/test/AngularSpec.js#L1883 describe('isError', () => { function testErrorFromDifferentContext( - createError: (win: Window) => Error | null, + createError: (win: Window | typeof globalThis) => Error | null, ) { const iframe = document.createElement('iframe'); document.body.appendChild(iframe); @@ -46,16 +46,19 @@ describe('isError', () => { }); it('should detect errors from another context', () => { - testErrorFromDifferentContext(win => new win.Error()); + testErrorFromDifferentContext( + win => new (win as typeof globalThis).Error(), + ); }); it('should detect DOMException errors from another context', () => { testErrorFromDifferentContext(win => { try { win.document.querySelectorAll(''); - } catch (e: any) { - return e; + } catch (e) { + return e as Error; } + return null; }); }); diff --git a/packages/jest-circus/src/__mocks__/testEventHandler.ts b/packages/jest-circus/src/__mocks__/testEventHandler.ts index 4891ed0f5c65..896ed921498f 100644 --- a/packages/jest-circus/src/__mocks__/testEventHandler.ts +++ b/packages/jest-circus/src/__mocks__/testEventHandler.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {Circus} from '@jest/types'; +import type {Circus} from '@jest/types'; const testEventHandler: Circus.EventHandler = (event, state) => { switch (event.name) { @@ -59,4 +59,5 @@ const testEventHandler: Circus.EventHandler = (event, state) => { console.log(`unhandledErrors: ${String(state.unhandledErrors.length)}`); } }; + export default testEventHandler; diff --git a/packages/jest-circus/src/__tests__/circusItFailingTestError.test.ts b/packages/jest-circus/src/__tests__/circusItFailingTestError.test.ts index 0d2117dc1234..df4bc1c65df9 100644 --- a/packages/jest-circus/src/__tests__/circusItFailingTestError.test.ts +++ b/packages/jest-circus/src/__tests__/circusItFailingTestError.test.ts @@ -7,11 +7,12 @@ import type {Global} from '@jest/types'; +// Aliases of `it` and `test` to avoid collision with global testing APIs. let circusIt: Global.It; let circusTest: Global.It; const aliasCircusIt = () => { - const {it, test} = require('../'); + const {it, test} = require('../') as typeof import('../'); circusIt = it; circusTest = test; }; diff --git a/packages/jest-circus/src/__tests__/circusItTestError.test.ts b/packages/jest-circus/src/__tests__/circusItTestError.test.ts index 36b1972c093a..6e21c7415627 100644 --- a/packages/jest-circus/src/__tests__/circusItTestError.test.ts +++ b/packages/jest-circus/src/__tests__/circusItTestError.test.ts @@ -7,14 +7,12 @@ import type {Global} from '@jest/types'; +// Aliases of `it` and `test` to avoid collision with global testing APIs. let circusIt: Global.It; let circusTest: Global.It; -// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate -// the two with this alias. - const aliasCircusIt = () => { - const {it, test} = require('../'); + const {it, test} = require('../') as typeof import('../'); circusIt = it; circusTest = test; }; diff --git a/packages/jest-circus/src/__tests__/circusItTodoTestError.test.ts b/packages/jest-circus/src/__tests__/circusItTodoTestError.test.ts index 8012e9c0198b..bb544b9a2206 100644 --- a/packages/jest-circus/src/__tests__/circusItTodoTestError.test.ts +++ b/packages/jest-circus/src/__tests__/circusItTodoTestError.test.ts @@ -7,13 +7,11 @@ import type {Global} from '@jest/types'; +// Alias of `it` to avoid collision with global testing APIs. let circusIt: Global.It; -// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate -// the two with this alias. - const aliasCircusIt = () => { - const {it} = require('../'); + const {it} = require('../') as typeof import('../'); circusIt = it; }; diff --git a/packages/jest-circus/src/__tests__/tsconfig.json b/packages/jest-circus/src/__tests__/tsconfig.json index 8ccc3083ffb0..5a91fe899445 100644 --- a/packages/jest-circus/src/__tests__/tsconfig.json +++ b/packages/jest-circus/src/__tests__/tsconfig.json @@ -3,5 +3,6 @@ "compilerOptions": { "rootDir": "../" }, - "include": ["../**/*"] + "include": ["../**/*"], + "references": [{"path": "../../"}] }