Skip to content

Commit 200adc0

Browse files
Implement tests for type definitions in Jest (#10407)
Co-authored-by: Karan Sanjeev <[email protected]>
1 parent c9c8dba commit 200adc0

File tree

8 files changed

+612
-17
lines changed

8 files changed

+612
-17
lines changed

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ module.exports = {
114114
'import/no-extraneous-dependencies': 0,
115115
},
116116
},
117+
{
118+
files: ['test-types/*.test.ts'],
119+
rules: {
120+
'jest/no-focused-tests': 0,
121+
'jest/no-identical-title': 0,
122+
'jest/valid-expect': 0,
123+
},
124+
},
117125
],
118126
parser: 'babel-eslint',
119127
plugins: ['markdown', 'import', 'prettier', 'eslint-comments'],
@@ -128,6 +136,7 @@ module.exports = {
128136
2,
129137
{
130138
devDependencies: [
139+
'/test-types/**',
131140
'**/__tests__/**',
132141
'**/__mocks__/**',
133142
'**/?(*.)(spec|test).js?(x)',

.github/workflows/nodejs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ jobs:
3939
run: yarn
4040
- name: build
4141
run: yarn build
42+
- name: test typings
43+
run: yarn test-types
4244
- name: verify [email protected] compatibility
4345
run: yarn verify-old-ts
4446
- name: run eslint

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
],
3636
testEnvironment: './packages/jest-environment-node',
3737
testPathIgnorePatterns: [
38+
'/test-types/',
3839
'/__arbitraries__/',
3940
'/node_modules/',
4041
'/examples/',

jest.config.types.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const assert = require('assert');
11+
const baseConfig = require('./jest.config');
12+
13+
const {
14+
modulePathIgnorePatterns,
15+
testPathIgnorePatterns,
16+
watchPathIgnorePatterns,
17+
} = baseConfig;
18+
19+
assert.strictEqual(
20+
testPathIgnorePatterns[0],
21+
'/test-types/',
22+
'First entry must be types',
23+
);
24+
25+
module.exports = {
26+
displayName: {
27+
color: 'blue',
28+
name: 'types',
29+
},
30+
modulePathIgnorePatterns,
31+
runner: 'jest-runner-tsd',
32+
testMatch: ['<rootDir>/test-types/*.test.ts'],
33+
testPathIgnorePatterns: testPathIgnorePatterns.slice(1),
34+
watchPathIgnorePatterns,
35+
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@
5555
"istanbul-reports": "^3.0.0",
5656
"jest": "workspace:packages/jest",
5757
"jest-junit": "^11.0.1",
58+
"jest-runner-tsd": "^1.1.0",
5859
"jest-silent-reporter": "^0.2.1",
5960
"jest-snapshot-serializer-raw": "^1.1.0",
6061
"jest-watch-typeahead": "^0.6.0",
6162
"jquery": "^3.2.1",
6263
"lerna": "^3.20.2",
6364
"micromatch": "^4.0.2",
65+
"mlh-tsd": "^0.14.1",
6466
"mock-fs": "^4.4.1",
6567
"prettier": "^2.0.1",
6668
"progress": "^2.0.0",
@@ -90,6 +92,7 @@
9092
"lint:prettier": "prettier '**/*.{md,yml,yaml}' 'website/static/**/*.{css,js}' --write --ignore-path .gitignore",
9193
"lint:prettier:ci": "prettier '**/*.{md,yml,yaml}' 'website/static/**/*.{css,js}' --check --ignore-path .gitignore",
9294
"publish": "yarn build-clean && yarn build && lerna publish --silent",
95+
"test-types": "yarn jest --config jest.config.types.js",
9396
"test-ci": "yarn jest-coverage --color -i --config jest.config.ci.js && yarn test-leak && node ./scripts/mapCoverage.js && codecov",
9497
"test-ci-partial": "yarn jest --color -i --config jest.config.ci.js",
9598
"test-pretty-format-perf": "node packages/pretty-format/perf/test.js",

test-types/empty.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* This has to be a empty file
8+
* @see https://github.com/MLH-Fellowship/jest-runner-tsd/blob/e25720040939fc79ab38d73c1495be90d5b92566/README.md#for-typescript-projects
9+
*/
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @type ./empty.d.ts
8+
*/
9+
10+
import {expectError, expectType} from 'mlh-tsd';
11+
//eslint-disable-next-line import/no-extraneous-dependencies
12+
import {jest} from '@jest/globals';
13+
14+
expectType<void>(jest.addMatchers({}));
15+
expectType<typeof jest>(jest.autoMockOff());
16+
expectType<typeof jest>(jest.autoMockOn());
17+
expectType<typeof jest>(jest.clearAllMocks());
18+
expectType<void>(jest.clearAllTimers());
19+
expectType<typeof jest>(jest.resetAllMocks());
20+
expectType<typeof jest>(jest.restoreAllMocks());
21+
expectType<void>(jest.clearAllTimers());
22+
expectType<typeof jest>(jest.deepUnmock('moduleName'));
23+
expectType<typeof jest>(jest.disableAutomock());
24+
expectType<typeof jest>(jest.doMock('moduleName'));
25+
expectType<typeof jest>(jest.doMock('moduleName', jest.fn()));
26+
27+
expectError(jest.doMock('moduleName', jest.fn(), {}));
28+
expectError(jest.doMock('moduleName', jest.fn(), {virtual: true}));
29+
30+
expectType<typeof jest>(jest.dontMock('moduleName'));
31+
expectType<typeof jest>(jest.enableAutomock());
32+
expectType<typeof jest>(jest.mock('moduleName'));
33+
expectType<typeof jest>(jest.mock('moduleName', jest.fn()));
34+
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {}));
35+
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {virtual: true}));
36+
expectType<typeof jest>(jest.resetModuleRegistry());
37+
expectType<typeof jest>(jest.resetModules());
38+
expectType<typeof jest>(jest.isolateModules(() => {}));
39+
expectType<typeof jest>(jest.retryTimes(3));
40+
41+
expectType<void>(jest.runAllImmediates());
42+
expectType<void>(jest.runAllTicks());
43+
expectType<void>(jest.runAllTimers());
44+
expectType<void>(jest.runOnlyPendingTimers());
45+
expectType<void>(jest.runTimersToTime(9001));
46+
expectType<void>(jest.advanceTimersByTime(9001));
47+
48+
expectType<typeof jest>(jest.setMock('moduleName', {}));
49+
expectType<typeof jest>(jest.setMock('moduleName', {}));
50+
expectType<typeof jest>(jest.setMock('moduleName', {a: 'b'}));
51+
expectType<typeof jest>(jest.setTimeout(9001));
52+
expectType<typeof jest>(jest.unmock('moduleName'));
53+
expectType<typeof jest>(jest.useFakeTimers());
54+
expectType<typeof jest>(jest.useRealTimers());
55+
56+
expectType<void>(jest.advanceTimersToNextTimer());
57+
expectType<void>(jest.advanceTimersToNextTimer(2));
58+
59+
// https://jestjs.io/docs/en/jest-object#jestusefaketimersimplementation-modern--legacy
60+
expectType<typeof jest>(jest.useFakeTimers('modern'));
61+
expectType<typeof jest>(jest.useFakeTimers('legacy'));
62+
63+
expectError(jest.useFakeTimers('foo'));
64+
65+
// https://jestjs.io/docs/en/jest-object#jestsetsystemtimenow-number--date
66+
expectType<void>(jest.setSystemTime());
67+
expectType<void>(jest.setSystemTime(0));
68+
expectType<void>(jest.setSystemTime(new Date(0)));
69+
70+
expectError(jest.setSystemTime('foo'));
71+
72+
// https://jestjs.io/docs/en/jest-object#jestgetrealsystemtime
73+
expectType<number>(jest.getRealSystemTime());
74+
75+
expectError(jest.getRealSystemTime('foo'));
76+
77+
// https://jestjs.io/docs/en/jest-object#jestrequireactualmodulename
78+
expectType<unknown>(jest.requireActual('./thisReturnsTheActualModule'));
79+
80+
// https://jestjs.io/docs/en/jest-object#jestrequiremockmodulename
81+
expectType<unknown>(jest.requireMock('./thisAlwaysReturnsTheMock'));

0 commit comments

Comments
 (0)