Skip to content

Commit

Permalink
Add test for styled-components SWC transform disabled (#31214)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
timneutkens and kodiakhq[bot] committed Nov 9, 2021
1 parent ea88ef3 commit b5b315a
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 8 deletions.
5 changes: 1 addition & 4 deletions packages/next/build/swc/options.js
Expand Up @@ -43,10 +43,7 @@ function getBaseSWCOptions({
},
},
},
styledComponents: {
displayName: styledComponents,
ssr: styledComponents,
},
styledComponents: styledComponents ? {} : null,
}
}

Expand Down
65 changes: 65 additions & 0 deletions test/development/basic/styled-components-disabled.test.ts
@@ -0,0 +1,65 @@
import { join } from 'path'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'

describe('styled-components SWC transform', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'next.config.js': new FileRef(
join(__dirname, 'styled-components-disabled/next.config.js')
),
pages: new FileRef(join(__dirname, 'styled-components-disabled/pages')),
},
dependencies: {
'styled-components': '5.3.3',
},
})
})
afterAll(() => next.destroy())

async function matchLogs$(browser) {
let foundLog = false

const browserLogs = await browser.log('browser')

browserLogs.forEach((log) => {
if (log.message.includes('Warning: Prop `%s` did not match.')) {
foundLog = true
}
})
return foundLog
}
it('should have hydration mismatch with styled-components transform disabled', async () => {
let browser
try {
browser = await webdriver(next.appPort, '/')

// Compile /_error
await fetchViaHTTP(next.appPort, '/404')

try {
// Try 4 times to be sure there is no mismatch
expect(await matchLogs$(browser)).toBe(false)
await browser.refresh()
expect(await matchLogs$(browser)).toBe(false)
await browser.refresh()
expect(await matchLogs$(browser)).toBe(false)
await browser.refresh()
expect(await matchLogs$(browser)).toBe(false)
throw new Error('did not find mismatch')
} catch (err) {
// Verify that it really has the logs
expect(await matchLogs$(browser)).toBe(true)
}
} finally {
if (browser) {
await browser.close()
}
}
})
})
@@ -0,0 +1 @@
module.exports = {}
26 changes: 26 additions & 0 deletions test/development/basic/styled-components-disabled/pages/_app.js
@@ -0,0 +1,26 @@
import { createGlobalStyle, ThemeProvider } from 'styled-components'

const GlobalStyle = createGlobalStyle`
body {
margin: 0;
padding: 0;
box-sizing: border-box;
}
`

const theme = {
colors: {
primary: '#0070f3',
},
}

export default function App({ Component, pageProps }) {
return (
<>
<GlobalStyle />
<ThemeProvider theme={theme}>
<Component {...pageProps} />
</ThemeProvider>
</>
)
}
@@ -0,0 +1,30 @@
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'

export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage

try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})

const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}
38 changes: 38 additions & 0 deletions test/development/basic/styled-components-disabled/pages/index.js
@@ -0,0 +1,38 @@
import styled, { css } from 'styled-components'
const Button = styled.a`
/* This renders the buttons above... Edit me! */
display: inline-block;
border-radius: 3px;
padding: 0.5rem 0;
margin: 0.5rem 1rem;
width: 11rem;
background: transparent;
color: white;
border: 2px solid white;
/* The GitHub button is a primary button
* edit this to target it specifically! */
${(props) =>
props.primary &&
css`
background: white;
color: black;
`}
`

export default function Home() {
return (
<div>
<Button
href="https://github.com/styled-components/styled-components"
target="_blank"
rel="noopener"
primary
>
GitHub
</Button>

<Button href="/docs">Documentation</Button>
</div>
)
}
7 changes: 3 additions & 4 deletions test/development/basic/styled-components.test.ts
Expand Up @@ -10,9 +10,9 @@ describe('styled-components SWC transform', () => {
beforeAll(async () => {
next = await createNext({
files: {
// 'next.config.js': new FileRef(
// join(__dirname, 'styled-components/next.config.js')
// ),
'next.config.js': new FileRef(
join(__dirname, 'styled-components/next.config.js')
),
pages: new FileRef(join(__dirname, 'styled-components/pages')),
},
dependencies: {
Expand All @@ -28,7 +28,6 @@ describe('styled-components SWC transform', () => {
const browserLogs = await browser.log('browser')

browserLogs.forEach((log) => {
console.error({ log })
if (log.message.includes('Warning: Prop `%s` did not match.')) {
foundLog = true
}
Expand Down

0 comments on commit b5b315a

Please sign in to comment.