Skip to content

Commit

Permalink
chore: bump happy-dom to 15.11.0 (#111)
Browse files Browse the repository at this point in the history
* chore: bump happy-dom to 15.11.0

* Create great-tips-punch.md
  • Loading branch information
layershifter authored Nov 11, 2024
1 parent fcfc357 commit 81bcb65
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-tips-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wyw-in-js/transform": patch
---

chore: bump happy-dom to 15.11.0
2 changes: 1 addition & 1 deletion packages/transform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
25 changes: 25 additions & 0 deletions packages/transform/src/__tests__/vm/createVmContext.test.ts
Original file line number Diff line number Diff line change
@@ -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),
});
}
);
});
29 changes: 22 additions & 7 deletions packages/transform/src/vm/createVmContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vm.Context>
): Partial<vm.Context> {
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;
Expand All @@ -57,7 +72,7 @@ function createHappyDOMWindow() {

return {
teardown: () => {
win.happyDOM.cancelAsync();
win.happyDOM.abort();
},
window: win,
};
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81bcb65

Please sign in to comment.