From 81bcb6558f83fd50d708d7b0eb3ae00ebc7cada3 Mon Sep 17 00:00:00 2001 From: Oleksandr Fediashov Date: Mon, 11 Nov 2024 14:22:11 +0100 Subject: [PATCH] chore: bump happy-dom to 15.11.0 (#111) * chore: bump happy-dom to 15.11.0 * Create great-tips-punch.md --- .changeset/great-tips-punch.md | 5 ++++ packages/transform/package.json | 2 +- .../src/__tests__/vm/createVmContext.test.ts | 25 ++++++++++++++++ packages/transform/src/vm/createVmContext.ts | 29 ++++++++++++++----- pnpm-lock.yaml | 12 ++++---- 5 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 .changeset/great-tips-punch.md create mode 100644 packages/transform/src/__tests__/vm/createVmContext.test.ts diff --git a/.changeset/great-tips-punch.md b/.changeset/great-tips-punch.md new file mode 100644 index 00000000..c0087be9 --- /dev/null +++ b/.changeset/great-tips-punch.md @@ -0,0 +1,5 @@ +--- +"@wyw-in-js/transform": patch +--- + +chore: bump happy-dom to 15.11.0 diff --git a/packages/transform/package.json b/packages/transform/package.json index 04ffa465..c4556c23 100644 --- a/packages/transform/package.json +++ b/packages/transform/package.json @@ -13,7 +13,7 @@ "@wyw-in-js/shared": "workspace:*", "babel-merge": "^3.0.0", "cosmiconfig": "^8.0.0", - "happy-dom": "^14.12.3", + "happy-dom": "^15.11.0", "source-map": "^0.7.4", "stylis": "^4.3.0", "ts-invariant": "^0.10.3" diff --git a/packages/transform/src/__tests__/vm/createVmContext.test.ts b/packages/transform/src/__tests__/vm/createVmContext.test.ts new file mode 100644 index 00000000..f756a0ac --- /dev/null +++ b/packages/transform/src/__tests__/vm/createVmContext.test.ts @@ -0,0 +1,25 @@ +import { createVmContext } from '../../vm/createVmContext'; + +describe('createVmContext', () => { + it.each([{ happyDOM: true }, { happyDOM: false }])( + 'should create a VM context with "happy-dom" (%p)', + (features) => { + const vmContext = createVmContext('filename', features, {}); + + expect(vmContext).toMatchObject({ + context: expect.any(Object), + teardown: expect.any(Function), + }); + + expect(vmContext.context).toMatchObject({ + __filename: 'filename', + + self: vmContext.context, + top: vmContext.context, + + setInterval: expect.any(Function), + setTimeout: expect.any(Function), + }); + } + ); +}); diff --git a/packages/transform/src/vm/createVmContext.ts b/packages/transform/src/vm/createVmContext.ts index 349040fe..75a18c08 100644 --- a/packages/transform/src/vm/createVmContext.ts +++ b/packages/transform/src/vm/createVmContext.ts @@ -21,19 +21,34 @@ function createWindow(): Window { return win; } +/** + * `happy-dom` already has required references, so we don't need to set them. + */ +function setReferencePropertyIfNotPresent( + context: vm.Context, + key: string +): void { + if (context[key] === context) { + return; + } + + context[key] = context; +} + function createBaseContext( win: Window | undefined, additionalContext: Partial ): Partial { const baseContext: vm.Context = win ?? {}; - baseContext.document = win?.document; - baseContext.window = win; - baseContext.self = win; - baseContext.top = win; - baseContext.parent = win; - baseContext.global = win; + setReferencePropertyIfNotPresent(baseContext, 'window'); + setReferencePropertyIfNotPresent(baseContext, 'self'); + setReferencePropertyIfNotPresent(baseContext, 'top'); + setReferencePropertyIfNotPresent(baseContext, 'parent'); + setReferencePropertyIfNotPresent(baseContext, 'global'); + setReferencePropertyIfNotPresent(baseContext, 'process'); + baseContext.document = win?.document; baseContext.process = process; baseContext.clearImmediate = NOOP; @@ -57,7 +72,7 @@ function createHappyDOMWindow() { return { teardown: () => { - win.happyDOM.cancelAsync(); + win.happyDOM.abort(); }, window: win, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b91e159e..5ed41888 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -519,8 +519,8 @@ importers: specifier: ^8.0.0 version: 8.2.0 happy-dom: - specifier: ^14.12.3 - version: 14.12.3 + specifier: ^15.11.0 + version: 15.11.0 source-map: specifier: ^0.7.4 version: 0.7.4 @@ -4624,9 +4624,9 @@ packages: resolution: {integrity: sha512-5v6yZd4JK3eMI3FqqCouswVqwugaA9r4dNZB1wwcmrD02QkV5H0y7XBQW8QwQqEaZY1pM9aqORSORhJRdNK44Q==} engines: {node: '>=6.0'} - happy-dom@14.12.3: - resolution: {integrity: sha512-vsYlEs3E9gLwA1Hp+w3qzu+RUDFf4VTT8cyKqVICoZ2k7WM++Qyd2LwzyTi5bqMJFiIC/vNpTDYuxdreENRK/g==} - engines: {node: '>=16.0.0'} + happy-dom@15.11.0: + resolution: {integrity: sha512-/zyxHbXriYJ8b9Urh43ILk/jd9tC07djURnJuAimJ3tJCOLOzOUp7dEHDwJOZyzROlrrooUhr/0INZIDBj1Bjw==} + engines: {node: '>=18.0.0'} hard-rejection@2.1.0: resolution: {integrity: sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==} @@ -12759,7 +12759,7 @@ snapshots: section-matter: 1.0.0 strip-bom-string: 1.0.0 - happy-dom@14.12.3: + happy-dom@15.11.0: dependencies: entities: 4.5.0 webidl-conversions: 7.0.0