Skip to content

Commit

Permalink
Instantiate test environment class with current test path
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Dec 12, 2018
1 parent df78255 commit 7c18f46
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
11 changes: 11 additions & 0 deletions e2e/__tests__/test_environment_async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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');
});
6 changes: 4 additions & 2 deletions e2e/test-environment-async/TestEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
});
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-environment-jsdom/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -23,7 +23,7 @@ class JSDOMEnvironment {
errorEventListener: ?Function;
moduleMocker: ?ModuleMocker;

constructor(config: ProjectConfig, options?: EnvironmentOptions = {}) {
constructor(config: ProjectConfig, options?: EnvironmentContext = {}) {
this.dom = new JSDOM(
'<!DOCTYPE html>',
Object.assign(
Expand Down
5 changes: 4 additions & 1 deletion packages/jest-runner/src/runTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions types/Environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 7c18f46

Please sign in to comment.