From 8759d63787b832af1fc9ac0ec2cc57b6f325ba9a Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 1 Oct 2022 18:29:09 +0300 Subject: [PATCH] chore: typecheck example and test files (#13353) --- .github/workflows/nodejs.yml | 4 +++ examples/angular/app.component.spec.ts | 10 +++--- package.json | 3 ++ .../src/__tests__/getCacheKey.test.ts | 29 ++++++++--------- packages/babel-jest/src/__tests__/index.ts | 31 ++++++++++++------- .../src/__tests__/index.test.ts | 28 ++++++++--------- tsconfig.test.json | 1 + 7 files changed, 62 insertions(+), 44 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 24e7f7e2de..1b660ebb7d 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -50,6 +50,10 @@ jobs: run: yarn test-ts --selectProjects type-tests - name: verify TypeScript@4.3 compatibility run: yarn verify-old-ts + - name: typecheck examples + run: yarn typecheck:examples + - name: typecheck tests + run: yarn typecheck:tests lint: name: Lint diff --git a/examples/angular/app.component.spec.ts b/examples/angular/app.component.spec.ts index 19fc8d9eb0..9be6bc3bd6 100644 --- a/examples/angular/app.component.spec.ts +++ b/examples/angular/app.component.spec.ts @@ -1,5 +1,5 @@ -import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; -import {describe, expect, it, jest} from '@jest/globals'; +import {ComponentFixture, TestBed} from '@angular/core/testing'; +import {beforeEach, describe, expect, it, jest} from '@jest/globals'; import {AppComponent} from './app.component'; import {DataService} from './shared/data.service'; @@ -15,14 +15,14 @@ describe('AppComponent', () => { let fixture: ComponentFixture; let app: AppComponent; - beforeEach(waitForAsync(() => { - TestBed.configureTestingModule({ + beforeEach(async () => { + await TestBed.configureTestingModule({ declarations: [AppComponent], providers: [{provide: DataService, useClass: dataServiceSpy}], }).compileComponents(); fixture = TestBed.createComponent(AppComponent); app = fixture.debugElement.componentInstance; - })); + }); it('should create the app', () => { expect(app).toBeTruthy(); diff --git a/package.json b/package.json index 360c57ab46..425b526675 100644 --- a/package.json +++ b/package.json @@ -110,6 +110,9 @@ "test-ts": "yarn jest --config jest.config.ts.mjs", "test-types": "yarn test-ts --selectProjects type-tests", "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}/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/babel-jest/src/__tests__/getCacheKey.test.ts b/packages/babel-jest/src/__tests__/getCacheKey.test.ts index d6eaa5d929..12b99b59bc 100644 --- a/packages/babel-jest/src/__tests__/getCacheKey.test.ts +++ b/packages/babel-jest/src/__tests__/getCacheKey.test.ts @@ -6,7 +6,7 @@ */ import type {TransformOptions as BabelTransformOptions} from '@babel/core'; -import type {TransformOptions as JestTransformOptions} from '@jest/transform'; +import type {TransformOptions} from '@jest/transform'; import babelJest from '../index'; const {getCacheKey} = babelJest.createTransformer(); @@ -39,9 +39,9 @@ describe('getCacheKey', () => { config: {rootDir: 'mock-root-dir'}, configString: 'mock-config-string', instrument: true, - } as JestTransformOptions; + } as TransformOptions; - const oldCacheKey = getCacheKey(sourceText, sourcePath, transformOptions); + const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions); test('returns cache key hash', () => { expect(oldCacheKey.length).toEqual(32); @@ -54,7 +54,7 @@ describe('getCacheKey', () => { const {createTransformer}: typeof import('../index') = require('../index'); - const newCacheKey = createTransformer().getCacheKey( + const newCacheKey = createTransformer().getCacheKey!( sourceText, sourcePath, transformOptions, @@ -77,7 +77,7 @@ describe('getCacheKey', () => { const {createTransformer}: typeof import('../index') = require('../index'); - const newCacheKey = createTransformer().getCacheKey( + const newCacheKey = createTransformer().getCacheKey!( sourceText, sourcePath, transformOptions, @@ -87,7 +87,7 @@ describe('getCacheKey', () => { }); test('if `sourceText` value is changing', () => { - const newCacheKey = getCacheKey( + const newCacheKey = getCacheKey!( 'new source text', sourcePath, transformOptions, @@ -97,7 +97,7 @@ describe('getCacheKey', () => { }); test('if `sourcePath` value is changing', () => { - const newCacheKey = getCacheKey( + const newCacheKey = getCacheKey!( sourceText, 'new-source-path.js', transformOptions, @@ -107,7 +107,7 @@ describe('getCacheKey', () => { }); test('if `configString` value is changing', () => { - const newCacheKey = getCacheKey(sourceText, sourcePath, { + const newCacheKey = getCacheKey!(sourceText, sourcePath, { ...transformOptions, configString: 'new-config-string', }); @@ -129,7 +129,7 @@ describe('getCacheKey', () => { const {createTransformer}: typeof import('../index') = require('../index'); - const newCacheKey = createTransformer().getCacheKey( + const newCacheKey = createTransformer().getCacheKey!( sourceText, sourcePath, transformOptions, @@ -152,7 +152,7 @@ describe('getCacheKey', () => { const {createTransformer}: typeof import('../index') = require('../index'); - const newCacheKey = createTransformer().getCacheKey( + const newCacheKey = createTransformer().getCacheKey!( sourceText, sourcePath, transformOptions, @@ -162,7 +162,7 @@ describe('getCacheKey', () => { }); test('if `instrument` value is changing', () => { - const newCacheKey = getCacheKey(sourceText, sourcePath, { + const newCacheKey = getCacheKey!(sourceText, sourcePath, { ...transformOptions, instrument: false, }); @@ -173,7 +173,7 @@ describe('getCacheKey', () => { test('if `process.env.NODE_ENV` value is changing', () => { process.env.NODE_ENV = 'NEW_NODE_ENV'; - const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions); + const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions); expect(oldCacheKey).not.toEqual(newCacheKey); }); @@ -181,16 +181,17 @@ describe('getCacheKey', () => { test('if `process.env.BABEL_ENV` value is changing', () => { process.env.BABEL_ENV = 'NEW_BABEL_ENV'; - const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions); + const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions); expect(oldCacheKey).not.toEqual(newCacheKey); }); test('if node version is changing', () => { + // @ts-expect-error: Testing purpose delete process.version; process.version = 'new-node-version'; - const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions); + const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions); expect(oldCacheKey).not.toEqual(newCacheKey); }); diff --git a/packages/babel-jest/src/__tests__/index.ts b/packages/babel-jest/src/__tests__/index.ts index 46dd4677ee..64ccfcad5b 100644 --- a/packages/babel-jest/src/__tests__/index.ts +++ b/packages/babel-jest/src/__tests__/index.ts @@ -5,12 +5,17 @@ * LICENSE file in the root directory of this source tree. */ +import type { + BabelFileResult, + TransformOptions as BabelTransformOptions, +} from '@babel/core'; import {makeProjectConfig} from '@jest/test-utils'; +import type {TransformOptions} from '@jest/transform'; import babelJest, {createTransformer} from '../index'; import {loadPartialConfig} from '../loadBabelConfig'; - jest.mock('../loadBabelConfig', () => { - const actual = jest.requireActual('@babel/core'); + const actual = + jest.requireActual('@babel/core'); return { loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)), @@ -20,7 +25,7 @@ jest.mock('../loadBabelConfig', () => { }; }); -const defaultBabelJestTransformer = babelJest.createTransformer(null); +const defaultBabelJestTransformer = babelJest.createTransformer(); //Mock data for all the tests const sourceString = ` @@ -49,15 +54,18 @@ test('Returns source string with inline maps when no transformOptions is passed' configString: JSON.stringify(makeProjectConfig()), instrument: false, transformerConfig: {}, - }, - ) as any; + } as TransformOptions, + ); + expect(typeof result).toBe('object'); expect(result.code).toBeDefined(); expect(result.map).toBeDefined(); expect(result.code).toMatch('//# sourceMappingURL'); expect(result.code).toMatch('customMultiply'); - expect(result.map!.sources).toEqual(['dummy_path.js']); - expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply'); + expect((result as BabelFileResult).map!.sources).toEqual(['dummy_path.js']); + expect( + JSON.stringify((result as BabelFileResult).map!.sourcesContent), + ).toMatch('customMultiply'); }); test('Returns source string with inline maps when no transformOptions is passed async', async () => { @@ -70,8 +78,9 @@ test('Returns source string with inline maps when no transformOptions is passed configString: JSON.stringify(makeProjectConfig()), instrument: false, transformerConfig: {}, - }, + } as TransformOptions, ); + expect(typeof result).toBe('object'); expect(result.code).toBeDefined(); expect(result.map).toBeDefined(); @@ -125,7 +134,7 @@ describe('caller option correctly merges from defaults and options', () => { instrument: false, transformerConfig: {}, ...input, - }); + } as TransformOptions); expect(loadPartialConfig).toHaveBeenCalledTimes(1); expect(loadPartialConfig).toHaveBeenCalledWith( @@ -142,14 +151,14 @@ describe('caller option correctly merges from defaults and options', () => { }); test('can pass null to createTransformer', () => { - const transformer = createTransformer(null); + const transformer = createTransformer(); transformer.process(sourceString, 'dummy_path.js', { cacheFS: new Map(), config: makeProjectConfig(), configString: JSON.stringify(makeProjectConfig()), instrument: false, transformerConfig: {}, - }); + } as TransformOptions); expect(loadPartialConfig).toHaveBeenCalledTimes(1); expect(loadPartialConfig).toHaveBeenCalledWith( diff --git a/packages/diff-sequences/src/__tests__/index.test.ts b/packages/diff-sequences/src/__tests__/index.test.ts index 7e0bf029ba..7120126bee 100644 --- a/packages/diff-sequences/src/__tests__/index.test.ts +++ b/packages/diff-sequences/src/__tests__/index.test.ts @@ -156,7 +156,7 @@ const assertCommonItems = ( const countDifferences = ( aLength: number, bLength: number, - isCommon, + isCommon: (aIndex: number, bIndex: number) => boolean, ): number => { const dMax = aLength + bLength; const aIndexes = [-1]; // initialize for aLast + 1 in loop when d = 0 @@ -274,7 +274,7 @@ describe('no common items', () => { // default export does not call findSubsequences nor divide describe('negative zero is equivalent to zero for length', () => { - const countItemsNegativeZero = (aLength, bLength) => { + const countItemsNegativeZero = (aLength: number, bLength: number) => { let n = 0; diff( aLength, @@ -301,21 +301,21 @@ describe('no common items', () => { }); test('a empty and b empty', () => { - const a = []; - const b = []; - const expected = []; + const a: Array = []; + const b: Array = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); test('a empty and b non-empty', () => { - const a = []; + const a: Array = []; const b = [false]; - const expected = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); test('a non-empty and b empty', () => { const a = [false, true]; - const b = []; - const expected = []; + const b: Array = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); @@ -327,7 +327,7 @@ describe('no common items', () => { // last segment cannot have a prev segment const a = [false]; const b = [true]; - const expected = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); test('baDeltaLength 1 odd', () => { @@ -336,7 +336,7 @@ describe('no common items', () => { // last segment has a prev segment because unroll a half iteration const a = [0, 1]; const b = ['0']; - const expected = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); test('baDeltaLength 2 even', () => { @@ -345,7 +345,7 @@ describe('no common items', () => { // last segment has a prev segment const a = [0, 1, 2, 3]; const b = ['0', '1']; - const expected = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); test('baDeltaLength 7 odd', () => { @@ -354,7 +354,7 @@ describe('no common items', () => { // last segment has a prev segment const a = ['0', '1', '2']; const b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - const expected = []; + const expected: Array = []; expectCommonItems(a, b, expected); }); }); @@ -734,7 +734,7 @@ const assertCommonSubstring = ( // Return array of substrings in a longest common subsequence of strings. const findCommonSubstrings = (a: string, b: string): Array => { - const array = []; + const array: Array = []; diff( a.length, b.length, diff --git a/tsconfig.test.json b/tsconfig.test.json index 69a0977f45..b575da7b25 100644 --- a/tsconfig.test.json +++ b/tsconfig.test.json @@ -4,6 +4,7 @@ "composite": false, "emitDeclarationOnly": false, "noEmit": true, + "skipLibCheck": true, "types": ["@jest/test-globals"] } }