Skip to content

Commit

Permalink
feat: allow test environment to run with preset/transform
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark1626 committed Nov 15, 2020
1 parent e66a0e8 commit a48e7e9
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,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-runtime, 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
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', () => {
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
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
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
Expand Up @@ -4,7 +4,8 @@
"transformIgnorePatterns": [
"/node_modules/",
"<rootDir>/__tests__",
"<rootDir>/ignored/"
"<rootDir>/ignored/",
"/jest-environment-node/"
],
"transform": {
"\\.js$": "<rootDir>/preprocessor.js"
Expand Down
2 changes: 1 addition & 1 deletion e2e/transform/babel-jest-ignored/babel.config.js
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
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
@@ -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);
});
18 changes: 18 additions & 0 deletions e2e/transform/transform-environment/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: 8},
},
],
],
};
16 changes: 16 additions & 0 deletions e2e/transform/transform-environment/environment.ts
@@ -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 {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;
}
}
14 changes: 14 additions & 0 deletions e2e/transform/transform-environment/package.json
@@ -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": "^24.9.0"
}
}
6 changes: 6 additions & 0 deletions e2e/transform/transform-environment/tsconfig.json
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"module": "commonjs",
}
}
4 changes: 3 additions & 1 deletion packages/jest-runner/src/runTest.ts
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),
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
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
8 changes: 6 additions & 2 deletions packages/jest-runtime/src/cli/index.ts
Expand Up @@ -11,9 +11,10 @@ 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 {setGlobal, tryRealpath} from 'jest-util';
import {interopRequireDefault, setGlobal, tryRealpath} from 'jest-util';
import {validateCLIOptions} from 'jest-validate';
import {VERSION} from '../version';
import * as args from './args';
Expand Down Expand Up @@ -74,7 +75,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

0 comments on commit a48e7e9

Please sign in to comment.