Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for styled-components SWC transform disabled #31214

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/next/build/swc/options.js
Expand Up @@ -43,10 +43,7 @@ function getBaseSWCOptions({
},
},
},
styledComponents: {
displayName: styledComponents,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True is the default, same for ssr

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(
Copy link
Member

@ijjk ijjk Nov 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the next.config.js being used here? This can be provided via the nextConfig field on createNext instead which is typed.

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()
}
}
}
@@ -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