NextJS import global stylesheets in all components #16294
-
Thanks to @lmiller1990 I got cypress component testing working with my NextJS application. It also plays nicely with I just ran into an issue with my global styles which are imported in the Tried to add them like this
/// <reference types="cypress" />
import '@testing-library/cypress/add-commands'
import '~/styles/normalize.scss'
import '~/styles/globals.scss'
import '~/styles/typography.scss' But that throws off NextJS ERROR in ./styles/globals.scss
Module Error (from ./node_modules/next/dist/build/webpack/loaders/error-loader.js):
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to pages/_app.js. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://nextjs.org/docs/messages/css-global
... |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 26 replies
-
I found a "hack" for it.. Converted my global stylesheets to styled components
import NormalizeStyle from '~/styles/NormalizeStyle'
import GlobalStyle from '~/styles/GlobalStyle'
import TypographyStyle from '~/styles/TypographyStyle'
function MyApp({ Component, pageProps }) {
return (
<>
<NormalizeStyle />
<GlobalStyle />
<TypographyStyle />
<Component {...pageProps} />
</>
)
}
export default MyApp that allows me to get the styles in like this describe('DishCard', () => {
it('looks great', () => {
mount(
<>
<NormalizeStyle />
<GlobalStyle />
<TypographyStyle />
<CartProvider>
<MenuProvider>
<DishCard id="dish02" />
</MenuProvider>
</CartProvider>
</>
)
cy.findByText('CHICKEN', { exact: false })
})
}) Currently trying to make a helper method to hide it a bit
import React from 'react'
import { mount } from '@cypress/react'
import NormalizeStyle from '~/styles/NormalizeStyle'
import GlobalStyle from '~/styles/GlobalStyle'
import TypographyStyle from '~/styles/TypographyStyle'
function render(Component: React.ReactNode): React.ReactNode {
return mount(
<>
<NormalizeStyle />
<GlobalStyle />
<TypographyStyle />
<Component />
</>
)
}
export { render } describe('DishCard', () => {
it('looks great', () => {
render(
<>
<CartProvider>
<MenuProvider>
<DishCard id="dish02" />
</MenuProvider>
</CartProvider>
</>
)
cy.findByText('CHICKEN', { exact: false })
})
}) but it does not work
|
Beta Was this translation helpful? Give feedback.
-
Hmm good point. This is going to be a problem for a lot of frameworks (not just Next.js). Importing them to support should generally be okay, but seems Next.js isn't happy. I can look next week sometime, we should find a clean solution and build it into the nextjs adapter so people don't need to think about this. |
Beta Was this translation helpful? Give feedback.
-
This issue is not supposed to happen on Next.js v10, there was an update that now allows to import .css files from any component. What version are you using? Upgrading might fix it (I import .css from any component and it works well) |
Beta Was this translation helpful? Give feedback.
-
This hack works better for me than the styles components (which keeps showing a warning because
import { mount as cyMount } from '@cypress/react'
import '@cypress/react/global.css'
export function mount(tsx: Parameters<typeof cyMount>[0], options?: Parameters<typeof cyMount>[1]) {
return cyMount(tsx, options)
}
@import './normalize.css';
@import './general.css';
@import './typography.css'; $ yarn add -D copyfiles
"scripts": {
...
"cy:open-ct": "yarn copyfiles -EV './styles/*.css' ./node_modules/@cypress/react && cypress open-ct",
... |
Beta Was this translation helpful? Give feedback.
-
I think I found another solution to allowing global.css to be imported to |
Beta Was this translation helpful? Give feedback.
This hack works better for me than the styles components (which keeps showing a warning because
@import
)