-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Ship craco (CRA config override) adapter in cypress/react
#16273
Comments
Can you post a minimal reproduction? We use the same webpack config as your project does, so everything should work. I have some projects using sass and CSS modules, for example, and it's working fine. Are you using craco less? It's possible we are missing some edges cases for tools like craco. I have not used craco before, but we can add an example to the |
Right, this makes perfect sense - using the I looked into this and learned I can use the craco webpack API, specifically I wrote this code and was able to get the less styling to work with Cypress. The following is in const { createWebpackDevConfig } = require("@craco/craco");
const { startDevServer } = require('@cypress/webpack-dev-server')
const cracoConfig = require("../../craco.config.js");
const webpackConfig = createWebpackDevConfig(cracoConfig);
module.exports = (on, config) => {
on('dev-server:start', options => {
return startDevServer({
options,
webpackConfig
})
})
return config
} I think we can justify bundling a craco plugin with Please let me know if this is working for you. |
cypress/react
We will have a CRACO specific plugin for cypress/react in the next release. See #16333. |
Thank you so much! |
It works, thank you! |
Is there any update on how to implement this with cypress v10+? I'm implementing the latest version (12.9.0) and it looks like not both the workaround and the "new" (as of 2021) plugin both don't work/don't exist anymore. |
@kirstenlindsmith we don't support CRACO internally right now, but you should be able to get something to work in your Cypress configuration file like this const { devServer } = require("@cypress/webpack-dev-server")
const { defineConfig } = require("cypress");
const { createWebpackDevConfig } = require("@craco/craco");
module.exports = defineConfig({
component: {
devServer: (devServerOptions) => devServer({
...devServerOptions,
framework: 'react',
webpackConfig: async () => {
const cracoConfig = require("./craco.config.js");
return createWebpackDevConfig(cracoConfig)
},
}),
},
}); We need to do this because Cypress doesn't know how to get the webpack configuration for the app. We look for it in the root and have special support for Create React App that gets the webpack config that they use. This is a little more complicated - we need to call Please let me know if this works for you. Thanks! |
@kirstenlindsmith did you get it to work? I also have 12.9 installed and can't get the component tests to start. Here is my cypress.json and craco.config.js cypress.json const { devServer } = '@cypress/webpack-dev-server';
const { defineConfig } = require('cypress');
const { createWebpackDevConfig } = require('@craco/craco');
module.exports = defineConfig({
pageLoadTimeout: 180000,
numTestsKeptInMemory: 0,
reporterOptions: {
toConsole: true,
},
env: {
API_GATEWAY_BASE: 'https://development.services.com',
DEV_SERVER_PORT: 3000,
},
e2e: {
experimentalRunAllSpecs: true,
excludeSpecPattern: [
'**/energy_simulation/**',
'**/debug/**.js',
'**/_common/**',
],
},
component: {
devServer(devServerConfig) {
const cracoConfigFile = require('../../craco.config.js');
const cracoConfig = createWebpackDevConfig({
...cracoConfigFile,
webpack: {
...cracoConfigFile.webpack,
devtool: 'eval-source-map',
},
});
return devServer({
...devServerConfig,
framework: 'react',
webpackConfig: cracoConfig,
});
},
},
}); craco.config.js const { whenDev } = require('@craco/craco');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
module.exports = {
webpack: {
plugins: [
new NodePolyfillPlugin({
excludeAliases: ['console'],
}),
],
configure: whenDev(() => ({
devtool: 'eval-source-map',
})),
},
}; I get the error: |
@kirstenlindsmith @khashashin check this example I made. I used the craco config and cypress config from the above post and got it to work: https://github.com/lmiller1990/cypress-craco Not sure how you ran into that error, looks like your |
By moving cypress.config.js to the root folder of the project, I was able to start component testing without configuring anything via craco. @lmiller1990 Thank you for your help |
You shouldn't need to move it to the root 🤔 but glad you got it working 🎉 |
I use craco for react, so I can use Less with create-react-app
It replaces
react-scripts start
withcraco start
, so that the Less gets bundled to css.When using cypress normally, it's all good, because I start up the web server, and then it just gets served from there.
With the component runner however, it just renders the components directly. How can i make the styles work? If I mount a component that directly imports the
App.less
file, it obviously makes no difference.The text was updated successfully, but these errors were encountered: