Skip to content

Commit

Permalink
Implement tests for type definitions in Jest (#10407)
Browse files Browse the repository at this point in the history
Co-authored-by: Karan Sanjeev <yo@alphaman.me>
  • Loading branch information
SaurabhAgarwala and smartclash authored Aug 19, 2020
1 parent c9c8dba commit 200adc0
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ module.exports = {
'import/no-extraneous-dependencies': 0,
},
},
{
files: ['test-types/*.test.ts'],
rules: {
'jest/no-focused-tests': 0,
'jest/no-identical-title': 0,
'jest/valid-expect': 0,
},
},
],
parser: 'babel-eslint',
plugins: ['markdown', 'import', 'prettier', 'eslint-comments'],
Expand All @@ -128,6 +136,7 @@ module.exports = {
2,
{
devDependencies: [
'/test-types/**',
'**/__tests__/**',
'**/__mocks__/**',
'**/?(*.)(spec|test).js?(x)',
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
run: yarn
- name: build
run: yarn build
- name: test typings
run: yarn test-types
- name: verify TypeScript@3.8 compatibility
run: yarn verify-old-ts
- name: run eslint
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
],
testEnvironment: './packages/jest-environment-node',
testPathIgnorePatterns: [
'/test-types/',
'/__arbitraries__/',
'/node_modules/',
'/examples/',
Expand Down
35 changes: 35 additions & 0 deletions jest.config.types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const assert = require('assert');
const baseConfig = require('./jest.config');

const {
modulePathIgnorePatterns,
testPathIgnorePatterns,
watchPathIgnorePatterns,
} = baseConfig;

assert.strictEqual(
testPathIgnorePatterns[0],
'/test-types/',
'First entry must be types',
);

module.exports = {
displayName: {
color: 'blue',
name: 'types',
},
modulePathIgnorePatterns,
runner: 'jest-runner-tsd',
testMatch: ['<rootDir>/test-types/*.test.ts'],
testPathIgnorePatterns: testPathIgnorePatterns.slice(1),
watchPathIgnorePatterns,
};
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
"istanbul-reports": "^3.0.0",
"jest": "workspace:packages/jest",
"jest-junit": "^11.0.1",
"jest-runner-tsd": "^1.1.0",
"jest-silent-reporter": "^0.2.1",
"jest-snapshot-serializer-raw": "^1.1.0",
"jest-watch-typeahead": "^0.6.0",
"jquery": "^3.2.1",
"lerna": "^3.20.2",
"micromatch": "^4.0.2",
"mlh-tsd": "^0.14.1",
"mock-fs": "^4.4.1",
"prettier": "^2.0.1",
"progress": "^2.0.0",
Expand Down Expand Up @@ -90,6 +92,7 @@
"lint:prettier": "prettier '**/*.{md,yml,yaml}' 'website/static/**/*.{css,js}' --write --ignore-path .gitignore",
"lint:prettier:ci": "prettier '**/*.{md,yml,yaml}' 'website/static/**/*.{css,js}' --check --ignore-path .gitignore",
"publish": "yarn build-clean && yarn build && lerna publish --silent",
"test-types": "yarn jest --config jest.config.types.js",
"test-ci": "yarn jest-coverage --color -i --config jest.config.ci.js && yarn test-leak && node ./scripts/mapCoverage.js && codecov",
"test-ci-partial": "yarn jest --color -i --config jest.config.ci.js",
"test-pretty-format-perf": "node packages/pretty-format/perf/test.js",
Expand Down
9 changes: 9 additions & 0 deletions test-types/empty.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* This has to be a empty file
* @see https://github.com/MLH-Fellowship/jest-runner-tsd/blob/e25720040939fc79ab38d73c1495be90d5b92566/README.md#for-typescript-projects
*/
81 changes: 81 additions & 0 deletions test-types/top-level-jest-namespace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @type ./empty.d.ts
*/

import {expectError, expectType} from 'mlh-tsd';
//eslint-disable-next-line import/no-extraneous-dependencies
import {jest} from '@jest/globals';

expectType<void>(jest.addMatchers({}));
expectType<typeof jest>(jest.autoMockOff());
expectType<typeof jest>(jest.autoMockOn());
expectType<typeof jest>(jest.clearAllMocks());
expectType<void>(jest.clearAllTimers());
expectType<typeof jest>(jest.resetAllMocks());
expectType<typeof jest>(jest.restoreAllMocks());
expectType<void>(jest.clearAllTimers());
expectType<typeof jest>(jest.deepUnmock('moduleName'));
expectType<typeof jest>(jest.disableAutomock());
expectType<typeof jest>(jest.doMock('moduleName'));
expectType<typeof jest>(jest.doMock('moduleName', jest.fn()));

expectError(jest.doMock('moduleName', jest.fn(), {}));
expectError(jest.doMock('moduleName', jest.fn(), {virtual: true}));

expectType<typeof jest>(jest.dontMock('moduleName'));
expectType<typeof jest>(jest.enableAutomock());
expectType<typeof jest>(jest.mock('moduleName'));
expectType<typeof jest>(jest.mock('moduleName', jest.fn()));
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {}));
expectType<typeof jest>(jest.mock('moduleName', jest.fn(), {virtual: true}));
expectType<typeof jest>(jest.resetModuleRegistry());
expectType<typeof jest>(jest.resetModules());
expectType<typeof jest>(jest.isolateModules(() => {}));
expectType<typeof jest>(jest.retryTimes(3));

expectType<void>(jest.runAllImmediates());
expectType<void>(jest.runAllTicks());
expectType<void>(jest.runAllTimers());
expectType<void>(jest.runOnlyPendingTimers());
expectType<void>(jest.runTimersToTime(9001));
expectType<void>(jest.advanceTimersByTime(9001));

expectType<typeof jest>(jest.setMock('moduleName', {}));
expectType<typeof jest>(jest.setMock('moduleName', {}));
expectType<typeof jest>(jest.setMock('moduleName', {a: 'b'}));
expectType<typeof jest>(jest.setTimeout(9001));
expectType<typeof jest>(jest.unmock('moduleName'));
expectType<typeof jest>(jest.useFakeTimers());
expectType<typeof jest>(jest.useRealTimers());

expectType<void>(jest.advanceTimersToNextTimer());
expectType<void>(jest.advanceTimersToNextTimer(2));

// https://jestjs.io/docs/en/jest-object#jestusefaketimersimplementation-modern--legacy
expectType<typeof jest>(jest.useFakeTimers('modern'));
expectType<typeof jest>(jest.useFakeTimers('legacy'));

expectError(jest.useFakeTimers('foo'));

// https://jestjs.io/docs/en/jest-object#jestsetsystemtimenow-number--date
expectType<void>(jest.setSystemTime());
expectType<void>(jest.setSystemTime(0));
expectType<void>(jest.setSystemTime(new Date(0)));

expectError(jest.setSystemTime('foo'));

// https://jestjs.io/docs/en/jest-object#jestgetrealsystemtime
expectType<number>(jest.getRealSystemTime());

expectError(jest.getRealSystemTime('foo'));

// https://jestjs.io/docs/en/jest-object#jestrequireactualmodulename
expectType<unknown>(jest.requireActual('./thisReturnsTheActualModule'));

// https://jestjs.io/docs/en/jest-object#jestrequiremockmodulename
expectType<unknown>(jest.requireMock('./thisAlwaysReturnsTheMock'));
Loading

0 comments on commit 200adc0

Please sign in to comment.