Skip to content

Commit 9208ac6

Browse files
avalySimenB
authored andcommitted
Instantiate test environment class with current test path (jestjs#7442)
1 parent c7caa7b commit 9208ac6

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- `[jest-worker]` Add node worker-thread support to jest-worker ([#7408](https://github.com/facebook/jest/pull/7408))
4040
- `[jest-config]` Allow `bail` setting to be configured with a number allowing tests to abort after `n` of failures ([#7335](https://github.com/facebook/jest/pull/7335))
4141
- `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494))
42+
- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442))
4243

4344
### Fixes
4445

docs/Configuration.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -789,13 +789,14 @@ Example:
789789
const NodeEnvironment = require('jest-environment-node');
790790

791791
class CustomEnvironment extends NodeEnvironment {
792-
constructor(config) {
793-
super(config);
792+
constructor(config, context) {
793+
super(config, context);
794+
this.testPath = context.testPath;
794795
}
795796

796797
async setup() {
797798
await super.setup();
798-
await someSetupTasks();
799+
await someSetupTasks(this.testPath);
799800
this.global.someGlobalObject = createGlobalObject();
800801
}
801802

e2e/__tests__/test_environment_async.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
import fs from 'fs';
12+
import path from 'path';
1213
import os from 'os';
1314
import runJest from '../runJest';
1415
import {cleanup} from '../Utils';
@@ -19,8 +20,18 @@ beforeEach(() => cleanup(DIR));
1920
afterAll(() => cleanup(DIR));
2021

2122
it('triggers setup/teardown hooks', () => {
23+
const testDir = path.resolve(
24+
__dirname,
25+
'..',
26+
'test-environment-async',
27+
'__tests__',
28+
);
29+
const testFile = path.join(testDir, 'custom.test.js');
30+
2231
const result = runJest('test-environment-async');
2332
expect(result.status).toBe(0);
33+
expect(result.stdout).toContain(`TestEnvironment.setup: ${testFile}`);
34+
2435
const teardown = fs.readFileSync(DIR + '/teardown', 'utf8');
2536
expect(teardown).toBe('teardown');
2637
});

e2e/test-environment-async/TestEnvironment.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const JSDOMEnvironment = require('jest-environment-jsdom');
1010
const DIR = os.tmpdir() + '/jest-test-environment';
1111

1212
class TestEnvironment extends JSDOMEnvironment {
13-
constructor(config) {
14-
super(config);
13+
constructor(config, context) {
14+
super(config, context);
15+
this.context = context;
1516
}
1617

1718
setup() {
19+
console.info('TestEnvironment.setup:', this.context.testPath);
1820
return super.setup().then(() => {
1921
this.global.setup = 'setup';
2022
});

packages/jest-environment-jsdom/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import type {Script} from 'vm';
1010
import type {ProjectConfig} from 'types/Config';
11-
import type {EnvironmentOptions} from 'types/Environment';
11+
import type {EnvironmentContext} from 'types/Environment';
1212
import type {Global} from 'types/Global';
1313
import type {ModuleMocker} from 'jest-mock';
1414

@@ -23,7 +23,7 @@ class JSDOMEnvironment {
2323
errorEventListener: ?Function;
2424
moduleMocker: ?ModuleMocker;
2525

26-
constructor(config: ProjectConfig, options?: EnvironmentOptions = {}) {
26+
constructor(config: ProjectConfig, options?: EnvironmentContext = {}) {
2727
this.dom = new JSDOM(
2828
'<!DOCTYPE html>',
2929
Object.assign(

packages/jest-runner/src/runTest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ async function runTestInternal(
102102
testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps());
103103
}
104104

105-
const environment = new TestEnvironment(config, {console: testConsole});
105+
const environment = new TestEnvironment(config, {
106+
console: testConsole,
107+
testPath: path,
108+
});
106109
const leakDetector = config.detectLeaks
107110
? new LeakDetector(environment)
108111
: null;

types/Environment.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import type {Global} from './Global';
1212
import type {Script} from 'vm';
1313
import type {ModuleMocker} from 'jest-mock';
1414

15-
export type EnvironmentOptions = {
15+
export type EnvironmentContext = {
1616
console?: Object,
17+
testPath?: string,
1718
};
1819

1920
declare class $JestEnvironment {
20-
constructor(config: ProjectConfig, options?: EnvironmentOptions): void;
21+
constructor(config: ProjectConfig, context?: EnvironmentContext): void;
2122
runScript(script: Script): any;
2223
global: Global;
2324
fakeTimers: {

0 commit comments

Comments
 (0)