Jest, snapshots and styled-jsx #13056
-
I want to remove the generated "css noise" from my component when generating snapshots. Babel, webpack and so on is still kind of a black-magic for me so i can't really figure out how to solve it. Is this documentation up-to-date? https://github.com/zeit/styled-jsx#rendering-in-tests It sounds really simple there that you should just add that 'styled-jsx/babel-test' plugin but it doesn't do anything for me? I have confirmed that my NODE_ENV actually is test. I can't get it to work. And when i read for example https://nextjs.org/docs/advanced-features/customizing-babel-config it seems like i should not use the plugins section? But i have no idea what to do there really and how to make it "Per env". |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
I've also found this and if i understand it correctly it should even work "out-of-the-box" now? |
Beta Was this translation helpful? Give feedback.
-
@olaj I have this code, I am not sure if it's the thing you need but take a look
module.exports = {
process() {
return 'module.exports = {};'
},
getCacheKey() {
return 'cssTransform'
},
}
// require('dotenv').config()
const { defaults } = require('jest-config')
module.exports = {
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
'!**/*.config.js',
],
collectCoverage: true,
coveragePathIgnorePatterns: [
'/node_modules/',
'/public/',
'/coverage/',
'enzyme.js',
'/jest.config.js',
'/.next/',
],
testMatch: ['<rootDir>/test/**/*.test.js'],
setupFilesAfterEnv: ['<rootDir>/test/setupTests.js'],
testPathIgnorePatterns: ['/node_modules/', '/.next/', '/public/', '/coverage/'],
transform: {
'^.+\\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
'.+\\.(css|styl|less|sass|scss)$': 'jest-css-modules-transform',
// '^.+\\.(css)$': '<rootDir>/test/config-jest/cssTransform.js',
},
transformIgnorePatterns: [
'/node_modules/',
'/public/',
'^.+\\.module\\.(css|sass|scss)$',
],
moduleNameMapper: {
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
},
} test command: "test": "jest --watch",
"test:ci": "jest --ci" |
Beta Was this translation helpful? Give feedback.
-
Sorry for the slow reply here but now i just tested with: https://github.com/vercel/next.js/tree/canary/examples/with-typescript-eslint-jest Then modified the .babelrc to
Then i created a snapshot. And i still get all the "jsx-124" on all of the elements.
Am I missing something? |
Beta Was this translation helpful? Give feedback.
-
It looks like Next automatically includes the And I believe your {
"presets": [
[
"next/babel",
{
"styled-jsx": {
"plugins": ["styled-jsx/babel-test"]
}
}
]
],
"plugins": []
}
Also here is a previous issue discussing this #6205 |
Beta Was this translation helpful? Give feedback.
-
@olaj Take a look at my Jest configuration, I bet you'll learn quite a few things. https://github.com/UnlyEd/next-right-now/tree/v2-mst-aptd-gcms-lcz-sty It uses Jest, snapshots, TS and more! |
Beta Was this translation helpful? Give feedback.
-
I went ahead and opened this issue, I'm getting the same error as @olaj |
Beta Was this translation helpful? Give feedback.
I went ahead and opened this issue, I'm getting the same error as @olaj
vercel/styled-jsx#649