question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ship craco (CRA config override) adapter in `cypress/react`

See original GitHub issue

I use craco for react, so I can use Less with create-react-app

It replaces react-scripts start with craco 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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lmiller1990commented, May 5, 2021

Right, this makes perfect sense - using the react-scripts plugin to inject the dev server grabs the unconfigured webpack config from CRA - the whole point of craco is to override that.

I looked into this and learned I can use the craco webpack API, specifically createWebpackDevConfig, to get the modified config.

I wrote this code and was able to get the less styling to work with Cypress. The following is in cypress/plugins/index.js.

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 @cypress/react. I will work on shipping this. For now, you could use the above snippet.

image

Please let me know if this is working for you.

0reactions
ghostcommented, Sep 2, 2021
const cracoConfig = require("../../craco.config.js");
const webpackConfig = createWebpackDevConfig(cracoConfig);

module.exports = (on, config) => {
  on('dev-server:start', options => {
    return startDevServer({
      options,
      webpackConfig
    })
  })

  return conf

It works, thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create React App Configuration Override with Craco. What is ...
In this video, we'll update babel in CRA. How do I add Babel config in React app? - it's not a problem.Create React...
Read more >
Custom Templates - Create React App
Custom Templates enable you to select a template to create your project from, while still retaining all of the features of Create React...
Read more >
create-react-app config override for build - Stack Overflow
Then I thought: "well, if create-react-app works, maybe I could use CRA and just override things I don't care about?". I installed craco...
Read more >
Migrating from Create React App - Next.js
js optimizes images on-demand, as users request them. Your build times aren't increased, whether shipping 10 images or 10 million images. import Image...
Read more >
Unit testing React components in Cypress without CRA and ...
I had to go through some hassle to get that working with a custom webpack configuration, but it's finally working, and here is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found