Cypress Component Testing, ReactJS, and TailwindCSS
Asked Answered
B

2

10

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.

Baghdad answered 15/8, 2021 at 15:41 Comment(0)
O
14

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
});
Oberon answered 15/8, 2021 at 23:35 Comment(1)
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 pluginsBaghdad
B
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 />)
})
Berna answered 15/8, 2021 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.