Unverified Commit 9faf14e0 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

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`
parent 0ae94488
...@@ -41,14 +41,19 @@ module.exports = { ...@@ -41,14 +41,19 @@ module.exports = {
return src return src
} }
const jestConfig = getJestConfig(jestOptions)
let swcTransformOpts = getJestSWCOptions({ let swcTransformOpts = getJestSWCOptions({
// When target is node it's similar to the server option set in SWC.
isServer:
jestConfig.testEnvironment && jestConfig.testEnvironment === 'node',
filename, filename,
styledComponents: inputOptions.styledComponents, styledComponents: inputOptions.styledComponents,
paths: inputOptions.paths, paths: inputOptions.paths,
baseUrl: inputOptions.resolvedBaseUrl, baseUrl: inputOptions.resolvedBaseUrl,
esm: esm:
isSupportEsm && isSupportEsm &&
isEsm(Boolean(inputOptions.isEsmProject), filename, jestOptions), isEsm(Boolean(inputOptions.isEsmProject), filename, jestConfig),
}) })
return transformSync(src, { ...swcTransformOpts, filename }) return transformSync(src, { ...swcTransformOpts, filename })
...@@ -64,11 +69,9 @@ function getJestConfig(jestConfig) { ...@@ -64,11 +69,9 @@ function getJestConfig(jestConfig) {
jestConfig jestConfig
} }
function isEsm(isEsmProject, filename, jestOptions) { function isEsm(isEsmProject, filename, jestConfig) {
return ( return (
(/\.jsx?$/.test(filename) && isEsmProject) || (/\.jsx?$/.test(filename) && isEsmProject) ||
getJestConfig(jestOptions).extensionsToTreatAsEsm?.find((ext) => jestConfig.extensionsToTreatAsEsm?.find((ext) => filename.endsWith(ext))
filename.endsWith(ext)
)
) )
} }
...@@ -60,6 +60,7 @@ function getBaseSWCOptions({ ...@@ -60,6 +60,7 @@ function getBaseSWCOptions({
} }
export function getJestSWCOptions({ export function getJestSWCOptions({
isServer,
filename, filename,
esm, esm,
styledComponents, styledComponents,
...@@ -70,7 +71,7 @@ export function getJestSWCOptions({ ...@@ -70,7 +71,7 @@ export function getJestSWCOptions({
filename, filename,
development: false, development: false,
hasReactRefresh: false, hasReactRefresh: false,
globalWindow: false, globalWindow: !isServer,
styledComponents, styledComponents,
paths, paths,
baseUrl, baseUrl,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment