Skip to content

Commit

Permalink
feat: Allow transforming jest-runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Aug 19, 2019
1 parent 08f109c commit 9f2e5ba
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 1 deletion.
11 changes: 11 additions & 0 deletions e2e/__tests__/transform.test.ts
Expand Up @@ -185,3 +185,14 @@ describe('transformer-config', () => {
expect(stdout).toMatchSnapshot();
});
});

describe('transform-runner', () => {
const dir = path.resolve(__dirname, '../transform/transform-runner');

it('should transform runner', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
10 changes: 10 additions & 0 deletions e2e/transform/transform-runner/__tests__/add.test.js
@@ -0,0 +1,10 @@
/**
* 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.
*/

it('should add two numbers', () => {
expect(1 + 1).toBe(2);
});
18 changes: 18 additions & 0 deletions e2e/transform/transform-runner/babel.config.js
@@ -0,0 +1,18 @@
/**
* 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.
*/

module.exports = {
presets: [
['@babel/preset-typescript'],
[
'@babel/preset-env',
{
targets: {node: 6},
},
],
],
};
11 changes: 11 additions & 0 deletions e2e/transform/transform-runner/package.json
@@ -0,0 +1,11 @@
{
"jest": {
"rootDir": "./",
"runner": "<rootDir>/runner.ts"
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"jest-environment-node": "^24.9.0"
}
}
79 changes: 79 additions & 0 deletions e2e/transform/transform-runner/runner.ts
@@ -0,0 +1,79 @@
/**
* 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.
*/

import {Config} from '@jest/types';
import {
OnTestFailure,
OnTestStart,
OnTestSuccess,
Test,
TestRunnerContext,
TestWatcher,
} from 'jest-runner';
import throat from 'throat';
import {TestResult} from '@jest/test-result';

export default class BaseTestRunner {
private _globalConfig: Config.GlobalConfig;
private _context: TestRunnerContext;

constructor(globalConfig: Config.GlobalConfig, context?: TestRunnerContext) {
this._globalConfig = globalConfig;
this._context = context || {};
}

async runTests(
tests: Array<Test>,
watcher: TestWatcher,
onStart: OnTestStart,
onResult: OnTestSuccess,
onFailure: OnTestFailure
): Promise<void> {
const mutex = throat(1);
return tests.reduce(
(promise, test) =>
mutex(() =>
promise
.then(async () => {
await onStart(test);
return {
numFailingTests: 0,
numPassingTests: 1,
numPendingTests: 0,
numTodoTests: 0,
perfStats: {
end: 0,
start: 0,
},
snapshot: {
added: 0,
fileDeleted: false,
matched: 0,
unchecked: 0,
unmatched: 0,
updated: 0,
},
testFilePath: test.path,
testResults: [
{
ancestorTitles: [],
duration: 2,
failureMessages: [],
fullName: 'sample test',
status: 'passed',
title: 'sample test',
},
],
} as TestResult;
})
.then(result => onResult(test, result))
.catch(err => onFailure(test, err))
),
Promise.resolve()
);
}
}
6 changes: 6 additions & 0 deletions e2e/transform/transform-runner/tsconfig.json
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
}
}
7 changes: 6 additions & 1 deletion packages/jest-core/src/TestScheduler.ts
Expand Up @@ -8,6 +8,7 @@
import chalk from 'chalk';
import {formatExecError} from 'jest-message-util';
import {Config} from '@jest/types';
import {ScriptTransformer} from '@jest/transform';
import snapshot from 'jest-snapshot';
import TestRunner, {Test} from 'jest-runner';
import {Context} from 'jest-runtime';
Expand All @@ -28,6 +29,7 @@ import {
buildFailureTestResult,
makeEmptyAggregatedTestResult,
} from '@jest/test-result';
import interopRequireDefault from 'jest-util/build/interopRequireDefault';
import ReporterDispatcher from './ReporterDispatcher';
import TestWatcher from './TestWatcher';
import {shouldRunInBand} from './testSchedulerHelper';
Expand Down Expand Up @@ -171,7 +173,10 @@ export default class TestScheduler {
const testRunners = Object.create(null);
contexts.forEach(({config}) => {
if (!testRunners[config.runner]) {
const Runner: typeof TestRunner = require(config.runner);
const transformer = new ScriptTransformer(config);
const Runner: typeof TestRunner = interopRequireDefault(
transformer.requireAndTranspileModule(config.runner),
).default;
testRunners[config.runner] = new Runner(this._globalConfig, {
changedFiles: this._context && this._context.changedFiles,
});
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-core/src/__tests__/TestScheduler.test.js
Expand Up @@ -88,6 +88,7 @@ test('schedule tests run in parallel per default', async () => {
context: {
config: {
runner: 'jest-runner-parallel',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand All @@ -109,6 +110,7 @@ test('schedule tests run in serial if the runner flags them', async () => {
context: {
config: {
runner: 'jest-runner-serial',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand All @@ -131,6 +133,7 @@ test('should bail after `n` failures', async () => {
config: {
rootDir: './',
runner: 'jest-runner-serial',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand Down Expand Up @@ -161,6 +164,7 @@ test('should not bail if less than `n` failures', async () => {
config: {
rootDir: './',
runner: 'jest-runner-serial',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand Down Expand Up @@ -190,6 +194,7 @@ test('should set runInBand to run in serial', async () => {
context: {
config: {
runner: 'jest-runner-parallel',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand All @@ -214,6 +219,7 @@ test('should set runInBand to not run in serial', async () => {
context: {
config: {
runner: 'jest-runner-parallel',
transform: [],
},
hasteFS: {
matchFiles: jest.fn(() => []),
Expand Down

0 comments on commit 9f2e5ba

Please sign in to comment.