Skip to content

Commit e998c92

Browse files
doniyor2109SimenB
authored andcommitted
TS migration jest-circus (#7916)
* Migration to ts (part 1) * Migration to ts (part 2) * Migration to ts (part 3) * Minor tweaks * TS migration (part 4) * Wrong dts * dts for co * Added project references * Remove not ts module * Replace custom co dts with @types/co * No index file Co-Authored-By: doniyor2109 <doniyor2109@gmail.com> * Some tweaks * Some tweaks * Temp DiffOptions type * Remove extra eslint disable * Workaround for global vars * Resolves * Move @types/co to devDeps * Update packages/jest-circus/src/run.ts Co-Authored-By: doniyor2109 <doniyor2109@gmail.com> * Update packages/jest-circus/src/utils.ts Co-Authored-By: doniyor2109 <doniyor2109@gmail.com> * Tweaks * Remove extra types * Fix failing test Cannot run ts via node cli * TS migration part (4) - Added Global and Environment types - Workaround for RawMatcherFn type * Fix linter errors * Fix types for tests * Fix @jest/types cannot be found in test * Detailed comment for flowfix * Ignore ts errors for non migrated modules * `import =` is not supported by @babel/plugin-transform-typescript * Fix weired ts error Exported variable 'jestAdapter' has or is using name '$JestEnvironment' from external module "packages/jest-types/build/Environment" but cannot be named. microsoft/TypeScript#5711 * Fix linter errors * Remove extra eslint disables * Move expect types to @jest/types * tweaks * remove jest config change * fix test * Added reminder for replacing Each type * keep comments from old tests * Update CHANGELOG.md
1 parent 88d6b72 commit e998c92

30 files changed

Lines changed: 799 additions & 422 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
- `[docs]` Add missing import to docs ([#7928](https://github.com/facebook/jest/pull/7928))
5555
- `[jest-resolve-dependencies]`: Migrate to TypeScript ([#7922](https://github.com/facebook/jest/pull/7922))
5656
- `[expect]`: Migrate to TypeScript ([#7919](https://github.com/facebook/jest/pull/7919))
57+
- `[jest-circus]`: Migrate to TypeScript ([#7916](https://github.com/facebook/jest/pull/7916))
5758

5859
### Performance
5960

packages/jest-circus/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
},
99
"license": "MIT",
1010
"main": "build/index.js",
11+
"types": "build/index.d.ts",
1112
"dependencies": {
1213
"@babel/traverse": "^7.1.0",
14+
"@jest/types": "^24.1.0",
15+
"@types/node": "*",
1316
"chalk": "^2.0.1",
1417
"co": "^4.6.0",
1518
"expect": "^24.1.0",
@@ -25,6 +28,7 @@
2528
},
2629
"devDependencies": {
2730
"@types/babel__traverse": "^7.0.4",
31+
"@types/co": "^4.6.0",
2832
"@types/stack-utils": "^1.0.1",
2933
"execa": "^1.0.0",
3034
"jest-diff": "^24.0.0",

packages/jest-circus/src/__mocks__/testEventHandler.js renamed to packages/jest-circus/src/__mocks__/testEventHandler.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7-
*
8-
* @flow scrict-local
97
*/
108

11-
'use strict';
12-
13-
import type {EventHandler} from 'types/Circus';
9+
import {EventHandler} from '../types';
1410

1511
const testEventHandler: EventHandler = (event, state) => {
1612
switch (event.name) {

packages/jest-circus/src/__mocks__/testUtils.js renamed to packages/jest-circus/src/__mocks__/testUtils.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7-
*
8-
* @flow strict-local
97
*/
108

11-
'use strict';
12-
// $FlowFixMe - execa is untyped
13-
import {sync as spawnSync} from 'execa';
149
import fs from 'fs';
1510
import os from 'os';
1611
import path from 'path';
1712
import crypto from 'crypto';
13+
import {sync as spawnSync, ExecaReturns} from 'execa';
14+
// @ts-ignore
1815
import {skipSuiteOnWindows} from '../../../../scripts/ConditionalTest';
1916

20-
const CIRCUS_PATH = require.resolve('../../build/index');
17+
const CIRCUS_PATH = require.resolve('../../build');
2118
const CIRCUS_RUN_PATH = require.resolve('../../build/run');
2219
const CIRCUS_STATE_PATH = require.resolve('../../build/state');
2320
const TEST_EVENT_HANDLER_PATH = require.resolve('./testEventHandler');
2421
const BABEL_REGISTER_PATH = require.resolve('@babel/register');
2522

2623
skipSuiteOnWindows();
2724

25+
interface Result extends ExecaReturns {
26+
status: number;
27+
error: string;
28+
}
29+
2830
export const runTest = (source: string) => {
2931
const filename = crypto
3032
.createHash('md5')
@@ -33,7 +35,7 @@ export const runTest = (source: string) => {
3335
const tmpFilename = path.join(os.tmpdir(), filename);
3436

3537
const content = `
36-
require('${BABEL_REGISTER_PATH}');
38+
require('${BABEL_REGISTER_PATH}')({extensions: [".js", ".ts"]});
3739
const circus = require('${CIRCUS_PATH}');
3840
global.test = circus.test;
3941
global.describe = circus.describe;
@@ -54,7 +56,9 @@ export const runTest = (source: string) => {
5456
`;
5557

5658
fs.writeFileSync(tmpFilename, content);
57-
const result = spawnSync('node', [tmpFilename], {cwd: process.cwd()});
59+
const result = spawnSync('node', [tmpFilename], {
60+
cwd: process.cwd(),
61+
}) as Result;
5862

5963
// For compat with cross-spawn
6064
result.status = result.code;

packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.js.snap renamed to packages/jest-circus/src/__tests__/__snapshots__/afterAll.test.ts.snap

File renamed without changes.

packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.js.snap renamed to packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap

File renamed without changes.

packages/jest-circus/src/__tests__/__snapshots__/hooks.test.js.snap renamed to packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap

File renamed without changes.

packages/jest-circus/src/__tests__/afterAll.test.js renamed to packages/jest-circus/src/__tests__/afterAll.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7-
*
8-
* @flow strict-local
97
*/
108

11-
'use strict';
12-
139
import {runTest} from '../__mocks__/testUtils';
1410

1511
test('tests are not marked done until their parent afterAll runs', () => {

packages/jest-circus/src/__tests__/baseTest.test.js renamed to packages/jest-circus/src/__tests__/baseTest.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
* This source code is licensed under the BSD-style license found in the
55
* LICENSE file in the root directory of this source tree. An additional grant
66
* of patent rights can be found in the PATENTS file in the same directory.
7-
*
8-
* @flow strict-local
97
*/
108

11-
'use strict';
12-
139
import {runTest} from '../__mocks__/testUtils';
1410

1511
test('simple test', () => {

packages/jest-circus/src/__tests__/circusItTestError.test.js renamed to packages/jest-circus/src/__tests__/circusItTestError.test.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
6-
*
7-
* @flow strict-local
86
*/
97

10-
'use strict';
8+
import {Global} from '@jest/types';
119

12-
let circusIt;
13-
let circusTest;
10+
let circusIt: Global.It;
11+
let circusTest: Global.It;
1412

1513
// using jest-jasmine2's 'it' to test jest-circus's 'it'. Had to differentiate
1614
// the two with this alias.
1715

1816
const aliasCircusIt = () => {
19-
const {it, test} = require('../index.js');
17+
const {it, test} = require('../');
2018
circusIt = it;
2119
circusTest = test;
2220
};
@@ -35,21 +33,21 @@ describe('test/it error throwing', () => {
3533
});
3634
it(`it throws error with missing callback function`, () => {
3735
expect(() => {
38-
// $FlowFixMe: Easy, we're testing runtime errors here
36+
// @ts-ignore: Easy, we're testing runtime errors here
3937
circusIt('test2');
4038
}).toThrowError(
4139
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
4240
);
4341
});
4442
it(`it throws an error when first argument isn't a string`, () => {
4543
expect(() => {
46-
// $FlowFixMe: Easy, we're testing runtime errors here
44+
// @ts-ignore: Easy, we're testing runtime errors here
4745
circusIt(() => {});
4846
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
4947
});
5048
it('it throws an error when callback function is not a function', () => {
5149
expect(() => {
52-
// $FlowFixMe: Easy, we're testing runtime errors here
50+
// @ts-ignore: Easy, we're testing runtime errors here
5351
circusIt('test4', 'test4b');
5452
}).toThrowError(
5553
'Invalid second argument, test4b. It must be a callback function.',
@@ -62,21 +60,21 @@ describe('test/it error throwing', () => {
6260
});
6361
it(`test throws error with missing callback function`, () => {
6462
expect(() => {
65-
// $FlowFixMe: Easy, we're testing runtime errors here
63+
// @ts-ignore: Easy, we're testing runtime errors here
6664
circusTest('test6');
6765
}).toThrowError(
6866
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
6967
);
7068
});
7169
it(`test throws an error when first argument isn't a string`, () => {
7270
expect(() => {
73-
// $FlowFixMe: Easy, we're testing runtime errors here
71+
// @ts-ignore: Easy, we're testing runtime errors here
7472
circusTest(() => {});
7573
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
7674
});
7775
it('test throws an error when callback function is not a function', () => {
7876
expect(() => {
79-
// $FlowFixMe: Easy, we're testing runtime errors here
77+
// @ts-ignore: Easy, we're testing runtime errors here
8078
circusTest('test8', 'test8b');
8179
}).toThrowError(
8280
'Invalid second argument, test8b. It must be a callback function.',

0 commit comments

Comments
 (0)