Skip to content

Commit

Permalink
Support transforming testRunner with preset/transform (#8823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Nov 15, 2020
1 parent 68d68f5 commit 0a0ac60
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@
- `[jest-snapshot]`: [**BREAKING**] Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
- `[jest-repl, jest-runner]` [**BREAKING**] Run transforms over environment ([#8751](https://github.com/facebook/jest/pull/8751))
- `[jest-runner]` [**BREAKING**] Run transforms over `runnner` ([#8823](https://github.com/facebook/jest/pull/8823))
- `[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 @@ -227,3 +227,14 @@ describe('transform-runner', () => {
expect(json.numPassedTests).toBe(1);
});
});

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/async-regenerator/package.json
Expand Up @@ -6,6 +6,10 @@
"@babel/polyfill": "^7.2.5"
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"jest-circus",
"jest-jasmine2"
]
}
}
4 changes: 3 additions & 1 deletion e2e/babel-plugin-jest-hoist/package.json
Expand Up @@ -9,7 +9,9 @@
"automock": true,
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
"jest-circus",
"jest-environment-node",
"jest-jasmine2"
]
}
}
5 changes: 4 additions & 1 deletion e2e/coverage-transform-instrumented/package.json
Expand Up @@ -7,7 +7,10 @@
"testRegex": "/__tests__/.*\\.(js)$",
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
"jest-circus",
"jest-each",
"jest-environment-node/",
"jest-jasmine2"
],
"moduleFileExtensions": [
"js"
Expand Down
5 changes: 4 additions & 1 deletion e2e/transform-linked-modules/package.json
Expand Up @@ -5,7 +5,10 @@
"/node_modules/",
"<rootDir>/__tests__",
"<rootDir>/ignored/",
"/jest-environment-node/"
"jest-circus",
"jest-each",
"jest-environment-node",
"jest-jasmine2"
],
"transform": {
"\\.js$": "<rootDir>/preprocessor.js"
Expand Down
11 changes: 10 additions & 1 deletion e2e/transform/babel-jest-ignored/babel.config.js
Expand Up @@ -5,4 +5,13 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = {only: ['blablabla', /jest-environment-node/]};
module.exports = {
only: [
'blablabla',
/jest-circus/,
/jest-each/,
/jest-environment-node/,
/jest-jasmine2/,
/jest-source-map/,
],
};
4 changes: 3 additions & 1 deletion e2e/transform/multiple-transformers/package.json
Expand Up @@ -7,7 +7,9 @@
},
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
"jest-circus",
"jest-environment-node",
"jest-jasmine2"
]
},
"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);
});
13 changes: 13 additions & 0 deletions e2e/transform/transform-testrunner/babel.config.js
@@ -0,0 +1,13 @@
/**
* 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-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
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"
}
}
36 changes: 36 additions & 0 deletions e2e/transform/transform-testrunner/test-runner.ts
@@ -0,0 +1,36 @@
/**
* 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 {JestEnvironment} from '@jest/environment';
import {TestResult, createEmptyTestResult} from '@jest/test-result';
import {Config} from '@jest/types';
import Runtime from 'jest-runtime';

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',
location: null,
numPassingAsserts: 1,
status: 'passed',
title: 'sample test',
},
],
};
}
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 @@ -17,6 +17,7 @@
"@jest/console": "^26.6.2",
"@jest/environment": "^26.6.2",
"@jest/test-result": "^26.6.2",
"@jest/transform": "^26.6.2",
"@jest/types": "^26.6.2",
"@types/node": "*",
"chalk": "^4.0.0",
Expand Down
9 changes: 5 additions & 4 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -108,10 +108,11 @@ async function runTestInternal(
transformer.requireAndTranspileModule(testEnvironment),
).default;
const testFramework: TestFramework = interopRequireDefault(
process.env.JEST_CIRCUS === '1'
? // eslint-disable-next-line import/no-extraneous-dependencies
require('jest-circus/runner')
: require(config.testRunner),
transformer.requireAndTranspileModule(
process.env.JEST_CIRCUS === '1'
? 'jest-circus/runner'
: config.testRunner,
),
).default;
const Runtime: typeof RuntimeClass = interopRequireDefault(
config.moduleLoader
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Expand Up @@ -12126,6 +12126,7 @@ fsevents@~2.1.2:
"@jest/console": ^26.6.2
"@jest/environment": ^26.6.2
"@jest/test-result": ^26.6.2
"@jest/transform": ^26.6.2
"@jest/types": ^26.6.2
"@types/exit": ^0.1.30
"@types/graceful-fs": ^4.1.2
Expand Down

0 comments on commit 0a0ac60

Please sign in to comment.