From b9497189e3d75a960bb1b618c70b35492a6cac20 Mon Sep 17 00:00:00 2001 From: Alex Early Date: Wed, 6 Nov 2024 12:55:57 -0800 Subject: [PATCH] fix: [jest-environment] Allow custom export conditions (#1555) * fix: [jest-environment] Respect custom export conditions * chore: [#1555] Use node as default until we do a major release as it is a breaking change --------- Co-authored-by: Alexander Early Co-authored-by: David Ortner --- package-lock.json | 133 ++++++++---------- packages/jest-environment/package.json | 4 +- packages/jest-environment/src/index.ts | 29 ++++ .../test/react/React.test.tsx | 11 +- 4 files changed, 96 insertions(+), 81 deletions(-) diff --git a/package-lock.json b/package-lock.json index 06083b078..ba04f27ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3529,124 +3529,111 @@ "license": "MIT" }, "node_modules/@vue/compiler-core": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.4.37.tgz", - "integrity": "sha512-ZDDT/KiLKuCRXyzWecNzC5vTcubGz4LECAtfGPENpo0nrmqJHwuWtRLxk/Sb9RAKtR9iFflFycbkjkY+W/PZUQ==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/compiler-core/-/compiler-core-3.5.12.tgz", + "integrity": "sha512-ISyBTRMmMYagUxhcpyEH0hpXRd/KqDU4ymofPgl2XAkY9ZhQ+h0ovEZJIiPop13UmR/54oA2cgMDjgroRelaEw==", "dev": true, "license": "MIT", "dependencies": { - "@babel/parser": "^7.24.7", - "@vue/shared": "3.4.37", - "entities": "^5.0.0", + "@babel/parser": "^7.25.3", + "@vue/shared": "3.5.12", + "entities": "^4.5.0", "estree-walker": "^2.0.2", "source-map-js": "^1.2.0" } }, - "node_modules/@vue/compiler-core/node_modules/entities": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/entities/-/entities-5.0.0.tgz", - "integrity": "sha512-BeJFvFRJddxobhvEdm5GqHzRV/X+ACeuw0/BuuxsCh1EUZcAIz8+kYmBp/LrQuloy6K1f3a0M7+IhmZ7QnkISA==", - "dev": true, - "license": "BSD-2-Clause", - "engines": { - "node": ">=0.12" - }, - "funding": { - "url": "https://github.com/fb55/entities?sponsor=1" - } - }, "node_modules/@vue/compiler-dom": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.4.37.tgz", - "integrity": "sha512-rIiSmL3YrntvgYV84rekAtU/xfogMUJIclUMeIKEtVBFngOL3IeZHhsH3UaFEgB5iFGpj6IW+8YuM/2Up+vVag==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/compiler-dom/-/compiler-dom-3.5.12.tgz", + "integrity": "sha512-9G6PbJ03uwxLHKQ3P42cMTi85lDRvGLB2rSGOiQqtXELat6uI4n8cNz9yjfVHRPIu+MsK6TE418Giruvgptckg==", "dev": true, "license": "MIT", "dependencies": { - "@vue/compiler-core": "3.4.37", - "@vue/shared": "3.4.37" + "@vue/compiler-core": "3.5.12", + "@vue/shared": "3.5.12" } }, "node_modules/@vue/compiler-sfc": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/compiler-sfc/-/compiler-sfc-3.4.37.tgz", - "integrity": "sha512-vCfetdas40Wk9aK/WWf8XcVESffsbNkBQwS5t13Y/PcfqKfIwJX2gF+82th6dOpnpbptNMlMjAny80li7TaCIg==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/compiler-sfc/-/compiler-sfc-3.5.12.tgz", + "integrity": "sha512-2k973OGo2JuAa5+ZlekuQJtitI5CgLMOwgl94BzMCsKZCX/xiqzJYzapl4opFogKHqwJk34vfsaKpfEhd1k5nw==", "dev": true, "license": "MIT", "dependencies": { - "@babel/parser": "^7.24.7", - "@vue/compiler-core": "3.4.37", - "@vue/compiler-dom": "3.4.37", - "@vue/compiler-ssr": "3.4.37", - "@vue/shared": "3.4.37", + "@babel/parser": "^7.25.3", + "@vue/compiler-core": "3.5.12", + "@vue/compiler-dom": "3.5.12", + "@vue/compiler-ssr": "3.5.12", + "@vue/shared": "3.5.12", "estree-walker": "^2.0.2", - "magic-string": "^0.30.10", - "postcss": "^8.4.40", + "magic-string": "^0.30.11", + "postcss": "^8.4.47", "source-map-js": "^1.2.0" } }, "node_modules/@vue/compiler-ssr": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/compiler-ssr/-/compiler-ssr-3.4.37.tgz", - "integrity": "sha512-TyAgYBWrHlFrt4qpdACh8e9Ms6C/AZQ6A6xLJaWrCL8GCX5DxMzxyeFAEMfU/VFr4tylHm+a2NpfJpcd7+20XA==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/compiler-ssr/-/compiler-ssr-3.5.12.tgz", + "integrity": "sha512-eLwc7v6bfGBSM7wZOGPmRavSWzNFF6+PdRhE+VFJhNCgHiF8AM7ccoqcv5kBXA2eWUfigD7byekvf/JsOfKvPA==", "dev": true, "license": "MIT", "dependencies": { - "@vue/compiler-dom": "3.4.37", - "@vue/shared": "3.4.37" + "@vue/compiler-dom": "3.5.12", + "@vue/shared": "3.5.12" } }, "node_modules/@vue/reactivity": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.4.37.tgz", - "integrity": "sha512-UmdKXGx0BZ5kkxPqQr3PK3tElz6adTey4307NzZ3whZu19i5VavYal7u2FfOmAzlcDVgE8+X0HZ2LxLb/jgbYw==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.5.12.tgz", + "integrity": "sha512-UzaN3Da7xnJXdz4Okb/BGbAaomRHc3RdoWqTzlvd9+WBR5m3J39J1fGcHes7U3za0ruYn/iYy/a1euhMEHvTAg==", "dev": true, "license": "MIT", "dependencies": { - "@vue/shared": "3.4.37" + "@vue/shared": "3.5.12" } }, "node_modules/@vue/runtime-core": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/runtime-core/-/runtime-core-3.4.37.tgz", - "integrity": "sha512-MNjrVoLV/sirHZoD7QAilU1Ifs7m/KJv4/84QVbE6nyAZGQNVOa1HGxaOzp9YqCG+GpLt1hNDC4RbH+KtanV7w==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/runtime-core/-/runtime-core-3.5.12.tgz", + "integrity": "sha512-hrMUYV6tpocr3TL3Ad8DqxOdpDe4zuQY4HPY3X/VRh+L2myQO8MFXPAMarIOSGNu0bFAjh1yBkMPXZBqCk62Uw==", "dev": true, "license": "MIT", "dependencies": { - "@vue/reactivity": "3.4.37", - "@vue/shared": "3.4.37" + "@vue/reactivity": "3.5.12", + "@vue/shared": "3.5.12" } }, "node_modules/@vue/runtime-dom": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/runtime-dom/-/runtime-dom-3.4.37.tgz", - "integrity": "sha512-Mg2EwgGZqtwKrqdL/FKMF2NEaOHuH+Ks9TQn3DHKyX//hQTYOun+7Tqp1eo0P4Ds+SjltZshOSRq6VsU0baaNg==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/runtime-dom/-/runtime-dom-3.5.12.tgz", + "integrity": "sha512-q8VFxR9A2MRfBr6/55Q3umyoN7ya836FzRXajPB6/Vvuv0zOPL+qltd9rIMzG/DbRLAIlREmnLsplEF/kotXKA==", "dev": true, "license": "MIT", "dependencies": { - "@vue/reactivity": "3.4.37", - "@vue/runtime-core": "3.4.37", - "@vue/shared": "3.4.37", + "@vue/reactivity": "3.5.12", + "@vue/runtime-core": "3.5.12", + "@vue/shared": "3.5.12", "csstype": "^3.1.3" } }, "node_modules/@vue/server-renderer": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/server-renderer/-/server-renderer-3.4.37.tgz", - "integrity": "sha512-jZ5FAHDR2KBq2FsRUJW6GKDOAG9lUTX8aBEGq4Vf6B/35I9fPce66BornuwmqmKgfiSlecwuOb6oeoamYMohkg==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/server-renderer/-/server-renderer-3.5.12.tgz", + "integrity": "sha512-I3QoeDDeEPZm8yR28JtY+rk880Oqmj43hreIBVTicisFTx/Dl7JpG72g/X7YF8hnQD3IFhkky5i2bPonwrTVPg==", "dev": true, "license": "MIT", "dependencies": { - "@vue/compiler-ssr": "3.4.37", - "@vue/shared": "3.4.37" + "@vue/compiler-ssr": "3.5.12", + "@vue/shared": "3.5.12" }, "peerDependencies": { - "vue": "3.4.37" + "vue": "3.5.12" } }, "node_modules/@vue/shared": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.4.37.tgz", - "integrity": "sha512-nIh8P2fc3DflG8+5Uw8PT/1i17ccFn0xxN/5oE9RfV5SVnd7G0XEFRwakrnNFE/jlS95fpGXDVG5zDETS26nmg==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/@vue/shared/-/shared-3.5.12.tgz", + "integrity": "sha512-L2RPSAwUFbgZH20etwrXyVyCBu9OxRSi8T/38QsvnkJyvq2LufW2lDCOzm7t/U9C1mkhJGWYfCuFBCmIuNivrg==", "dev": true, "license": "MIT" }, @@ -11313,17 +11300,17 @@ "license": "MIT" }, "node_modules/vue": { - "version": "3.4.37", - "resolved": "https://registry.npmjs.org/vue/-/vue-3.4.37.tgz", - "integrity": "sha512-3vXvNfkKTBsSJ7JP+LyR7GBuwQuckbWvuwAid3xbqK9ppsKt/DUvfqgZ48fgOLEfpy1IacL5f8QhUVl77RaI7A==", + "version": "3.5.12", + "resolved": "https://registry.npmjs.org/vue/-/vue-3.5.12.tgz", + "integrity": "sha512-CLVZtXtn2ItBIi/zHZ0Sg1Xkb7+PU32bJJ8Bmy7ts3jxXTcbfsEfBivFYYWz1Hur+lalqGAh65Coin0r+HRUfg==", "dev": true, "license": "MIT", "dependencies": { - "@vue/compiler-dom": "3.4.37", - "@vue/compiler-sfc": "3.4.37", - "@vue/runtime-dom": "3.4.37", - "@vue/server-renderer": "3.4.37", - "@vue/shared": "3.4.37" + "@vue/compiler-dom": "3.5.12", + "@vue/compiler-sfc": "3.5.12", + "@vue/runtime-dom": "3.5.12", + "@vue/server-renderer": "3.5.12", + "@vue/shared": "3.5.12" }, "peerDependencies": { "typescript": "*" @@ -12177,7 +12164,7 @@ "@types/react-dom": "^18.2.0", "@typescript-eslint/eslint-plugin": "^5.16.0", "@typescript-eslint/parser": "^5.16.0", - "@vue/test-utils": "^2.4.4", + "@vue/test-utils": "^2.4.6", "express": "^4.18.2", "glob": "^7.2.0", "jest": "^29.4.0", @@ -12192,7 +12179,7 @@ "ts-jest": "^29.1.1", "typescript": "^5.0.4", "usehooks-ts": "^3.0.1", - "vue": "^3.2.31", + "vue": "^3.5.12", "zone.js": "^0.10.3" }, "engines": { diff --git a/packages/jest-environment/package.json b/packages/jest-environment/package.json index 1d1f4e7cb..7c65a76cf 100644 --- a/packages/jest-environment/package.json +++ b/packages/jest-environment/package.json @@ -64,7 +64,7 @@ "@radix-ui/react-select": "^1.2.2", "@testing-library/react": "^14.0.0", "@testing-library/user-event": "^14.5.1", - "@vue/test-utils": "^2.4.4", + "@vue/test-utils": "^2.4.6", "@types/jest": "29.5.2", "@types/node": "^16.11.7", "@types/react": "^18.2.0", @@ -85,7 +85,7 @@ "rxjs": "^6.5.3", "ts-jest": "^29.1.1", "typescript": "^5.0.4", - "vue": "^3.2.31", + "vue": "^3.5.12", "zone.js": "^0.10.3" }, "engines": { diff --git a/packages/jest-environment/src/index.ts b/packages/jest-environment/src/index.ts index 0ffe8699d..96e09a76c 100644 --- a/packages/jest-environment/src/index.ts +++ b/packages/jest-environment/src/index.ts @@ -19,6 +19,16 @@ export default class HappyDOMEnvironment implements JestEnvironment { public global: Global.Global; public moduleMocker: ModuleMocker; + /** + * jest-environment-jsdom" has the default set to ['browser'] + * As changing this value would be a breaking change, we will keep it at ['node', 'node-addons'] until we do a major release + * + * @see https://stackoverflow.com/questions/72428323/jest-referenceerror-vue-is-not-defined + */ + public customExportConditions = ['node', 'node-addons']; + + private _configuredExportConditions: string[]; + /** * Constructor. * @@ -47,6 +57,18 @@ export default class HappyDOMEnvironment implements JestEnvironment { throw new Error('Unsupported jest version.'); } + if ('customExportConditions' in projectConfig.testEnvironmentOptions) { + const { customExportConditions } = projectConfig.testEnvironmentOptions; + if ( + Array.isArray(customExportConditions) && + customExportConditions.every((condition) => typeof condition === 'string') + ) { + this._configuredExportConditions = customExportConditions; + } else { + throw new Error('Custom export conditions specified but they are not an array of strings'); + } + } + // Initialize Window and Global this.window = new Window({ url: 'http://localhost/', @@ -99,6 +121,13 @@ export default class HappyDOMEnvironment implements JestEnvironment { return happyDOMSetTimeout.call(this.global, ...args); }; } + /** + * Respect any export conditions specified as options + * https://jestjs.io/docs/configuration#testenvironmentoptions-object + */ + public exportConditions(): string[] { + return this._configuredExportConditions ?? this.customExportConditions; + } /** * Setup. diff --git a/packages/jest-environment/test/react/React.test.tsx b/packages/jest-environment/test/react/React.test.tsx index f28f1ba8b..4bcf03f2a 100644 --- a/packages/jest-environment/test/react/React.test.tsx +++ b/packages/jest-environment/test/react/React.test.tsx @@ -2,7 +2,6 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import * as ReactTestingLibrary from '@testing-library/react'; import ReactTestingLibraryUserEvent from '@testing-library/user-event'; -import { act } from 'react-dom/test-utils'; import { ReactDivComponent, ReactSelectComponent, @@ -28,7 +27,7 @@ describe('React', () => { }); it('Tests integration.', async () => { - act(() => { + React.act(() => { ReactDOM.createRoot(appElement).render(); }); await new Promise((resolve) => setTimeout(resolve, 2)); @@ -37,18 +36,18 @@ describe('React', () => { it('Can unmount a component.', async () => { const root = ReactDOM.createRoot(appElement); - act(() => { + React.act(() => { root.render(); }); await new Promise((resolve) => setTimeout(resolve, 2)); - act(() => { + React.act(() => { root.unmount(); }); expect(appElement.innerHTML).toBe(''); }); it('Handles adding and removing event listeners.', () => { - act(() => { + React.act(() => { ReactDOM.createRoot(appElement).render( {}} />); }); }); @@ -63,7 +62,7 @@ describe('React', () => { }); it('Can render Radix UI Select component.', async () => { - act(() => { + React.act(() => { ReactDOM.createRoot(appElement).render(