chore: typecheck example and test files (#13353)

This commit is contained in:
Tom Mrazauskas 2022-10-01 18:29:09 +03:00 committed by GitHub
parent eafabf99e6
commit 8759d63787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 44 deletions

View File

@ -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

View File

@ -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<AppComponent>;
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();

View File

@ -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",

View File

@ -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<BabelTransformOptions>;
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);
});

View File

@ -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<typeof import('@babel/core')>('@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<BabelTransformOptions>,
);
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<BabelTransformOptions>,
);
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<BabelTransformOptions>);
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<string, string>(),
config: makeProjectConfig(),
configString: JSON.stringify(makeProjectConfig()),
instrument: false,
transformerConfig: {},
});
} as TransformOptions<BabelTransformOptions>);
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
expect(loadPartialConfig).toHaveBeenCalledWith(

View File

@ -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<unknown> = [];
const b: Array<unknown> = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('a empty and b non-empty', () => {
const a = [];
const a: Array<unknown> = [];
const b = [false];
const expected = [];
const expected: Array<unknown> = [];
expectCommonItems(a, b, expected);
});
test('a non-empty and b empty', () => {
const a = [false, true];
const b = [];
const expected = [];
const b: Array<unknown> = [];
const expected: Array<unknown> = [];
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<unknown> = [];
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<unknown> = [];
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<unknown> = [];
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<unknown> = [];
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<string> => {
const array = [];
const array: Array<string> = [];
diff(
a.length,
b.length,

View File

@ -4,6 +4,7 @@
"composite": false,
"emitDeclarationOnly": false,
"noEmit": true,
"skipLibCheck": true,
"types": ["@jest/test-globals"]
}
}