Skip to content

Commit

Permalink
Revert "fix(47299): allow testing pages with metadata in jsdom test e…
Browse files Browse the repository at this point in the history
…nvironment (#53578)"

This reverts commit 77acd16.
  • Loading branch information
huozhi committed Aug 17, 2023
1 parent fc3bf37 commit fa01e3a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/next/package.json
Expand Up @@ -242,6 +242,7 @@
"image-size": "1.0.0",
"is-docker": "2.0.0",
"is-wsl": "2.2.0",
"jest-docblock": "29.4.3",
"jest-worker": "27.0.0-next.5",
"json5": "2.2.3",
"jsonwebtoken": "9.0.0",
Expand Down
19 changes: 17 additions & 2 deletions packages/next/src/build/swc/jest-transformer.ts
Expand Up @@ -29,6 +29,7 @@ DEALINGS IN THE SOFTWARE.
import vm from 'vm'
import { transformSync } from './index'
import { getJestSWCOptions } from './options'
import * as docblock from 'next/dist/compiled/jest-docblock'
import type {
TransformerCreator,
TransformOptions,
Expand Down Expand Up @@ -76,16 +77,30 @@ function isEsm(
)
}

function getTestEnvironment(
src: string,
jestConfig: Config.ProjectConfig
): string {
const docblockPragmas = docblock.parse(docblock.extract(src))
const pragma = docblockPragmas['jest-environment']
const environment =
(Array.isArray(pragma) ? pragma[0] : pragma) ?? jestConfig.testEnvironment
return environment
}

const createTransformer: TransformerCreator<
SyncTransformer<JestTransformerConfig>,
JestTransformerConfig
> = (inputOptions) => ({
process(src, filename, jestOptions) {
const jestConfig = getJestConfig(jestOptions)
const testEnvironment = getTestEnvironment(src, jestConfig)

const swcTransformOpts = getJestSWCOptions({
// Always target server when compiling during test, to pass server-only validations and allow testing pages with metadatas
isServer: true,
// When target is node it's similar to the server option set in SWC.
isServer:
testEnvironment === 'node' ||
testEnvironment.includes('jest-environment-node'),
filename,
jsConfig: inputOptions?.jsConfig,
resolvedBaseUrl: inputOptions?.resolvedBaseUrl,
Expand Down
21 changes: 21 additions & 0 deletions packages/next/src/compiled/jest-docblock/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) Meta Platforms, Inc. and affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/next/src/compiled/jest-docblock/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/next/src/compiled/jest-docblock/package.json
@@ -0,0 +1 @@
{"name":"jest-docblock","main":"index.js","license":"MIT"}
10 changes: 10 additions & 0 deletions packages/next/taskfile.js
Expand Up @@ -2195,6 +2195,15 @@ export async function ncc_https_proxy_agent(task, opts) {
.target('src/compiled/https-proxy-agent')
}

// eslint-disable-next-line camelcase
externals['jest-docblock'] = 'next/dist/compiled/jest-docblock'
export async function ncc_jest_docblock(task, opts) {
await task
.source(relative(__dirname, require.resolve('jest-docblock')))
.ncc({ packageName: 'jest-docblock', externals })
.target('src/compiled/jest-docblock')
}

export async function precompile(task, opts) {
await task.parallel(
[
Expand Down Expand Up @@ -2329,6 +2338,7 @@ export async function ncc(task, opts) {
'ncc_opentelemetry_api',
'ncc_http_proxy_agent',
'ncc_https_proxy_agent',
'ncc_jest_docblock',
'ncc_mini_css_extract_plugin',
],
opts
Expand Down
5 changes: 5 additions & 0 deletions packages/next/types/misc.d.ts
Expand Up @@ -458,3 +458,8 @@ declare module 'next/dist/compiled/@opentelemetry/api' {
import * as m from '@opentelemetry/api'
export = m
}

declare module 'next/dist/compiled/jest-docblock' {
import m from 'jest-docblock'
export = m
}
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 8 additions & 39 deletions test/production/jest/server-only.test.ts
Expand Up @@ -8,19 +8,17 @@ describe('next/jest', () => {
next = await createNext({
skipStart: true,
files: {
'app/page.jsx': `import { PI } from '../lib/util'
export default function Home() {
return <h1>{PI}</h1>
}`,
'app/layout.jsx': `export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}`,

'app/page.jsx': `import { PI } from '../lib/util'
export default function Home() {
return <h1>{PI}</h1>
}`,

'app/page.test.jsx': `import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import Page from './page'
Expand All @@ -29,42 +27,13 @@ describe('next/jest', () => {
render(<Page />)
expect(screen.getByRole('heading')).toHaveTextContent('3.14')
})`,

'app/[blog]/page.jsx': `import { Metadata } from 'next'
export async function generateMetadata({
params: { blog: title },
}) {
return { title, description: 'A blog post about ' + title }
}
export default function Page({ params }) {
return <h1>All about {params.blog}</h1>
}
`,

'app/[blog]/page.test.jsx': `import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import Page from './page'
describe('Blog Page', () => {
it('has the appropriate title', () => {
render(<Page params={{ blog: 'Jane' }} />)
expect(screen.getByRole('heading')).toHaveTextContent('All about Jane')
})
})
`,

'lib/util.js': `
'lib/util.js': `/** @jest-environment node */
import 'server-only'
export const PI = 3.14;`,

'lib/utils.test.ts': `
import { PI } from './util'
'lib/utils.test.ts': `import { PI } from './util'
it('works from server-side code', () => {
expect(PI).toEqual(3.14)
})`,

'jest.config.js': `module.exports = require('next/jest')({ dir: './' })({ testEnvironment: 'jsdom' })`,
},
buildCommand: `yarn jest`,
Expand All @@ -79,11 +48,11 @@ describe('next/jest', () => {

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

it('can run test against server server only code', async () => {
it('can run test against server side components', async () => {
try {
await next.start()
} finally {
expect(next.cliOutput).toInclude('Tests: 3 passed, 3 total')
expect(next.cliOutput).toInclude('Tests: 2 passed, 2 total')
}
})
})

0 comments on commit fa01e3a

Please sign in to comment.