Skip to content

Commit

Permalink
feat: Allow testRunner to be configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Aug 22, 2019
1 parent 522ed17 commit 7caaa64
Show file tree
Hide file tree
Showing 15 changed files with 119 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@
- `[jest-config]` [**BREAKING**] Set default display name color based on runner ([#8689](https://github.com/facebook/jest/pull/8689))
- `[jest-diff]` Add options for colors and symbols ([#8841](https://github.com/facebook/jest/pull/8841))
- `[@jest/test-result]` Create method to create empty `TestResult` ([#8867](https://github.com/facebook/jest/pull/8867))
- `[jest-runner]` [**BREAKING**] Run transforms over `testRunnner` ([#8823](https://github.com/facebook/jest/pull/8823))

### Fixes

Expand Down
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-testrunner', () => {
const dir = path.resolve(__dirname, '../transform/transform-testrunner');

it('should transform testRunner', () => {
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
6 changes: 5 additions & 1 deletion e2e/babel-plugin-jest-hoist/package.json
Expand Up @@ -7,6 +7,10 @@
},
"jest": {
"automock": true,
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"jest-jasmine2",
"jest-circus"
]
}
}
5 changes: 5 additions & 0 deletions e2e/coverage-transform-instrumented/package.json
Expand Up @@ -4,6 +4,11 @@
"transform": {
"^.+\\.(js)$": "<rootDir>/preprocessor.js"
},
"transformIgnorePatterns": [
"jest-jasmine2",
"jest-each",
"jest-circus"
],
"testRegex": "/__tests__/.*\\.(js)$",
"testEnvironment": "node",
"moduleFileExtensions": [
Expand Down
5 changes: 4 additions & 1 deletion e2e/transform-linked-modules/package.json
Expand Up @@ -4,7 +4,10 @@
"transformIgnorePatterns": [
"/node_modules/",
"<rootDir>/__tests__",
"<rootDir>/ignored/"
"<rootDir>/ignored/",
"jest-jasmine2",
"jest-each",
"jest-circus"
],
"transform": {
"^.+\\.js$": "<rootDir>/preprocessor.js"
Expand Down
4 changes: 3 additions & 1 deletion e2e/transform/babel-jest-ignored/babel.config.js
Expand Up @@ -5,4 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = {only: ['blablabla']};
module.exports = {
only: ['blablabla', /jest-jasmine2/, /jest-circus/, /jest-each/],
};
1 change: 1 addition & 0 deletions e2e/transform/multiple-transformers/package.json
Expand Up @@ -5,6 +5,7 @@
"^.+\\.js$": "<rootDir>/jsPreprocessor.js",
"^.+\\.svg$": "<rootDir>/filePreprocessor.js"
},
"transformIgnorePatterns": ["jest-jasmine2", "jest-circus"],
"testEnvironment": "node"
},
"dependencies": {
Expand Down
10 changes: 10 additions & 0 deletions e2e/transform/transform-testrunner/__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-testrunner/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},
},
],
],
};
10 changes: 10 additions & 0 deletions e2e/transform/transform-testrunner/package.json
@@ -0,0 +1,10 @@
{
"jest": {
"rootDir": "./",
"testRunner": "<rootDir>/test-runner.ts"
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0"
}
}
34 changes: 34 additions & 0 deletions e2e/transform/transform-testrunner/test-runner.ts
@@ -0,0 +1,34 @@
/**
* 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 {JestEnvironment} from '@jest/environment';
import Runtime from 'jest-runtime';
import {TestResult, createEmptyTestResult} from '@jest/test-result';

export default async function testRunner(
globalConfig: Config.GlobalConfig,
config: Config.ProjectConfig,
environment: JestEnvironment,
runtime: Runtime,
testPath: string
): Promise<TestResult> {
return {
...createEmptyTestResult(),
numPassingTests: 1,
testFilePath: testPath,
testResults: [
{
ancestorTitles: [],
duration: 2,
failureMessages: [],
fullName: 'sample test',
status: 'passed',
title: 'sample test',
},
],
} as TestResult;
}
6 changes: 6 additions & 0 deletions e2e/transform/transform-testrunner/tsconfig.json
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
}
}
1 change: 1 addition & 0 deletions packages/jest-runner/package.json
Expand Up @@ -13,6 +13,7 @@
"@jest/console": "^24.7.1",
"@jest/environment": "^24.9.0",
"@jest/test-result": "^24.9.0",
"@jest/transform": "^24.9.0",
"@jest/types": "^24.9.0",
"chalk": "^2.4.2",
"exit": "^0.1.2",
Expand Down
13 changes: 9 additions & 4 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -17,6 +17,7 @@ import {
getConsoleOutput,
} from '@jest/console';
import {JestEnvironment} from '@jest/environment';
import {ScriptTransformer} from '@jest/transform';
import RuntimeClass = require('jest-runtime');
import * as fs from 'graceful-fs';
import {ErrorWithStack, interopRequireDefault, setGlobal} from 'jest-util';
Expand Down Expand Up @@ -102,13 +103,17 @@ async function runTestInternal(
});
}

const transformer = new ScriptTransformer(config);
const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
require(testEnvironment),
).default;
const testFramework: TestFramework =
process.env.JEST_CIRCUS === '1'
? require('jest-circus/runner') // eslint-disable-line import/no-extraneous-dependencies
: require(config.testRunner);
const testFramework: TestFramework = interopRequireDefault(
transformer.requireAndTranspileModule(
process.env.JEST_CIRCUS === '1'
? 'jest-circus/runner'
: config.testRunner,
),
).default;
const Runtime: typeof RuntimeClass = config.moduleLoader
? require(config.moduleLoader)
: require('jest-runtime');
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/tsconfig.json
Expand Up @@ -15,6 +15,7 @@
{"path": "../jest-resolve"},
{"path": "../jest-runtime"},
{"path": "../jest-test-result"},
{"path": "../jest-transform"},
{"path": "../jest-types"},
{"path": "../jest-worker"},
{"path": "../jest-util"}
Expand Down

0 comments on commit 7caaa64

Please sign in to comment.