-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
require.context is not a function, snapshots, CRA 2 #4479
Comments
I think you can put {
"env": {
"test": {
"plugins": ["babel-plugin-require-context-hook"]
}
}
} |
I tried adding a .babelrc in the .storybook folder with
and then
just above
but I still get the error |
I am not the biggest expert in CRA =), but I see people are using react-app-rewired for that kind of things. |
Ok looks like I will have to downgrade to current version of Storybook for time being. react-app-rewired isn't compatible with CRA 2.0 unfortunately. I think a lot of people using require, CRA2 and the next version of Storybook are going to run into this issue. |
They mention customize-cra in docs. |
Thanks. I'm not entirely sure why but the following seems to have fixed the issue 🤞 -
As I did not actually add any overrides I did not need to use customize-cra. If I remove |
@semb09, jest.config.js ...
setupTestFrameworkScriptFile: '<rootDir>/testing/setupTestFramework.js',
transform: {
'^.+\\.jsx?$': '<rootDir>/testing/transform.js'
},
... testing/transform.js const babelOptions = {
presets: ['@babel/preset-react', '@babel/preset-env'],
plugins: ['require-context-hook', 'react-hot-loader/babel', '@babel/plugin-proposal-class-properties']
};
module.exports = require('babel-jest').createTransformer(babelOptions); testing/setupTestFramework.js import registerRequireContextHook from 'babel-plugin-require-context-hook/register';
import '@babel/polyfill';
registerRequireContextHook(); I do not have a |
@ecsmyth thank you for your solution!
|
@ecsmyth Are you using create-react-app (CRA2)? It doesn't look like we can override |
@mAAdhaTTah, my solution will not work with CRA2 unless you eject or fork With CRA2, you can set up the test environment by creating the file |
Hello I use require.context only in storybook config for loadStories; I followed these instructions: https://www.npmjs.com/package/@storybook/addon-storyshots#configure-jest-to-work-with-webpacks-requirecontext I used the macro, as its the only way to make it work in CRAv2. Now storybook doesn't build in the normal "development" webpack build. (warnings and errors)
and 4 more Module not found errors... thanks |
turns out that storybook can't find react-scripts config because i use a forked package instead. Any ideas how can i make storybook read the forked config? |
@d3bgger The relevant code is here: https://github.com/storybooks/storybook/blob/next/app/react/src/server/cra-config.js We'll be documenting these presets more comprehensively after the v5 release |
If you're having trouble with require-context.macro, here's a workaround that worked for me. Install
And add this to a
|
I was watching this issue, but not the damned repo I made... I've gone ahead and spruced that repo up! |
For those who stumble onto
|
The @ecsmyth solution works for me. Thanks |
For those who used it for i18next config:
Has nothing related to stories, I know, but I came here for the same kind of error, maybe it helps another dev. |
If you are reporting a bug or requesting support, start here:
Bug or support request summary
Bug
After updating to Create React App 2.0 I can no longer run snapshot tests with
yarn test
. I get the following errorSteps to reproduce
The code is written exactly as explained in the Quick Star Guide under structural testing https://storybook.js.org/testing/structural-testing/ and was working fine before I upgraded to CRA2. I also updated the versions of Storybook to the latest Beta. And I am also using the new SVG import syntax in CRA2 so I'm not sure which of these things has caused the issue.
Please specify which version of Storybook and optionally any affected addons that you're running
"@storybook/addon-actions": "^4.0.0-alpha.24",
"@storybook/addon-knobs": "^4.0.0-alpha.24",
"@storybook/addon-links": "^4.0.0-alpha.24",
"@storybook/addon-storyshots": "^4.0.0-alpha.24",
"@storybook/addons": "^4.0.0-alpha.24",
"@storybook/react": "^4.0.0-alpha.24",
I read in the Storyshot documentation that you may need to add
registerRequireContextHook
to your Jest test setup. This was not needed before I made the changes as mentioned above. And as I am using CRA, I only have access to setupTests.js and no way of adding a plugin to the jest babelrc file afaik.Any help would be really appreciated
The text was updated successfully, but these errors were encountered: