8

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.

2 Answers 2

9

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Both methods worked. The only thing, on the first option my custom css does not make any difference but is loading when I set up Craco on cypress plugins
0

You can directly import the tailwind CSS like this as mentioned in the cypress 7.0 migration guide. Also, mountingOptions.stylesheets are not recommended.

require('tailwindcss/dist/tailwindcss.min.css')

The entire snippet:

// In the majority of modern style-loaders,
// these styles will be injected into document.head when they're imported below
require('./index.scss')
require('tailwindcss/dist/tailwindcss.min.css')

const { mount } = require('@cypress/react')
const Button = require('./Button')

it('renders a Button', () => {
  // This button will render with the Tailwind CSS styles
  // as well as the application's index.scss styles
  mount(<Button />)
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.