Commit 01b7c576 authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Breakup CSS test suite (#9715)

* Breakup CSS test suite

* De-dupe next.config.js for css-fixtures
parent b1168569
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { remove } from 'fs-extra'
import {
nextBuild,
nextStart,
waitFor,
findPort,
killApp,
renderViaHTTP,
} from 'next-test-utils'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const fixturesDir = join(__dirname, '../../css-fixtures')
describe('CSS Module client-side navigation in Production', () => {
const appDir = join(fixturesDir, 'multi-module')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
let appPort
let app
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})
it('should be able to client-side navigate from red to blue', async () => {
let browser
try {
browser = await webdriver(appPort, '/red')
await browser.eval(`window.__did_not_ssr = 'make sure this is set'`)
const redColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-red')).color`
)
expect(redColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`)
await browser.elementByCss('#link-blue').click()
await browser.waitForElementByCss('#verify-blue')
const blueColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-blue')).color`
)
expect(blueColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`)
expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot(
`"make sure this is set"`
)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should be able to client-side navigate from blue to red', async () => {
const content = await renderViaHTTP(appPort, '/blue')
const $ = cheerio.load(content)
// Ensure only `/blue` page's CSS is preloaded
const serverCssPreloads = $('link[rel="preload"][as="style"]')
expect(serverCssPreloads.length).toBe(1)
let browser
try {
browser = await webdriver(appPort, '/blue')
await waitFor(2000) // Ensure hydration
await browser.eval(`window.__did_not_ssr = 'make sure this is set'`)
const redColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-blue')).color`
)
expect(redColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`)
// Check that Red was preloaded
const result = await browser.eval(
`[].slice.call(document.querySelectorAll('link[rel="preload"][as="style"]')).map(e=>({href:e.href})).sort()`
)
expect(result.length).toBe(2)
// Check that CSS was not loaded as script
const cssPreloads = await browser.eval(
`[].slice.call(document.querySelectorAll('link[rel=preload][href*=".css"]')).map(e=>e.as)`
)
expect(cssPreloads.every(e => e === 'style')).toBe(true)
await browser.elementByCss('#link-red').click()
await browser.waitForElementByCss('#verify-red')
const blueColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-red')).color`
)
expect(blueColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`)
expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot(
`"make sure this is set"`
)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should be able to client-side navigate from none to red', async () => {
let browser
try {
browser = await webdriver(appPort, '/none')
await browser.eval(`window.__did_not_ssr = 'make sure this is set'`)
await browser.elementByCss('#link-red').click()
await browser.waitForElementByCss('#verify-red')
const blueColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-red')).color`
)
expect(blueColor).toMatchInlineSnapshot(`"rgb(255, 0, 0)"`)
expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot(
`"make sure this is set"`
)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should be able to client-side navigate from none to blue', async () => {
let browser
try {
browser = await webdriver(appPort, '/none')
await browser.eval(`window.__did_not_ssr = 'make sure this is set'`)
await browser.elementByCss('#link-blue').click()
await browser.waitForElementByCss('#verify-blue')
const blueColor = await browser.eval(
`window.getComputedStyle(document.querySelector('#verify-blue')).color`
)
expect(blueColor).toMatchInlineSnapshot(`"rgb(0, 0, 255)"`)
expect(await browser.eval(`window.__did_not_ssr`)).toMatchInlineSnapshot(
`"make sure this is set"`
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import { readdir, readFile, remove } from 'fs-extra'
import { nextBuild } from 'next-test-utils'
import escapeStringRegexp from 'escape-string-regexp'
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 1
const fixturesDir = join(__dirname, '../..', 'css-fixtures')
describe('CSS Customization', () => {
const appDir = join(fixturesDir, 'custom-configuration')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should build successfully', async () => {
await nextBuild(appDir)
})
it(`should've compiled and prefixed`, 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(
`"@media (480px <= width < 768px){::placeholder{color:green}}.video{max-width:400px;max-height:300px}"`
)
// Contains a source map
expect(cssContent).toMatch(/\/\*#\s*sourceMappingURL=(.+\.map)\s*\*\//)
})
it(`should've emitted a source map`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const files = await readdir(cssFolder)
const cssMapFiles = files.filter(f => /\.css\.map$/.test(f))
expect(cssMapFiles.length).toBe(1)
const cssMapContent = (
await readFile(join(cssFolder, cssMapFiles[0]), 'utf8')
).trim()
const { version, mappings, sourcesContent } = JSON.parse(cssMapContent)
expect({ version, mappings, sourcesContent }).toMatchInlineSnapshot(`
Object {
"mappings": "AACA,gCACE,cACE,WACF,CACF,CAGA,OACE,eAA0B,CAA1B,gBACF",
"sourcesContent": Array [
"/* this should pass through untransformed */
@media (480px <= width < 768px) {
::placeholder {
color: green;
}
}
/* this should be transformed to width/height */
.video {
-xyz-max-size: 400px 300px;
}
",
],
"version": 3,
}
`)
})
})
describe('CSS Customization Array', () => {
const appDir = join(fixturesDir, 'custom-configuration-arr')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should build successfully', async () => {
await nextBuild(appDir)
})
it(`should've compiled and prefixed`, 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(
`"@media (480px <= width < 768px){a:before{content:\\"\\"}::placeholder{color:green}}.video{max-width:6400px;max-height:4800px;max-width:400rem;max-height:300rem}"`
)
// Contains a source map
expect(cssContent).toMatch(/\/\*#\s*sourceMappingURL=(.+\.map)\s*\*\//)
})
it(`should've emitted a source map`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const files = await readdir(cssFolder)
const cssMapFiles = files.filter(f => /\.css\.map$/.test(f))
expect(cssMapFiles.length).toBe(1)
const cssMapContent = (
await readFile(join(cssFolder, cssMapFiles[0]), 'utf8')
).trim()
const { version, mappings, sourcesContent } = JSON.parse(cssMapContent)
expect({ version, mappings, sourcesContent }).toMatchInlineSnapshot(`
Object {
"mappings": "AACA,gCACE,SACE,UACF,CACA,cACE,WACF,CACF,CAGA,OACE,gBAA4B,CAA5B,iBAA4B,CAA5B,gBAA4B,CAA5B,iBACF",
"sourcesContent": Array [
"/* this should pass through untransformed */
@media (480px <= width < 768px) {
a::before {
content: '';
}
::placeholder {
color: green;
}
}
/* this should be transformed to width/height */
.video {
-xyz-max-size: 400rem 300rem;
}
",
],
"version": 3,
}
`)
})
})
describe('Bad CSS Customization', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should build successfully', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(/field which is not supported.*?sourceMap/)
;[
'postcss-modules-values',
'postcss-modules-scope',
'postcss-modules-extract-imports',
'postcss-modules-local-by-default',
'postcss-modules',
].forEach(plugin => {
expect(stderr).toMatch(
new RegExp(`Please remove the.*?${escapeStringRegexp(plugin)}`)
)
})
})
it(`should've compiled and prefixed`, 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(
`".video{max-width:400px;max-height:300px}"`
)
// Contains a source map
expect(cssContent).toMatch(/\/\*#\s*sourceMappingURL=(.+\.map)\s*\*\//)
})
it(`should've emitted a source map`, async () => {
const cssFolder = join(appDir, '.next/static/css')
const files = await readdir(cssFolder)
const cssMapFiles = files.filter(f => /\.css\.map$/.test(f))
expect(cssMapFiles.length).toBe(1)
})
})
describe('Bad CSS Customization Array (1)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-1')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/A PostCSS Plugin was passed as an array but did not provide its configuration \('postcss-trolling'\)/
)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (2)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-2')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/Error: Your PostCSS configuration for 'postcss-trolling' cannot have null configuration./
)
expect(stderr).toMatch(
/To disable 'postcss-trolling', pass false, otherwise, pass true or a configuration object./
)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (3)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-3')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/A PostCSS Plugin must be provided as a string. Instead, we got: '5'/
)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (4)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-4')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(/An unknown PostCSS plugin was provided \(5\)/)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (5)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-5')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/Your custom PostCSS configuration must export a `plugins` key./
)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (6)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-6')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/Your custom PostCSS configuration must export a `plugins` key./
)
expect(stderr).toMatch(/Build error occurred/)
})
})
describe('Bad CSS Customization Array (7)', () => {
const appDir = join(fixturesDir, 'bad-custom-configuration-arr-7')
beforeAll(async () => {
await remove(join(appDir, '.next'))
})
it('should fail the build', async () => {
const { stderr } = await nextBuild(appDir, [], { stderr: true })
expect(stderr).toMatch(
/A PostCSS Plugin was passed as an array but did not provide its configuration \('postcss-trolling'\)/
)
expect(stderr).toMatch(/Build error occurred/)
})
})
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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