diff --git a/CHANGELOG.md b/CHANGELOG.md index ffa3830e0bfa..6e3953fa287b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - `[jest-worker]` Add node worker-thread support to jest-worker ([#7408](https://github.com/facebook/jest/pull/7408)) - `[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)) - `[jest-config]` Allow % based configuration of `--max-workers` ([#7494](https://github.com/facebook/jest/pull/7494)) +- `[jest-runner]` Instantiate the test environment class with the current `testPath` ([#7442](https://github.com/facebook/jest/pull/7442)) ### Fixes diff --git a/docs/Configuration.md b/docs/Configuration.md index 0c2a96832dcf..40ce1cad37db 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -771,13 +771,14 @@ Example: const NodeEnvironment = require('jest-environment-node'); class CustomEnvironment extends NodeEnvironment { - constructor(config) { - super(config); + constructor(config, context) { + super(config, context); + this.testPath = context.testPath; } async setup() { await super.setup(); - await someSetupTasks(); + await someSetupTasks(this.testPath); this.global.someGlobalObject = createGlobalObject(); } diff --git a/e2e/__tests__/test_environment_async.test.js b/e2e/__tests__/test_environment_async.test.js index 32be3bdcf488..8eea17cfca7a 100644 --- a/e2e/__tests__/test_environment_async.test.js +++ b/e2e/__tests__/test_environment_async.test.js @@ -9,6 +9,7 @@ 'use strict'; import fs from 'fs'; +import path from 'path'; import os from 'os'; import runJest from '../runJest'; import {cleanup} from '../Utils'; @@ -19,8 +20,18 @@ beforeEach(() => cleanup(DIR)); afterAll(() => cleanup(DIR)); it('triggers setup/teardown hooks', () => { + const testDir = path.resolve( + __dirname, + '..', + 'test-environment-async', + '__tests__', + ); + const testFile = path.join(testDir, 'custom.test.js'); + const result = runJest('test-environment-async'); expect(result.status).toBe(0); + expect(result.stdout).toContain(`TestEnvironment.setup: ${testFile}`); + const teardown = fs.readFileSync(DIR + '/teardown', 'utf8'); expect(teardown).toBe('teardown'); }); diff --git a/e2e/test-environment-async/TestEnvironment.js b/e2e/test-environment-async/TestEnvironment.js index e99ea01bdd4e..5feaa1a81c0b 100644 --- a/e2e/test-environment-async/TestEnvironment.js +++ b/e2e/test-environment-async/TestEnvironment.js @@ -10,11 +10,13 @@ const JSDOMEnvironment = require('jest-environment-jsdom'); const DIR = os.tmpdir() + '/jest-test-environment'; class TestEnvironment extends JSDOMEnvironment { - constructor(config) { - super(config); + constructor(config, context) { + super(config, context); + this.context = context; } setup() { + console.info('TestEnvironment.setup:', this.context.testPath); return super.setup().then(() => { this.global.setup = 'setup'; }); diff --git a/packages/jest-environment-jsdom/src/index.js b/packages/jest-environment-jsdom/src/index.js index df345faabf4d..8e79912bf11b 100644 --- a/packages/jest-environment-jsdom/src/index.js +++ b/packages/jest-environment-jsdom/src/index.js @@ -8,7 +8,7 @@ import type {Script} from 'vm'; import type {ProjectConfig} from 'types/Config'; -import type {EnvironmentOptions} from 'types/Environment'; +import type {EnvironmentContext} from 'types/Environment'; import type {Global} from 'types/Global'; import type {ModuleMocker} from 'jest-mock'; @@ -23,7 +23,7 @@ class JSDOMEnvironment { errorEventListener: ?Function; moduleMocker: ?ModuleMocker; - constructor(config: ProjectConfig, options?: EnvironmentOptions = {}) { + constructor(config: ProjectConfig, options?: EnvironmentContext = {}) { this.dom = new JSDOM( '', Object.assign( diff --git a/packages/jest-runner/src/runTest.js b/packages/jest-runner/src/runTest.js index 8908a54b58ec..f1c2020d8bac 100644 --- a/packages/jest-runner/src/runTest.js +++ b/packages/jest-runner/src/runTest.js @@ -101,7 +101,10 @@ async function runTestInternal( testConsole = new BufferedConsole(() => runtime && runtime.getSourceMaps()); } - const environment = new TestEnvironment(config, {console: testConsole}); + const environment = new TestEnvironment(config, { + console: testConsole, + testPath: path, + }); const leakDetector = config.detectLeaks ? new LeakDetector(environment) : null; diff --git a/types/Environment.js b/types/Environment.js index 07128ef7da27..237b47077532 100644 --- a/types/Environment.js +++ b/types/Environment.js @@ -12,12 +12,13 @@ import type {Global} from './Global'; import type {Script} from 'vm'; import type {ModuleMocker} from 'jest-mock'; -export type EnvironmentOptions = { +export type EnvironmentContext = { console?: Object, + testPath?: string, }; declare class $JestEnvironment { - constructor(config: ProjectConfig, options?: EnvironmentOptions): void; + constructor(config: ProjectConfig, context?: EnvironmentContext): void; runScript(script: Script): any; global: Global; fakeTimers: {