From 9faf14e0b77326bc07a1cb39e6837a83d6aa3e6e Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Thu, 11 Nov 2021 16:05:32 +0100 Subject: [PATCH] next/jest: Ensure typeof window is not transformed in jsdom environment (#31304) Found that when the target is `jsdom` instead of `node` the "typeof window" transform is incorrect. This was causing an unexpected failure in on of the tests for vercel.com. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint` --- packages/next/build/swc/jest-transformer.js | 13 ++++++++----- packages/next/build/swc/options.js | 3 ++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/next/build/swc/jest-transformer.js b/packages/next/build/swc/jest-transformer.js index b2da3d124889ad2..72dfc686f393530 100644 --- a/packages/next/build/swc/jest-transformer.js +++ b/packages/next/build/swc/jest-transformer.js @@ -41,14 +41,19 @@ module.exports = { return src } + const jestConfig = getJestConfig(jestOptions) + let swcTransformOpts = getJestSWCOptions({ + // When target is node it's similar to the server option set in SWC. + isServer: + jestConfig.testEnvironment && jestConfig.testEnvironment === 'node', filename, styledComponents: inputOptions.styledComponents, paths: inputOptions.paths, baseUrl: inputOptions.resolvedBaseUrl, esm: isSupportEsm && - isEsm(Boolean(inputOptions.isEsmProject), filename, jestOptions), + isEsm(Boolean(inputOptions.isEsmProject), filename, jestConfig), }) return transformSync(src, { ...swcTransformOpts, filename }) @@ -64,11 +69,9 @@ function getJestConfig(jestConfig) { jestConfig } -function isEsm(isEsmProject, filename, jestOptions) { +function isEsm(isEsmProject, filename, jestConfig) { return ( (/\.jsx?$/.test(filename) && isEsmProject) || - getJestConfig(jestOptions).extensionsToTreatAsEsm?.find((ext) => - filename.endsWith(ext) - ) + jestConfig.extensionsToTreatAsEsm?.find((ext) => filename.endsWith(ext)) ) } diff --git a/packages/next/build/swc/options.js b/packages/next/build/swc/options.js index e3d454f00165119..ea025465366585b 100644 --- a/packages/next/build/swc/options.js +++ b/packages/next/build/swc/options.js @@ -60,6 +60,7 @@ function getBaseSWCOptions({ } export function getJestSWCOptions({ + isServer, filename, esm, styledComponents, @@ -70,7 +71,7 @@ export function getJestSWCOptions({ filename, development: false, hasReactRefresh: false, - globalWindow: false, + globalWindow: !isServer, styledComponents, paths, baseUrl,