Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow test environment to run with preset/transform #8751

Merged
merged 4 commits into from
Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
- `[jest-runner]` [**BREAKING**] set exit code to 1 if test logs after teardown ([#10728](https://github.com/facebook/jest/pull/10728))
- `[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))

### Fixes

Expand Down
11 changes: 11 additions & 0 deletions e2e/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,14 @@ describe('transformer caching', () => {
expect(loggedFiles).toHaveLength(2);
});
});

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

it('should transform the environment', () => {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
const {json, stderr} = runWithJson(dir, ['--no-cache']);
expect(stderr).toMatch(/PASS/);
expect(json.success).toBe(true);
expect(json.numPassedTests).toBe(1);
});
});
5 changes: 4 additions & 1 deletion e2e/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
},
"jest": {
"automock": true,
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
]
}
}
3 changes: 3 additions & 0 deletions e2e/coverage-transform-instrumented/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
},
"testRegex": "/__tests__/.*\\.(js)$",
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
],
"moduleFileExtensions": [
"js"
]
Expand Down
3 changes: 2 additions & 1 deletion e2e/transform-linked-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"transformIgnorePatterns": [
"/node_modules/",
"<rootDir>/__tests__",
"<rootDir>/ignored/"
"<rootDir>/ignored/",
"/jest-environment-node/"
jeysal marked this conversation as resolved.
Show resolved Hide resolved
],
"transform": {
"\\.js$": "<rootDir>/preprocessor.js"
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform/babel-jest-ignored/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* LICENSE file in the root directory of this source tree.
*/

module.exports = {only: ['blablabla']};
module.exports = {only: ['blablabla', /jest-environment-node/]};
5 changes: 4 additions & 1 deletion e2e/transform/multiple-transformers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"\\.js$": "<rootDir>/jsPreprocessor.js",
"\\.svg$": "<rootDir>/filePreprocessor.js"
},
"testEnvironment": "node"
"testEnvironment": "node",
"transformIgnorePatterns": [
"/jest-environment-node/"
]
},
"dependencies": {
"@babel/core": "^7.0.0",
Expand Down
11 changes: 11 additions & 0 deletions e2e/transform/transform-environment/__tests__/add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* 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', () => {
// eslint-disable-next-line no-undef
expect(one + 1).toBe(2);
});
13 changes: 13 additions & 0 deletions e2e/transform/transform-environment/babel.config.js
Original file line number Diff line number Diff line change
@@ -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',
],
};
16 changes: 16 additions & 0 deletions e2e/transform/transform-environment/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* 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 type {Config} from '@jest/types';
import NodeEnvironment from 'jest-environment-node';

export default class CustomEnvironment extends NodeEnvironment {
constructor(config: Config.ProjectConfig) {
super(config);
this.global.one = 1;
SimenB marked this conversation as resolved.
Show resolved Hide resolved
}
}
14 changes: 14 additions & 0 deletions e2e/transform/transform-environment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jest": {
"testEnvironment": "<rootDir>/environment.ts",
"transformIgnorePatterns": [
"jest-environment-node"
],
"rootDir": "./"
},
"dependencies": {
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.0.0",
"jest-environment-node": "^26.6.2"
}
}
6 changes: 6 additions & 0 deletions e2e/transform/transform-environment/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
}
}
5 changes: 4 additions & 1 deletion e2e/v8-coverage/empty-sourcemap/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
*/

module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript'],
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
};
8 changes: 6 additions & 2 deletions packages/jest-repl/src/cli/runtime-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import chalk = require('chalk');
import yargs = require('yargs');
import {CustomConsole} from '@jest/console';
import type {JestEnvironment} from '@jest/environment';
import {ScriptTransformer} from '@jest/transform';
import type {Config} from '@jest/types';
import {deprecationEntries, readConfig} from 'jest-config';
import Runtime from 'jest-runtime';
import {setGlobal, tryRealpath} from 'jest-util';
import {interopRequireDefault, setGlobal, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import * as args from './args';
import {VERSION} from './version';
Expand Down Expand Up @@ -73,7 +74,10 @@ export async function run(
watchman: globalConfig.watchman,
});

const Environment: typeof JestEnvironment = require(config.testEnvironment);
const transformer = new ScriptTransformer(config);
const Environment: typeof JestEnvironment = interopRequireDefault(
transformer.requireAndTranspileModule(config.testEnvironment),
).default;
const environment = new Environment(config);
setGlobal(
environment.global,
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-runner/src/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@jest/console';
import type {JestEnvironment} from '@jest/environment';
import type {TestResult} from '@jest/test-result';
import {ScriptTransformer} from '@jest/transform';
import type {Config} from '@jest/types';
import {getTestEnvironment} from 'jest-config';
import * as docblock from 'jest-docblock';
Expand Down Expand Up @@ -102,8 +103,9 @@ async function runTestInternal(
});
}

const transformer = new ScriptTransformer(config);
const TestEnvironment: typeof JestEnvironment = interopRequireDefault(
require(testEnvironment),
Mark1626 marked this conversation as resolved.
Show resolved Hide resolved
transformer.requireAndTranspileModule(testEnvironment),
).default;
const testFramework: TestFramework = interopRequireDefault(
process.env.JEST_CIRCUS === '1'
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runner/tsconfig.json
Original file line number Diff line number Diff line change
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