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:
- We are planning to implement this in the future.
- [Relay transforms](/docs/architecture/nextjs-compiler#relay)
- 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)
- We are planning to implement this in the future.
- `experimental.nextScriptWorkers`
......
......@@ -6,28 +6,32 @@ import { join } from 'path'
// In order for the global isNextStart to be set
import 'e2e-utils'
describe('Invalid SCSS in _document', () => {
;(Boolean((global as any).isNextStart) ? describe : describe.skip)(
'production only',
() => {
const appDir = __dirname
// TODO: Implement warning for Turbopack
;(process.env.TURBOPACK ? describe.skip : describe)(
'Invalid SCSS in _document',
() => {
;(Boolean((global as any).isNextStart) ? describe : describe.skip)(
'production only',
() => {
const appDir = __dirname
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => {
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
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.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 */
import { readdir, readFile, remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils'
import { remove } from 'fs-extra'
import {
findPort,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
fetchViaHTTP,
} from 'next-test-utils'
import cheerio from 'cheerio'
import { join } from 'path'
const fixturesDir = join(__dirname, '../..', 'css-fixtures')
describe('Invalid CSS in _document', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-module-document')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail to build', async () => {
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
// TODO: Implement warning for Turbopack
;(process.env.TURBOPACK ? describe.skip : describe)(
'Invalid CSS in _document',
() => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-module-document')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
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 () => {
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.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 () => {
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
// eslint-disable-next-line jest/no-identical-title
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/)
})
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', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
......@@ -63,82 +78,108 @@ describe('Valid Global CSS from npm', () => {
() => {
const appDir = join(fixturesDir, 'import-global-from-module')
let appPort
let app
beforeAll(async () => {
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)
})
it('should compile successfully', async () => {
const { code, stdout } = await nextBuild(appDir, [], {
stdout: true,
})
expect(code).toBe(0)
expect(stdout).toMatch(/Compiled successfully/)
afterAll(async () => {
await killApp(app)
})
it(`should've emitted a single CSS file`, async () => {
const cssFolder = join(appDir, '.next/static/css')
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"}"`)
})
}
)
})
const content = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(content)
describe('Invalid Global CSS with Custom App', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'invalid-global-with-app')
const cssSheet = $('link[rel="stylesheet"]')
expect(cssSheet.length).toBe(1)
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
const stylesheet = cssSheet.attr('href')
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/
const cssContent = (
await fetchViaHTTP(appPort, stylesheet).then((res) => res.text())
)
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', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
const appDir = join(fixturesDir, 'valid-and-invalid-global')
// TODO: Implement warning for Turbopack
;(process.env.TURBOPACK ? describe.skip : describe)(
'Invalid Global CSS with Custom App',
() => {
;(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 () => {
await remove(join(appDir, '.next'))
})
// eslint-disable-next-line jest/no-identical-title
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 () => {
const { code, stderr } = await nextBuild(appDir, [], {
stderr: true,
// eslint-disable-next-line jest/no-identical-title
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).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 @@
},
"test/e2e/app-dir/scss/invalid-module-document/invalid-module-document.test.ts": {
"passed": [],
"failed": [
"failed": [],
"pending": [
"Invalid SCSS in _document production only should fail to build"
],
"pending": [],
"flakey": [],
"runtimeError": false
},
......@@ -7911,16 +7911,15 @@
},
"test/integration/css/test/valid-invalid-css.test.js": {
"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 Global CSS 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"
],
"pending": [],
"flakey": [],
"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