Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jest-environment): Support configuration of happy-dom with testEnvironmentOptions #1287

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions packages/jest-environment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,19 @@ Jest uses `node` as test environment by default. In order to tell Jest to use a

```json
{
"jest": {
"testEnvironment": "@happy-dom/jest-environment"
}
"jest": {
"testEnvironment": "@happy-dom/jest-environment",
"testEnvironmentOptions": {
"url": "http://localhost",
"width": 1920,
"height": 1080,
"settings": {
"navigator": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
}
}
}
}
```

Expand All @@ -89,7 +99,17 @@ Jest uses `node` as test environment by default. In order to tell Jest to use a

```json
{
"testEnvironment": "@happy-dom/jest-environment"
"testEnvironment": "@happy-dom/jest-environment",
"testEnvironmentOptions": {
"url": "http://localhost",
"width": 1920,
"height": 1080,
"settings": {
"navigator": {
"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
}
}
}
```

Expand Down
50 changes: 27 additions & 23 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as JestUtil from 'jest-util';
import { ModuleMocker } from 'jest-mock';
import { LegacyFakeTimers, ModernFakeTimers } from '@jest/fake-timers';
import { JestEnvironment, EnvironmentContext } from '@jest/environment';
import { Window, IWindow } from 'happy-dom';
import { Window, IWindow, BrowserErrorCaptureEnum, IOptionalBrowserSettings } from 'happy-dom';
import { Script } from 'vm';
import { Global, Config } from '@jest/types';

Expand All @@ -15,12 +15,9 @@ import { Global, Config } from '@jest/types';
export default class HappyDOMEnvironment implements JestEnvironment {
public fakeTimers: LegacyFakeTimers<number> = null;
public fakeTimersModern: ModernFakeTimers = null;
public window: IWindow = new Window({
console: globalThis.console,
settings: { disableErrorCapturing: true }
});
public global: Global.Global = <Global.Global>(<unknown>this.window);
public moduleMocker: ModuleMocker = new ModuleMocker(<typeof globalThis>(<unknown>this.window));
public window: IWindow;
public global: Global.Global;
public moduleMocker: ModuleMocker;

/**
* Constructor.
Expand All @@ -36,17 +33,8 @@ export default class HappyDOMEnvironment implements JestEnvironment {
| Config.ProjectConfig,
options?: EnvironmentContext
) {
// Node's error-message stack size is limited to 10, but it's pretty useful to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;

// TODO: Remove this ASAP as it currently causes tests to run really slow.
this.global.Buffer = Buffer;

// Needed as Jest is using it
this.window['global'] = this.global;

let globals: Config.ConfigGlobals;
let projectConfig: Config.ProjectConfig;
let globals: Config.ConfigGlobals;
if (isJestConfigVersion29(config)) {
// Jest 29
globals = config.globals;
Expand All @@ -59,6 +47,28 @@ export default class HappyDOMEnvironment implements JestEnvironment {
throw new Error('Unsupported jest version.');
}

// Initialize Window and Global
this.window = new Window({
url: 'http://localhost/',
...projectConfig.testEnvironmentOptions,
console: options.console,
settings: {
...(<IOptionalBrowserSettings>projectConfig.testEnvironmentOptions?.settings),
errorCapture: BrowserErrorCaptureEnum.disabled
}
});
this.global = <Global.Global>(<unknown>this.window);
this.moduleMocker = new ModuleMocker(<typeof globalThis>(<unknown>this.window));

// Node's error-message stack size is limited to 10, but it's pretty useful to see more than that when a test fails.
this.global.Error.stackTraceLimit = 100;

// TODO: Remove this ASAP as it currently causes tests to run really slow.
this.global.Buffer = Buffer;

// Needed as Jest is using it
this.window['global'] = this.global;

JestUtil.installCommonGlobals(<typeof globalThis>(<unknown>this.window), globals);

// For some reason Jest removes the global setImmediate, so we need to add it back.
Expand All @@ -69,12 +79,6 @@ export default class HappyDOMEnvironment implements JestEnvironment {
this.global.window['console'] = options.console;
}

if (projectConfig.testEnvironmentOptions['url']) {
this.window.happyDOM?.setURL(String(projectConfig.testEnvironmentOptions['url']));
} else {
this.window.happyDOM?.setURL('http://localhost/');
}

this.fakeTimers = new LegacyFakeTimers({
config: projectConfig,
global: <typeof globalThis>(<unknown>this.window),
Expand Down
Loading