Skip to content

Commit

Permalink
feat: make context argument to test environments mandatory (#12469)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 23, 2022
1 parent f975098 commit c7da417
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 51 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,8 +11,10 @@
- `[jest-environment-jsdom]` [**BREAKING**] Upgrade jsdom to 19.0.0 ([#12290](https://github.com/facebook/jest/pull/12290))
- `[jest-environment-jsdom]` [**BREAKING**] Add default `browser` condition to `exportConditions` for `jsdom` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[jest-environment-jsdom]` [**BREAKING**] Pass global config to Jest environment constructor for `jsdom` environment ([#12461](https://github.com/facebook/jest/pull/12461))
- `[jest-environment-jsdom]` [**BREAKING**] Second argument `context` to constructor is mandatory ([#12469](https://github.com/facebook/jest/pull/12469))
- `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924))
- `[jest-environment-node]` [**BREAKING**] Pass global config to Jest environment constructor for `node` environment ([#12461](https://github.com/facebook/jest/pull/12461))
- `[jest-environment-node]` [**BREAKING**] Second argument `context` to constructor is mandatory ([#12469](https://github.com/facebook/jest/pull/12469))
- `[@jest/expect]` New module which extends `expect` with `jest-snapshot` matchers ([#12404](https://github.com/facebook/jest/pull/12404), [#12410](https://github.com/facebook/jest/pull/12410), [#12418](https://github.com/facebook/jest/pull/12418))
- `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323))
- `[jest-mock]` Improve `isMockFunction` to infer types of passed function ([#12442](https://github.com/facebook/jest/pull/12442))
Expand Down
Expand Up @@ -10,10 +10,13 @@ import JSDomEnvironment from '../';

describe('JSDomEnvironment', () => {
it('should configure setTimeout/setInterval to use the browser api', () => {
const env = new JSDomEnvironment({
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
});
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
},
{console, docblockPragmas: {}, testPath: __filename},
);

env.fakeTimers!.useFakeTimers();

Expand All @@ -26,23 +29,29 @@ describe('JSDomEnvironment', () => {
});

it('has modern fake timers implementation', () => {
const env = new JSDomEnvironment({
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
});
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
},
{console, docblockPragmas: {}, testPath: __filename},
);

expect(env.fakeTimersModern).toBeDefined();
});

it('should respect userAgent option', () => {
const env = new JSDomEnvironment({
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig({
testEnvironmentOptions: {
userAgent: 'foo',
},
}),
});
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig({
testEnvironmentOptions: {
userAgent: 'foo',
},
}),
},
{console, docblockPragmas: {}, testPath: __filename},
);

expect(env.dom.window.navigator.userAgent).toEqual('foo');
});
Expand All @@ -60,10 +69,13 @@ describe('JSDomEnvironment', () => {
* will be called, so please make sure the global.document is still available at this point.
*/
it('should not set the global.document to null too early', () => {
const env = new JSDomEnvironment({
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
});
const env = new JSDomEnvironment(
{
globalConfig: makeGlobalConfig(),
projectConfig: makeProjectConfig(),
},
{console, docblockPragmas: {}, testPath: __filename},
);

const originalCloseFn = env.global.close.bind(env.global);
env.global.close = () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/jest-environment-jsdom/src/index.ts
Expand Up @@ -34,7 +34,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
private errorEventListener: ((event: Event & {error: Error}) => void) | null;
moduleMocker: ModuleMocker | null;

constructor(config: JestEnvironmentConfig, options?: EnvironmentContext) {
constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
const {projectConfig} = config;
this.dom = new JSDOM(
typeof projectConfig.testEnvironmentOptions.html === 'string'
Expand All @@ -50,9 +50,7 @@ export default class JSDOMEnvironment implements JestEnvironment<number> {
: undefined,
runScripts: 'dangerously',
url: projectConfig.testURL,
virtualConsole: new VirtualConsole().sendTo(
options?.console || console,
),
virtualConsole: new VirtualConsole().sendTo(context.console),
...projectConfig.testEnvironmentOptions,
},
);
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-environment-node/src/index.ts
Expand Up @@ -6,7 +6,11 @@
*/

import {Context, createContext, runInContext} from 'vm';
import type {JestEnvironment, JestEnvironmentConfig} from '@jest/environment';
import type {
EnvironmentContext,
JestEnvironment,
JestEnvironmentConfig,
} from '@jest/environment';
import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers';
import type {Global} from '@jest/types';
import {ModuleMocker} from 'jest-mock';
Expand All @@ -25,7 +29,8 @@ export default class NodeEnvironment implements JestEnvironment<Timer> {
global: Global.Global;
moduleMocker: ModuleMocker | null;

constructor(config: JestEnvironmentConfig) {
// while `context` is unused, it should always be passed
constructor(config: JestEnvironmentConfig, _context: EnvironmentContext) {
const {projectConfig} = config;
this.context = createContext();
const global = (this.global = runInContext(
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-environment/src/index.ts
Expand Up @@ -34,7 +34,7 @@ export interface JestEnvironmentConfig {
}

export declare class JestEnvironment<Timer = unknown> {
constructor(config: JestEnvironmentConfig, context?: EnvironmentContext);
constructor(config: JestEnvironmentConfig, context: EnvironmentContext);
global: Global.Global;
fakeTimers: LegacyFakeTimers<Timer> | null;
fakeTimersModern: ModernFakeTimers | null;
Expand Down
28 changes: 10 additions & 18 deletions packages/jest-repl/src/cli/runtime-cli.ts
Expand Up @@ -80,25 +80,17 @@ export async function run(
projectConfig.testEnvironment,
);

const environment = new Environment({
globalConfig,
projectConfig,
});
setGlobal(
environment.global as unknown as typeof globalThis,
'console',
new CustomConsole(process.stdout, process.stderr),
);
setGlobal(
environment.global as unknown as typeof globalThis,
'jestProjectConfig',
projectConfig,
);
setGlobal(
environment.global as unknown as typeof globalThis,
'jestGlobalConfig',
globalConfig,
const customConsole = new CustomConsole(process.stdout, process.stderr);
const environment = new Environment(
{
globalConfig,
projectConfig,
},
{console: customConsole, docblockPragmas: {}, testPath: filePath},
);
setGlobal(environment.global, 'console', customConsole);
setGlobal(environment.global, 'jestProjectConfig', projectConfig);
setGlobal(environment.global, 'jestGlobalConfig', globalConfig);

const runtime = new Runtime(
projectConfig,
Expand Down
6 changes: 1 addition & 5 deletions packages/jest-runner/src/runTest.ts
Expand Up @@ -162,11 +162,7 @@ async function runTestInternal(
? new LeakDetector(environment)
: null;

setGlobal(
environment.global as unknown as typeof globalThis,
'console',
testConsole,
);
setGlobal(environment.global, 'console', testConsole);

const runtime = new Runtime(
projectConfig,
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-util/src/setGlobal.ts
Expand Up @@ -5,8 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import type {Global} from '@jest/types';

export default function setGlobal(
globalToMutate: typeof globalThis,
globalToMutate: typeof globalThis | Global.Global,
key: string,
value: unknown,
): void {
Expand Down

0 comments on commit c7da417

Please sign in to comment.