Unverified Commit 01b6ec52 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack build: Fix CSS tests (#68841)

## What?

Further fixes for CSS related tests with Turbopack build, skipped a few
that are checking for a particular warning that is not really relevant
for Turbopack, documented that the warning is missing currently.

Also fixed another test that was reading from disk instead of loading
the page.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 7d952edc
...@@ -53,6 +53,9 @@ These features are currently not supported: ...@@ -53,6 +53,9 @@ These features are currently not supported:
- We are planning to implement this in the future. - We are planning to implement this in the future.
- [Relay transforms](/docs/architecture/nextjs-compiler#relay) - [Relay transforms](/docs/architecture/nextjs-compiler#relay)
- We are planning to implement this in the future. - We are planning to implement this in the future.
- Blocking `.css` imports in `pages/_document.tsx`
- Currently with webpack Next.js blocks importing `.css` files in `pages/_document.tsx`
- We are planning to implement this warning in the future.
- [`experimental.typedRoutes](https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes) - [`experimental.typedRoutes](https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes)
- We are planning to implement this in the future. - We are planning to implement this in the future.
- `experimental.nextScriptWorkers` - `experimental.nextScriptWorkers`
......
...@@ -6,28 +6,32 @@ import { join } from 'path' ...@@ -6,28 +6,32 @@ import { join } from 'path'
// In order for the global isNextStart to be set // In order for the global isNextStart to be set
import 'e2e-utils' import 'e2e-utils'
describe('Invalid SCSS in _document', () => { // TODO: Implement warning for Turbopack
;(Boolean((global as any).isNextStart) ? describe : describe.skip)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production only', 'Invalid SCSS in _document',
() => { () => {
const appDir = __dirname ;(Boolean((global as any).isNextStart) ? describe : describe.skip)(
'production only',
() => {
const appDir = __dirname
beforeAll(async () => { beforeAll(async () => {
await remove(join(appDir, '.next')) await remove(join(appDir, '.next'))
}) })
it('should fail to build', async () => { it('should fail to build', async () => {
const { code, stderr } = await nextBuild(appDir, [], { const { code, stderr } = await nextBuild(appDir, [], {
stderr: true, stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles.module.scss')
expect(stderr).toMatch(
/CSS.*cannot.*be imported within.*pages[\\/]_document\.js/
)
expect(stderr).toMatch(/Location:.*pages[\\/]_document\.js/)
}) })
expect(code).not.toBe(0) }
expect(stderr).toContain('Failed to compile') )
expect(stderr).toContain('styles.module.scss') }
expect(stderr).toMatch( )
/CSS.*cannot.*be imported within.*pages[\\/]_document\.js/
)
expect(stderr).toMatch(/Location:.*pages[\\/]_document\.js/)
})
}
)
})
/* eslint-env jest */ /* eslint-env jest */
import { readdir, readFile, remove } from 'fs-extra' import { remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils' import {
findPort,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
fetchViaHTTP,
} from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path' import { join } from 'path'
const fixturesDir = join(__dirname, '../..', 'css-fixtures') const fixturesDir = join(__dirname, '../..', 'css-fixtures')
describe('Invalid CSS in _document', () => { // TODO: Implement warning for Turbopack
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'Invalid CSS in _document',
() => { () => {
const appDir = join(fixturesDir, 'invalid-module-document') ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
beforeAll(async () => { () => {
await remove(join(appDir, '.next')) const appDir = join(fixturesDir, 'invalid-module-document')
})
beforeAll(async () => {
it('should fail to build', async () => { await remove(join(appDir, '.next'))
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
}) })
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles.module.css')
expect(stderr).toMatch(
/CSS.*cannot.*be imported within.*pages[\\/]_document\.js/
)
expect(stderr).toMatch(/Location:.*pages[\\/]_document\.js/)
})
}
)
})
describe('Invalid Global CSS', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-global')
beforeAll(async () => { it('should fail to build', async () => {
await remove(join(appDir, '.next')) const { code, stderr } = await nextBuild(appDir, [], {
}) stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles.module.css')
expect(stderr).toMatch(
/CSS.*cannot.*be imported within.*pages[\\/]_document\.js/
)
expect(stderr).toMatch(/Location:.*pages[\\/]_document\.js/)
})
}
)
}
)
;(process.env.TURBOPACK ? describe.skip : describe)(
'Invalid Global CSS',
() => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-global')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => { // eslint-disable-next-line jest/no-identical-title
const { code, stderr } = await nextBuild(appDir, [], { it('should fail to build', async () => {
stderr: true, const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles/global.css')
expect(stderr).toMatch(
/Please move all first-party global CSS imports.*?pages(\/|\\)_app/
)
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/)
}) })
expect(code).not.toBe(0) }
expect(stderr).toContain('Failed to compile') )
expect(stderr).toContain('styles/global.css') }
expect(stderr).toMatch( )
/Please move all first-party global CSS imports.*?pages(\/|\\)_app/
)
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/)
})
}
)
})
describe('Valid Global CSS from npm', () => { describe('Valid Global CSS from npm', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
...@@ -63,82 +78,108 @@ describe('Valid Global CSS from npm', () => { ...@@ -63,82 +78,108 @@ describe('Valid Global CSS from npm', () => {
() => { () => {
const appDir = join(fixturesDir, 'import-global-from-module') const appDir = join(fixturesDir, 'import-global-from-module')
let appPort
let app
beforeAll(async () => { beforeAll(async () => {
await remove(join(appDir, '.next')) await remove(join(appDir, '.next'))
const { code } = await nextBuild(appDir)
if (code !== 0) {
throw new Error('failed to build')
}
appPort = await findPort()
app = await nextStart(appDir, appPort)
}) })
afterAll(async () => {
it('should compile successfully', async () => { await killApp(app)
const { code, stdout } = await nextBuild(appDir, [], {
stdout: true,
})
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
}) })
it(`should've emitted a single CSS file`, async () => { it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css') const content = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(content)
const files = await readdir(cssFolder)
const cssFiles = files.filter((f) => /\.css$/.test(f))
expect(cssFiles.length).toBe(1)
const cssContent = await readFile(join(cssFolder, cssFiles[0]), 'utf8')
expect(
cssContent.replace(/\/\*.*?\*\//g, '').trim()
).toMatchInlineSnapshot(`".red-text{color:"red"}"`)
})
}
)
})
describe('Invalid Global CSS with Custom App', () => { const cssSheet = $('link[rel="stylesheet"]')
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( expect(cssSheet.length).toBe(1)
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-global-with-app')
beforeAll(async () => { const stylesheet = cssSheet.attr('href')
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => { const cssContent = (
const { code, stderr } = await nextBuild(appDir, [], { await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles/global.css')
expect(stderr).toMatch(
/Please move all first-party global CSS imports.*?pages(\/|\\)_app/
) )
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/) .replace(/\/\*.*?\*\//g, '')
.trim()
if (process.env.TURBOPACK) {
expect(cssContent).toMatchInlineSnapshot(`
".red-text {
color: "red";
}"
`)
} else {
expect(cssContent).toMatchInlineSnapshot(`".red-text{color:"red"}"`)
}
}) })
} }
) )
}) })
describe('Valid and Invalid Global CSS with Custom App', () => { // TODO: Implement warning for Turbopack
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK ? describe.skip : describe)(
'production mode', 'Invalid Global CSS with Custom App',
() => { () => {
const appDir = join(fixturesDir, 'valid-and-invalid-global') ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-global-with-app')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
beforeAll(async () => { // eslint-disable-next-line jest/no-identical-title
await remove(join(appDir, '.next')) it('should fail to build', async () => {
}) const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles/global.css')
expect(stderr).toMatch(
/Please move all first-party global CSS imports.*?pages(\/|\\)_app/
)
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/)
})
}
)
}
)
// TODO: Implement warning for Turbopack
;(process.env.TURBOPACK ? describe.skip : describe)(
'Valid and Invalid Global CSS with Custom App',
() => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'valid-and-invalid-global')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => { // eslint-disable-next-line jest/no-identical-title
const { code, stderr } = await nextBuild(appDir, [], { it('should fail to build', async () => {
stderr: true, const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
})
expect(code).not.toBe(0)
expect(stderr).toContain('Failed to compile')
expect(stderr).toContain('styles/global.css')
expect(stderr).toContain(
'Please move all first-party global CSS imports'
)
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/)
}) })
expect(code).not.toBe(0) }
expect(stderr).toContain('Failed to compile') )
expect(stderr).toContain('styles/global.css') }
expect(stderr).toContain( )
'Please move all first-party global CSS imports'
)
expect(stderr).toMatch(/Location:.*pages[\\/]index\.js/)
})
}
)
})
...@@ -3704,10 +3704,10 @@ ...@@ -3704,10 +3704,10 @@
}, },
"test/e2e/app-dir/scss/invalid-module-document/invalid-module-document.test.ts": { "test/e2e/app-dir/scss/invalid-module-document/invalid-module-document.test.ts": {
"passed": [], "passed": [],
"failed": [ "failed": [],
"pending": [
"Invalid SCSS in _document production only should fail to build" "Invalid SCSS in _document production only should fail to build"
], ],
"pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
}, },
...@@ -7911,16 +7911,15 @@ ...@@ -7911,16 +7911,15 @@
}, },
"test/integration/css/test/valid-invalid-css.test.js": { "test/integration/css/test/valid-invalid-css.test.js": {
"passed": [ "passed": [
"Valid Global CSS from npm production mode should compile successfully" "Valid Global CSS from npm production mode should've emitted a single CSS file"
], ],
"failed": [ "failed": [],
"pending": [
"Invalid CSS in _document production mode should fail to build", "Invalid CSS in _document production mode should fail to build",
"Invalid Global CSS production mode should fail to build", "Invalid Global CSS production mode should fail to build",
"Invalid Global CSS with Custom App production mode should fail to build", "Invalid Global CSS with Custom App production mode should fail to build",
"Valid Global CSS from npm production mode should've emitted a single CSS file",
"Valid and Invalid Global CSS with Custom App production mode should fail to build" "Valid and Invalid Global CSS with Custom App production mode should fail to build"
], ],
"pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
}, },
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