Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v13.7.3
Choose a base ref
...
head repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 16396f9d1f114ad70c926f56da40a31382aeabcb
Choose a head ref
  • 7 commits
  • 4 files changed
  • 2 contributors

Commits on Mar 11, 2024

  1. feat(jest-environment): Support configuration of happy-dom with testE…

    …nvironmentOptions
    Codex- committed Mar 11, 2024
    Copy the full SHA
    ff9b0ed View commit details
  2. chore: [#1272] Fixes problem with the release

    capricorn86 committed Mar 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    e68fdbc View commit details
  3. Merge pull request #1295 from capricorn86/fix-release-3

    chore: [#1272] Fixes problem with the release
    capricorn86 authored Mar 11, 2024
    Copy the full SHA
    3a01d71 View commit details
  4. Merge branch 'master' into jest-environment_support_full_config

    capricorn86 authored Mar 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    721b9d6 View commit details
  5. chore: [#1287] Simplifies solution, by sending in testEnvironmentOpti…

    …ons directly to the Window constructor
    capricorn86 committed Mar 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c80bb03 View commit details
  6. chore: [#1287] Simplifies solution, by sending in testEnvironmentOpti…

    …ons directly to the Window constructor
    capricorn86 committed Mar 11, 2024
    Copy the full SHA
    94e214c View commit details
  7. Merge pull request #1287 from Codex-/jest-environment_support_full_co…

    …nfig
    
    feat(jest-environment): Support configuration of happy-dom with testEnvironmentOptions
    capricorn86 authored Mar 11, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    16396f9 View commit details
Showing with 59 additions and 33 deletions.
  1. +4 −1 .github/workflows/pull_request.yml
  2. +4 −5 .github/workflows/release.yml
  3. +24 −4 packages/jest-environment/README.md
  4. +27 −23 packages/jest-environment/src/index.ts
5 changes: 4 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Pull request
on: pull_request

on:
pull_request

jobs:
build:
runs-on: ubuntu-latest
9 changes: 4 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Release

on:
pull_request:
types: [closed]
push:
branches:
- master

jobs:
check-next-version:
@@ -16,8 +17,6 @@ jobs:
next_version: ${{ steps.versionCheck.outputs.next_version }}
current_version: ${{ steps.versionCheck.outputs.current_version }}

if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'master'

steps:
- uses: actions/checkout@v4
with:
@@ -150,7 +149,7 @@ jobs:
run: npm ci --ignore-scripts

- name: Generate release notes
run: node ./node_modules/.bin/happy-release-notes --author=githubUsername --authorUsername=${{ github.event.pull_request.user.login }} > ./RELEASE_NOTES.md
run: node ./node_modules/.bin/happy-release-notes --author=githubUsername > ./RELEASE_NOTES.md

- name: Create release
id: create_release
28 changes: 24 additions & 4 deletions packages/jest-environment/README.md
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
}
```

@@ -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"
}
}
}
}
```

50 changes: 27 additions & 23 deletions packages/jest-environment/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

@@ -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.
@@ -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;
@@ -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.
@@ -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),