Skip to content

Commit

Permalink
next/jest: Ensure typeof window is not transformed in jsdom environme…
Browse files Browse the repository at this point in the history
…nt (#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`
  • Loading branch information
timneutkens committed Nov 11, 2021
1 parent 0ae9448 commit 9faf14e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 8 additions & 5 deletions packages/next/build/swc/jest-transformer.js
Expand Up @@ -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 })
Expand All @@ -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))
)
}
3 changes: 2 additions & 1 deletion packages/next/build/swc/options.js
Expand Up @@ -60,6 +60,7 @@ function getBaseSWCOptions({
}

export function getJestSWCOptions({
isServer,
filename,
esm,
styledComponents,
Expand All @@ -70,7 +71,7 @@ export function getJestSWCOptions({
filename,
development: false,
hasReactRefresh: false,
globalWindow: false,
globalWindow: !isServer,
styledComponents,
paths,
baseUrl,
Expand Down

0 comments on commit 9faf14e

Please sign in to comment.