reactjscypresstailwind-csscraco

Cypress Component Testing, ReactJS, and TailwindCSS


Does anyone know how can I load the TailwindCSS from the testing files?

I've tried to use the same approach I used on VueJS, importing the css file, but it does just not load the styles.

Here's the commit where I added the cypress component testing: https://github.com/vicainelli/cypress-component-testing-react-tailwindcss/commit/2fa25833cb965fadfeda6c53b80a23bb12b3b1c5

I know in mount there are options that I can pass the stylesheet, for example

Like this:

mount(<App />, { stylesheet: "https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" });

But I would like to use my custom css.


Solution

  • The Cypress docs have a typo, you should import this

    import 'tailwindcss/dist/tailwind.min.css'
    

    not this

    import 'tailwindcss/dist/tailwindcss.min.css'   // causes error, not in node_modules 
    
    import React from 'react';
    import { mount } from '@cypress/react';
    import App from './App';
    import './index.css';
    import 'tailwindcss/dist/tailwind.min.css'
    
    it('should renders the App correctly', () => {
      mount(<App />) 
      cy.get('h1').contains('Cypress Component Testing with Tailwind CSS')
        .should('have.css', 'font-family') 
        .and('match', /Georgia/)          // passes
    });
    

    Or can use the cracao plugin in cypress/plugins/index.js

    yarn add -D @cypress/react
    //or
    npm install -D @cypress/react
    
    const cracoConfig = require('../../craco.config.js')
    const injectDevServer = require('@cypress/react/plugins/craco')
    
    module.exports = (on, config) => {
      injectDevServer(on, config, cracoConfig)
    
      return config
    }
    

    which activates the contents of craco.config.js (you already have)

    module.exports = {
      style: {
        postcss: {
          plugins: [
            require('tailwindcss'),
            require('autoprefixer'),
          ],
        },
      },
    }
    
    import React from 'react';
    import { mount } from '@cypress/react';
    import App from './App';
    import './index.css';
    // import 'tailwindcss/dist/tailwind.min.css'    // not required, plugin works
    
    it('should renders the App correctly', () => {
      mount(<App />) 
      cy.get('h1').contains('Cypress Component Testing with Tailwind CSS')
        .should('have.css', 'font-family') 
        .and('match', /Georgia/)          // passes
    });