Skip to content

Commit

Permalink
TS migration jest-circus (#7916)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
doniyor2109 authored and SimenB committed Feb 22, 2019
1 parent 88d6b72 commit e998c92
Show file tree
Hide file tree
Showing 30 changed files with 799 additions and 422 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -54,6 +54,7 @@
- `[docs]` Add missing import to docs ([#7928](https://github.com/facebook/jest/pull/7928))
- `[jest-resolve-dependencies]`: Migrate to TypeScript ([#7922](https://github.com/facebook/jest/pull/7922))
- `[expect]`: Migrate to TypeScript ([#7919](https://github.com/facebook/jest/pull/7919))
- `[jest-circus]`: Migrate to TypeScript ([#7916](https://github.com/facebook/jest/pull/7916))

### Performance

Expand Down
4 changes: 4 additions & 0 deletions packages/jest-circus/package.json
Expand Up @@ -8,8 +8,11 @@
},
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"dependencies": {
"@babel/traverse": "^7.1.0",
"@jest/types": "^24.1.0",
"@types/node": "*",
"chalk": "^2.0.1",
"co": "^4.6.0",
"expect": "^24.1.0",
Expand All @@ -25,6 +28,7 @@
},
"devDependencies": {
"@types/babel__traverse": "^7.0.4",
"@types/co": "^4.6.0",
"@types/stack-utils": "^1.0.1",
"execa": "^1.0.0",
"jest-diff": "^24.0.0",
Expand Down
Expand Up @@ -4,13 +4,9 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow scrict-local
*/

'use strict';

import type {EventHandler} from 'types/Circus';
import {EventHandler} from '../types';

const testEventHandler: EventHandler = (event, state) => {
switch (event.name) {
Expand Down
Expand Up @@ -4,27 +4,29 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow strict-local
*/

'use strict';
// $FlowFixMe - execa is untyped
import {sync as spawnSync} from 'execa';
import fs from 'fs';
import os from 'os';
import path from 'path';
import crypto from 'crypto';
import {sync as spawnSync, ExecaReturns} from 'execa';
// @ts-ignore
import {skipSuiteOnWindows} from '../../../../scripts/ConditionalTest';

const CIRCUS_PATH = require.resolve('../../build/index');
const CIRCUS_PATH = require.resolve('../../build');
const CIRCUS_RUN_PATH = require.resolve('../../build/run');
const CIRCUS_STATE_PATH = require.resolve('../../build/state');
const TEST_EVENT_HANDLER_PATH = require.resolve('./testEventHandler');
const BABEL_REGISTER_PATH = require.resolve('@babel/register');

skipSuiteOnWindows();

interface Result extends ExecaReturns {
status: number;
error: string;
}

export const runTest = (source: string) => {
const filename = crypto
.createHash('md5')
Expand All @@ -33,7 +35,7 @@ export const runTest = (source: string) => {
const tmpFilename = path.join(os.tmpdir(), filename);

const content = `
require('${BABEL_REGISTER_PATH}');
require('${BABEL_REGISTER_PATH}')({extensions: [".js", ".ts"]});
const circus = require('${CIRCUS_PATH}');
global.test = circus.test;
global.describe = circus.describe;
Expand All @@ -54,7 +56,9 @@ export const runTest = (source: string) => {
`;

fs.writeFileSync(tmpFilename, content);
const result = spawnSync('node', [tmpFilename], {cwd: process.cwd()});
const result = spawnSync('node', [tmpFilename], {
cwd: process.cwd(),
}) as Result;

// For compat with cross-spawn
result.status = result.code;
Expand Down
Expand Up @@ -4,12 +4,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow strict-local
*/

'use strict';

import {runTest} from '../__mocks__/testUtils';

test('tests are not marked done until their parent afterAll runs', () => {
Expand Down
Expand Up @@ -4,12 +4,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow strict-local
*/

'use strict';

import {runTest} from '../__mocks__/testUtils';

test('simple test', () => {
Expand Down
Expand Up @@ -3,20 +3,18 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
*/

'use strict';
import {Global} from '@jest/types';

let circusIt;
let circusTest;
let circusIt: Global.It;
let circusTest: Global.It;

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

const aliasCircusIt = () => {
const {it, test} = require('../index.js');
const {it, test} = require('../');
circusIt = it;
circusTest = test;
};
Expand All @@ -35,21 +33,21 @@ describe('test/it error throwing', () => {
});
it(`it throws error with missing callback function`, () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusIt('test2');
}).toThrowError(
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
);
});
it(`it throws an error when first argument isn't a string`, () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusIt(() => {});
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
});
it('it throws an error when callback function is not a function', () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusIt('test4', 'test4b');
}).toThrowError(
'Invalid second argument, test4b. It must be a callback function.',
Expand All @@ -62,21 +60,21 @@ describe('test/it error throwing', () => {
});
it(`test throws error with missing callback function`, () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusTest('test6');
}).toThrowError(
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
);
});
it(`test throws an error when first argument isn't a string`, () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusTest(() => {});
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
});
it('test throws an error when callback function is not a function', () => {
expect(() => {
// $FlowFixMe: Easy, we're testing runtime errors here
// @ts-ignore: Easy, we're testing runtime errors here
circusTest('test8', 'test8b');
}).toThrowError(
'Invalid second argument, test8b. It must be a callback function.',
Expand Down
Expand Up @@ -3,19 +3,17 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
*/

'use strict';
import {Global} from '@jest/types';

let circusIt;
let circusIt: Global.It;

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

const aliasCircusIt = () => {
const {it} = require('../index.js');
const {it} = require('../');
circusIt = it;
};

Expand All @@ -24,7 +22,7 @@ aliasCircusIt();
describe('test/it.todo error throwing', () => {
it('todo throws error when given no arguments', () => {
expect(() => {
// $FlowFixMe: Testing runitme errors here
// @ts-ignore: Testing runtime errors here
circusIt.todo();
}).toThrowError('Todo must be called with only a description.');
});
Expand All @@ -35,7 +33,7 @@ describe('test/it.todo error throwing', () => {
});
it('todo throws error when given none string description', () => {
expect(() => {
// $FlowFixMe: Testing runitme errors here
// @ts-ignore: Testing runtime errors here
circusIt.todo(() => {});
}).toThrowError('Todo must be called with only a description.');
});
Expand Down
Expand Up @@ -4,12 +4,8 @@
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow strict-local
*/

'use strict';

import {runTest} from '../__mocks__/testUtils';

test('beforeEach is executed before each test in current/child describe blocks', () => {
Expand Down
Expand Up @@ -3,17 +3,14 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

'use strict';

const circus = require('../index.js');
import circus from '../';
import {HookType} from '../types';

describe.each([['beforeEach'], ['beforeAll'], ['afterEach'], ['afterAll']])(
describe.each(['beforeEach', 'beforeAll', 'afterEach', 'afterAll'])(
'%s hooks error throwing',
fn => {
(fn: HookType) => {
test.each([
['String'],
[1],
Expand Down
Expand Up @@ -3,11 +3,9 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
*/

import type {EventHandler, Exception} from 'types/Circus';
import {EventHandler, TEST_TIMEOUT_SYMBOL} from './types';

import {
addErrorToEachTestUnderDescribe,
Expand All @@ -22,9 +20,6 @@ import {
restoreGlobalErrorHandlers,
} from './globalErrorHandlers';

// To pass this value from Runtime object to state we need to use global[sym]
const TEST_TIMEOUT_SYMBOL = Symbol.for('TEST_TIMEOUT_SYMBOL');

const eventHandler: EventHandler = (event, state): void => {
switch (event.name) {
case 'include_test_location_in_result': {
Expand Down Expand Up @@ -112,14 +107,14 @@ const eventHandler: EventHandler = (event, state): void => {

if (type === 'beforeAll') {
invariant(describeBlock, 'always present for `*All` hooks');
addErrorToEachTestUnderDescribe(describeBlock, error, asyncError);
addErrorToEachTestUnderDescribe(describeBlock!, error, asyncError);
} else if (type === 'afterAll') {
// Attaching `afterAll` errors to each test makes execution flow
// too complicated, so we'll consider them to be global.
state.unhandledErrors.push([error, asyncError]);
} else {
invariant(test, 'always present for `*Each` hooks');
test.errors.push([error, asyncError]);
test!.errors.push([error, asyncError]);
}
break;
}
Expand Down Expand Up @@ -152,8 +147,7 @@ const eventHandler: EventHandler = (event, state): void => {
break;
}
case 'test_retry': {
const errors: Array<[?Exception, Exception]> = [];
event.test.errors = errors;
event.test.errors = [];
break;
}
case 'run_start': {
Expand Down Expand Up @@ -185,8 +179,8 @@ const eventHandler: EventHandler = (event, state): void => {
invariant(state.originalGlobalErrorHandlers);
invariant(state.parentProcess);
restoreGlobalErrorHandlers(
state.parentProcess,
state.originalGlobalErrorHandlers,
state.parentProcess!,
state.originalGlobalErrorHandlers!,
);
break;
}
Expand Down

0 comments on commit e998c92

Please sign in to comment.