Skip to content

Commit 02c258d

Browse files
authored
Revert "fix(47299): allow testing pages with metadata in jsdom test environment" (#54160)
Reverts #53578 This PR (#53578) will break client components test, revert it for now. Can repro by adding `"use client"` to `app/page.tsx` in `test/production/jest/server-only.test.ts` ``` FAIL app/page.test.jsx ● Test suite failed to run· Cannot find module 'private-next-rsc-mod-ref-proxy' from 'app/page.jsx'· Require stack: ```
1 parent fc3bf37 commit 02c258d

File tree

9 files changed

+74
-41
lines changed

9 files changed

+74
-41
lines changed

packages/next/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@
242242
"image-size": "1.0.0",
243243
"is-docker": "2.0.0",
244244
"is-wsl": "2.2.0",
245+
"jest-docblock": "29.4.3",
245246
"jest-worker": "27.0.0-next.5",
246247
"json5": "2.2.3",
247248
"jsonwebtoken": "9.0.0",

packages/next/src/build/swc/jest-transformer.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ DEALINGS IN THE SOFTWARE.
2929
import vm from 'vm'
3030
import { transformSync } from './index'
3131
import { getJestSWCOptions } from './options'
32+
import * as docblock from 'next/dist/compiled/jest-docblock'
3233
import type {
3334
TransformerCreator,
3435
TransformOptions,
@@ -76,16 +77,30 @@ function isEsm(
7677
)
7778
}
7879

80+
function getTestEnvironment(
81+
src: string,
82+
jestConfig: Config.ProjectConfig
83+
): string {
84+
const docblockPragmas = docblock.parse(docblock.extract(src))
85+
const pragma = docblockPragmas['jest-environment']
86+
const environment =
87+
(Array.isArray(pragma) ? pragma[0] : pragma) ?? jestConfig.testEnvironment
88+
return environment
89+
}
90+
7991
const createTransformer: TransformerCreator<
8092
SyncTransformer<JestTransformerConfig>,
8193
JestTransformerConfig
8294
> = (inputOptions) => ({
8395
process(src, filename, jestOptions) {
8496
const jestConfig = getJestConfig(jestOptions)
97+
const testEnvironment = getTestEnvironment(src, jestConfig)
8598

8699
const swcTransformOpts = getJestSWCOptions({
87-
// Always target server when compiling during test, to pass server-only validations and allow testing pages with metadatas
88-
isServer: true,
100+
// When target is node it's similar to the server option set in SWC.
101+
isServer:
102+
testEnvironment === 'node' ||
103+
testEnvironment.includes('jest-environment-node'),
89104
filename,
90105
jsConfig: inputOptions?.jsConfig,
91106
resolvedBaseUrl: inputOptions?.resolvedBaseUrl,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Meta Platforms, Inc. and affiliates.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/next/src/compiled/jest-docblock/index.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"jest-docblock","main":"index.js","license":"MIT"}

packages/next/taskfile.js

+10
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,15 @@ export async function ncc_https_proxy_agent(task, opts) {
21952195
.target('src/compiled/https-proxy-agent')
21962196
}
21972197

2198+
// eslint-disable-next-line camelcase
2199+
externals['jest-docblock'] = 'next/dist/compiled/jest-docblock'
2200+
export async function ncc_jest_docblock(task, opts) {
2201+
await task
2202+
.source(relative(__dirname, require.resolve('jest-docblock')))
2203+
.ncc({ packageName: 'jest-docblock', externals })
2204+
.target('src/compiled/jest-docblock')
2205+
}
2206+
21982207
export async function precompile(task, opts) {
21992208
await task.parallel(
22002209
[
@@ -2329,6 +2338,7 @@ export async function ncc(task, opts) {
23292338
'ncc_opentelemetry_api',
23302339
'ncc_http_proxy_agent',
23312340
'ncc_https_proxy_agent',
2341+
'ncc_jest_docblock',
23322342
'ncc_mini_css_extract_plugin',
23332343
],
23342344
opts

packages/next/types/misc.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,8 @@ declare module 'next/dist/compiled/@opentelemetry/api' {
458458
import * as m from '@opentelemetry/api'
459459
export = m
460460
}
461+
462+
declare module 'next/dist/compiled/jest-docblock' {
463+
import m from 'jest-docblock'
464+
export = m
465+
}

pnpm-lock.yaml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/production/jest/server-only.test.ts

+8-39
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ describe('next/jest', () => {
88
next = await createNext({
99
skipStart: true,
1010
files: {
11+
'app/page.jsx': `import { PI } from '../lib/util'
12+
export default function Home() {
13+
return <h1>{PI}</h1>
14+
}`,
1115
'app/layout.jsx': `export default function RootLayout({ children }) {
1216
return (
1317
<html lang="en">
1418
<body>{children}</body>
1519
</html>
1620
)
1721
}`,
18-
19-
'app/page.jsx': `import { PI } from '../lib/util'
20-
export default function Home() {
21-
return <h1>{PI}</h1>
22-
}`,
23-
2422
'app/page.test.jsx': `import { render, screen } from '@testing-library/react'
2523
import '@testing-library/jest-dom'
2624
import Page from './page'
@@ -29,42 +27,13 @@ describe('next/jest', () => {
2927
render(<Page />)
3028
expect(screen.getByRole('heading')).toHaveTextContent('3.14')
3129
})`,
32-
33-
'app/[blog]/page.jsx': `import { Metadata } from 'next'
34-
35-
export async function generateMetadata({
36-
params: { blog: title },
37-
}) {
38-
return { title, description: 'A blog post about ' + title }
39-
}
40-
41-
export default function Page({ params }) {
42-
return <h1>All about {params.blog}</h1>
43-
}
44-
`,
45-
46-
'app/[blog]/page.test.jsx': `import { render, screen } from '@testing-library/react'
47-
import '@testing-library/jest-dom'
48-
import Page from './page'
49-
50-
describe('Blog Page', () => {
51-
it('has the appropriate title', () => {
52-
render(<Page params={{ blog: 'Jane' }} />)
53-
expect(screen.getByRole('heading')).toHaveTextContent('All about Jane')
54-
})
55-
})
56-
`,
57-
58-
'lib/util.js': `
30+
'lib/util.js': `/** @jest-environment node */
5931
import 'server-only'
6032
export const PI = 3.14;`,
61-
62-
'lib/utils.test.ts': `
63-
import { PI } from './util'
33+
'lib/utils.test.ts': `import { PI } from './util'
6434
it('works from server-side code', () => {
6535
expect(PI).toEqual(3.14)
6636
})`,
67-
6837
'jest.config.js': `module.exports = require('next/jest')({ dir: './' })({ testEnvironment: 'jsdom' })`,
6938
},
7039
buildCommand: `yarn jest`,
@@ -79,11 +48,11 @@ describe('next/jest', () => {
7948

8049
afterAll(() => next.destroy())
8150

82-
it('can run test against server server only code', async () => {
51+
it('can run test against server side components', async () => {
8352
try {
8453
await next.start()
8554
} finally {
86-
expect(next.cliOutput).toInclude('Tests: 3 passed, 3 total')
55+
expect(next.cliOutput).toInclude('Tests: 2 passed, 2 total')
8756
}
8857
})
8958
})

0 commit comments

Comments
 (0)