Skip to content

Commit 8759d63

Browse files
authored
chore: typecheck example and test files (#13353)
1 parent eafabf9 commit 8759d63

File tree

7 files changed

+62
-44
lines changed

7 files changed

+62
-44
lines changed

.github/workflows/nodejs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ jobs:
5050
run: yarn test-ts --selectProjects type-tests
5151
- name: verify [email protected] compatibility
5252
run: yarn verify-old-ts
53+
- name: typecheck examples
54+
run: yarn typecheck:examples
55+
- name: typecheck tests
56+
run: yarn typecheck:tests
5357

5458
lint:
5559
name: Lint

examples/angular/app.component.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
2-
import {describe, expect, it, jest} from '@jest/globals';
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {beforeEach, describe, expect, it, jest} from '@jest/globals';
33
import {AppComponent} from './app.component';
44
import {DataService} from './shared/data.service';
55

@@ -15,14 +15,14 @@ describe('AppComponent', () => {
1515
let fixture: ComponentFixture<AppComponent>;
1616
let app: AppComponent;
1717

18-
beforeEach(waitForAsync(() => {
19-
TestBed.configureTestingModule({
18+
beforeEach(async () => {
19+
await TestBed.configureTestingModule({
2020
declarations: [AppComponent],
2121
providers: [{provide: DataService, useClass: dataServiceSpy}],
2222
}).compileComponents();
2323
fixture = TestBed.createComponent(AppComponent);
2424
app = fixture.debugElement.componentInstance;
25-
}));
25+
});
2626

2727
it('should create the app', () => {
2828
expect(app).toBeTruthy();

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
"test-ts": "yarn jest --config jest.config.ts.mjs",
111111
"test-types": "yarn test-ts --selectProjects type-tests",
112112
"test": "yarn lint && yarn jest",
113+
"typecheck": "yarn typecheck:examples && yarn typecheck:tests",
114+
"typecheck:examples": "tsc -p examples/angular --noEmit && tsc -p examples/expect-extend --noEmit && tsc -p examples/typescript --noEmit",
115+
"typecheck:tests": "tsc -b packages/{babel-jest,babel-plugin-jest-hoist,diff-sequences}/src/__tests__",
113116
"verify-old-ts": "node ./scripts/verifyOldTs.mjs",
114117
"verify-pnp": "node ./scripts/verifyPnP.mjs",
115118
"watch": "yarn build:js && node ./scripts/watch.mjs",

packages/babel-jest/src/__tests__/getCacheKey.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import type {TransformOptions as BabelTransformOptions} from '@babel/core';
9-
import type {TransformOptions as JestTransformOptions} from '@jest/transform';
9+
import type {TransformOptions} from '@jest/transform';
1010
import babelJest from '../index';
1111

1212
const {getCacheKey} = babelJest.createTransformer();
@@ -39,9 +39,9 @@ describe('getCacheKey', () => {
3939
config: {rootDir: 'mock-root-dir'},
4040
configString: 'mock-config-string',
4141
instrument: true,
42-
} as JestTransformOptions;
42+
} as TransformOptions<BabelTransformOptions>;
4343

44-
const oldCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
44+
const oldCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
4545

4646
test('returns cache key hash', () => {
4747
expect(oldCacheKey.length).toEqual(32);
@@ -54,7 +54,7 @@ describe('getCacheKey', () => {
5454

5555
const {createTransformer}: typeof import('../index') = require('../index');
5656

57-
const newCacheKey = createTransformer().getCacheKey(
57+
const newCacheKey = createTransformer().getCacheKey!(
5858
sourceText,
5959
sourcePath,
6060
transformOptions,
@@ -77,7 +77,7 @@ describe('getCacheKey', () => {
7777

7878
const {createTransformer}: typeof import('../index') = require('../index');
7979

80-
const newCacheKey = createTransformer().getCacheKey(
80+
const newCacheKey = createTransformer().getCacheKey!(
8181
sourceText,
8282
sourcePath,
8383
transformOptions,
@@ -87,7 +87,7 @@ describe('getCacheKey', () => {
8787
});
8888

8989
test('if `sourceText` value is changing', () => {
90-
const newCacheKey = getCacheKey(
90+
const newCacheKey = getCacheKey!(
9191
'new source text',
9292
sourcePath,
9393
transformOptions,
@@ -97,7 +97,7 @@ describe('getCacheKey', () => {
9797
});
9898

9999
test('if `sourcePath` value is changing', () => {
100-
const newCacheKey = getCacheKey(
100+
const newCacheKey = getCacheKey!(
101101
sourceText,
102102
'new-source-path.js',
103103
transformOptions,
@@ -107,7 +107,7 @@ describe('getCacheKey', () => {
107107
});
108108

109109
test('if `configString` value is changing', () => {
110-
const newCacheKey = getCacheKey(sourceText, sourcePath, {
110+
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
111111
...transformOptions,
112112
configString: 'new-config-string',
113113
});
@@ -129,7 +129,7 @@ describe('getCacheKey', () => {
129129

130130
const {createTransformer}: typeof import('../index') = require('../index');
131131

132-
const newCacheKey = createTransformer().getCacheKey(
132+
const newCacheKey = createTransformer().getCacheKey!(
133133
sourceText,
134134
sourcePath,
135135
transformOptions,
@@ -152,7 +152,7 @@ describe('getCacheKey', () => {
152152

153153
const {createTransformer}: typeof import('../index') = require('../index');
154154

155-
const newCacheKey = createTransformer().getCacheKey(
155+
const newCacheKey = createTransformer().getCacheKey!(
156156
sourceText,
157157
sourcePath,
158158
transformOptions,
@@ -162,7 +162,7 @@ describe('getCacheKey', () => {
162162
});
163163

164164
test('if `instrument` value is changing', () => {
165-
const newCacheKey = getCacheKey(sourceText, sourcePath, {
165+
const newCacheKey = getCacheKey!(sourceText, sourcePath, {
166166
...transformOptions,
167167
instrument: false,
168168
});
@@ -173,24 +173,25 @@ describe('getCacheKey', () => {
173173
test('if `process.env.NODE_ENV` value is changing', () => {
174174
process.env.NODE_ENV = 'NEW_NODE_ENV';
175175

176-
const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
176+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
177177

178178
expect(oldCacheKey).not.toEqual(newCacheKey);
179179
});
180180

181181
test('if `process.env.BABEL_ENV` value is changing', () => {
182182
process.env.BABEL_ENV = 'NEW_BABEL_ENV';
183183

184-
const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
184+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
185185

186186
expect(oldCacheKey).not.toEqual(newCacheKey);
187187
});
188188

189189
test('if node version is changing', () => {
190+
// @ts-expect-error: Testing purpose
190191
delete process.version;
191192
process.version = 'new-node-version';
192193

193-
const newCacheKey = getCacheKey(sourceText, sourcePath, transformOptions);
194+
const newCacheKey = getCacheKey!(sourceText, sourcePath, transformOptions);
194195

195196
expect(oldCacheKey).not.toEqual(newCacheKey);
196197
});

packages/babel-jest/src/__tests__/index.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
import type {
9+
BabelFileResult,
10+
TransformOptions as BabelTransformOptions,
11+
} from '@babel/core';
812
import {makeProjectConfig} from '@jest/test-utils';
13+
import type {TransformOptions} from '@jest/transform';
914
import babelJest, {createTransformer} from '../index';
1015
import {loadPartialConfig} from '../loadBabelConfig';
11-
1216
jest.mock('../loadBabelConfig', () => {
13-
const actual = jest.requireActual('@babel/core');
17+
const actual =
18+
jest.requireActual<typeof import('@babel/core')>('@babel/core');
1419

1520
return {
1621
loadPartialConfig: jest.fn((...args) => actual.loadPartialConfig(...args)),
@@ -20,7 +25,7 @@ jest.mock('../loadBabelConfig', () => {
2025
};
2126
});
2227

23-
const defaultBabelJestTransformer = babelJest.createTransformer(null);
28+
const defaultBabelJestTransformer = babelJest.createTransformer();
2429

2530
//Mock data for all the tests
2631
const sourceString = `
@@ -49,15 +54,18 @@ test('Returns source string with inline maps when no transformOptions is passed'
4954
configString: JSON.stringify(makeProjectConfig()),
5055
instrument: false,
5156
transformerConfig: {},
52-
},
53-
) as any;
57+
} as TransformOptions<BabelTransformOptions>,
58+
);
59+
5460
expect(typeof result).toBe('object');
5561
expect(result.code).toBeDefined();
5662
expect(result.map).toBeDefined();
5763
expect(result.code).toMatch('//# sourceMappingURL');
5864
expect(result.code).toMatch('customMultiply');
59-
expect(result.map!.sources).toEqual(['dummy_path.js']);
60-
expect(JSON.stringify(result.map!.sourcesContent)).toMatch('customMultiply');
65+
expect((result as BabelFileResult).map!.sources).toEqual(['dummy_path.js']);
66+
expect(
67+
JSON.stringify((result as BabelFileResult).map!.sourcesContent),
68+
).toMatch('customMultiply');
6169
});
6270

6371
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
7078
configString: JSON.stringify(makeProjectConfig()),
7179
instrument: false,
7280
transformerConfig: {},
73-
},
81+
} as TransformOptions<BabelTransformOptions>,
7482
);
83+
7584
expect(typeof result).toBe('object');
7685
expect(result.code).toBeDefined();
7786
expect(result.map).toBeDefined();
@@ -125,7 +134,7 @@ describe('caller option correctly merges from defaults and options', () => {
125134
instrument: false,
126135
transformerConfig: {},
127136
...input,
128-
});
137+
} as TransformOptions<BabelTransformOptions>);
129138

130139
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
131140
expect(loadPartialConfig).toHaveBeenCalledWith(
@@ -142,14 +151,14 @@ describe('caller option correctly merges from defaults and options', () => {
142151
});
143152

144153
test('can pass null to createTransformer', () => {
145-
const transformer = createTransformer(null);
154+
const transformer = createTransformer();
146155
transformer.process(sourceString, 'dummy_path.js', {
147156
cacheFS: new Map<string, string>(),
148157
config: makeProjectConfig(),
149158
configString: JSON.stringify(makeProjectConfig()),
150159
instrument: false,
151160
transformerConfig: {},
152-
});
161+
} as TransformOptions<BabelTransformOptions>);
153162

154163
expect(loadPartialConfig).toHaveBeenCalledTimes(1);
155164
expect(loadPartialConfig).toHaveBeenCalledWith(

packages/diff-sequences/src/__tests__/index.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const assertCommonItems = (
156156
const countDifferences = (
157157
aLength: number,
158158
bLength: number,
159-
isCommon,
159+
isCommon: (aIndex: number, bIndex: number) => boolean,
160160
): number => {
161161
const dMax = aLength + bLength;
162162
const aIndexes = [-1]; // initialize for aLast + 1 in loop when d = 0
@@ -274,7 +274,7 @@ describe('no common items', () => {
274274
// default export does not call findSubsequences nor divide
275275

276276
describe('negative zero is equivalent to zero for length', () => {
277-
const countItemsNegativeZero = (aLength, bLength) => {
277+
const countItemsNegativeZero = (aLength: number, bLength: number) => {
278278
let n = 0;
279279
diff(
280280
aLength,
@@ -301,21 +301,21 @@ describe('no common items', () => {
301301
});
302302

303303
test('a empty and b empty', () => {
304-
const a = [];
305-
const b = [];
306-
const expected = [];
304+
const a: Array<unknown> = [];
305+
const b: Array<unknown> = [];
306+
const expected: Array<unknown> = [];
307307
expectCommonItems(a, b, expected);
308308
});
309309
test('a empty and b non-empty', () => {
310-
const a = [];
310+
const a: Array<unknown> = [];
311311
const b = [false];
312-
const expected = [];
312+
const expected: Array<unknown> = [];
313313
expectCommonItems(a, b, expected);
314314
});
315315
test('a non-empty and b empty', () => {
316316
const a = [false, true];
317-
const b = [];
318-
const expected = [];
317+
const b: Array<unknown> = [];
318+
const expected: Array<unknown> = [];
319319
expectCommonItems(a, b, expected);
320320
});
321321

@@ -327,7 +327,7 @@ describe('no common items', () => {
327327
// last segment cannot have a prev segment
328328
const a = [false];
329329
const b = [true];
330-
const expected = [];
330+
const expected: Array<unknown> = [];
331331
expectCommonItems(a, b, expected);
332332
});
333333
test('baDeltaLength 1 odd', () => {
@@ -336,7 +336,7 @@ describe('no common items', () => {
336336
// last segment has a prev segment because unroll a half iteration
337337
const a = [0, 1];
338338
const b = ['0'];
339-
const expected = [];
339+
const expected: Array<unknown> = [];
340340
expectCommonItems(a, b, expected);
341341
});
342342
test('baDeltaLength 2 even', () => {
@@ -345,7 +345,7 @@ describe('no common items', () => {
345345
// last segment has a prev segment
346346
const a = [0, 1, 2, 3];
347347
const b = ['0', '1'];
348-
const expected = [];
348+
const expected: Array<unknown> = [];
349349
expectCommonItems(a, b, expected);
350350
});
351351
test('baDeltaLength 7 odd', () => {
@@ -354,7 +354,7 @@ describe('no common items', () => {
354354
// last segment has a prev segment
355355
const a = ['0', '1', '2'];
356356
const b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
357-
const expected = [];
357+
const expected: Array<unknown> = [];
358358
expectCommonItems(a, b, expected);
359359
});
360360
});
@@ -734,7 +734,7 @@ const assertCommonSubstring = (
734734

735735
// Return array of substrings in a longest common subsequence of strings.
736736
const findCommonSubstrings = (a: string, b: string): Array<string> => {
737-
const array = [];
737+
const array: Array<string> = [];
738738
diff(
739739
a.length,
740740
b.length,

tsconfig.test.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"composite": false,
55
"emitDeclarationOnly": false,
66
"noEmit": true,
7+
"skipLibCheck": true,
78
"types": ["@jest/test-globals"]
89
}
910
}

0 commit comments

Comments
 (0)