0

I am trying to do some component testing with Cypress and I can't get it to work, I am always getting an error.

enter image description here

I think this is happening because Tailwind is not set up correctly. I tried following multiple guides online including the one from cypress themselves but I can't get it to work.

My test:

import React from 'react';
import MyComponent from "./page";

describe('render', () => {
    it('renders', () => {
        cy.mount(<MyComponent/>)
    })
})

Component:

export default function MyComponent() {
  return (
     <div className="relative isolate">
        <div className="mx-auto grid max-w-7xl grid-cols-1">
           Hello
        </div>
     </div>
   )
}

cypress.config.js:

const { defineConfig } = require('cypress');

module.exports = defineConfig({
    component: {
        supportFile: 'cypress/support/component.js',
        devServer: {
            framework: 'next',
            bundler: 'webpack',
        },
    },
})

cypress/support/component.js:

import "tailwindcss/tailwind.css";

I also tried compiling css using tailwindcss -i src/app/globals.css -o src/index.css and including index.css but that didn't change anything either.

I tried testing a component that does not use tailwind in any way and it works so the issue is definitely with tailwind.

What am I missing?

The app is set up using Next if that matters.

4
  • 2
    Please provide a reproducible example. The Cypress example app listed below works.
    – A.Pearsson
    Commented Jul 9 at 9:09
  • this is a reproducible example, that's my current setup and it doesn't work for me
    – mevoker659
    Commented Jul 9 at 14:47
  • How is it reproducible, since you haven't posted the component? How can we compare to the published Cypress example? Commented Jul 10 at 2:01
  • I added a component to the question. I haven't posted it because it doesn't really matter what component I am testing. Once I add tailwind classes it fails. If I remove those classes it works just fine.
    – mevoker659
    Commented Jul 10 at 2:42

1 Answer 1

6

The cause may be that Tailwind code needs to be compiled, but component testing does not have the compilation steps of a full app.

Looking at cypress-component-testing-apps repo (the react-cra4-js example) they have a script in package.json to pre-compile the CSS.

"scripts": {
  ...
  "compile:css": "tailwindcss -i src/tailwind.css -o src/index.css",

Then the compiled index.css is imported in cypress/support/component.js

// Import commands.js using ES2015 syntax:
import './commands'

// Ensure global app styles are loaded:
import '../../src/index.css';                <-- after running compile:css script

import { mount } from 'cypress/react'

Cypress.Commands.add('mount', mount)

This seems to be a fairly simple way of by-passing the Tailwind compilation step inside the test.


Your component and test

I added Tailwind bg color and text color for more obvious effect,

export default function MyComponent() {
  return (
     <div className="relative isolate">
        <h1 className="mx-auto grid max-w-7xl grid-cols-1 bg-cy-blue text-white">
           Hello
        </h1>
     </div>
   )
}
import React from 'react'
import MyComponent from './my-component'

describe('render', () => {
  it('renders', () => {
    cy.mount(<MyComponent />)
    cy.get('h1')
      .should('have.text', 'Hello')
      .and('have.css', 'background-color', 'rgb(73, 86, 227)')  // a slightly saturated medium violet
  })
})

enter image description here

This is the imported CSS (into /cypress/support/component.ts)

@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');

body {
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

@tailwind base;
@tailwind components;
@tailwind utilities;
3
  • But wouldn’t importing tailwind.css from tailwindcss node module be enough?
    – mevoker659
    Commented Jul 8 at 12:41
  • also, same error. It just has index.css in the stack trace now: at push../src/index.css.options.insert (webpack://_N_E/./src/index.css?9f38:21)...
    – mevoker659
    Commented Jul 8 at 22:59
  • what the hell...I went through your update. It exactly matches my setup
    – mevoker659
    Commented Jul 10 at 16:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.