Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next/jest: Ensure typeof window is not transformed in jsdom environment #31304

Merged
merged 1 commit into from Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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