twin.macro icon indicating copy to clipboard operation
twin.macro copied to clipboard

Stitches: jest test fail to run when there's more than two tw/css prop in a component

Open taneba opened this issue 3 years ago • 7 comments

If you have more than two tw or css prop in a component, jest test (using testing-library) will fail with error ReferenceError: _styled is not defined

Step to reproduce

(you can test it with my forked repo. Clone and run yarn run test in next-stitches-typescript folder : https://github.com/taneba/twin.examples/tree/repro-stitches-jest/next-stitches-typescript )

Let's say you have a component like:

import 'twin.macro'

const IndexPage = () => (
  <div>
    <div tw="text-center">Hello</div>
    <div tw="text-left">Hi</div>
  </div>
)

export default IndexPage

And test code:

import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import IndexPage from './index'

it('renders buttons', () => {
  render(<IndexPage />)
  expect(screen.getByText('Hello')).toBeInTheDocument()
})

This test should be passed, but if you run test you'll get

 FAIL  pages/index.test.tsx
  ● Test suite failed to run

    ReferenceError: _styled is not defined

      at Object.<anonymous> (pages/index.tsx:21:23)
      at Object.<anonymous> (pages/index.test.tsx:8:1)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:401:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Interestingly, the test will be passed if you get rid of one of the tw prop:

import 'twin.macro'

const IndexPage = () => (
  <div>
    <div tw="text-center">Hello</div>
    <div>Hi</div>
  </div>
)

export default IndexPage

taneba avatar Oct 21 '21 16:10 taneba

This sounds like a strange bug! I'll take a look into it and let you know.

ben-rogerson avatar Oct 24 '21 21:10 ben-rogerson

I've experienced this as well in a project using Next, Stitches, TypeScript, and Jest. Is there anything I could do to help fix this issue?

leolabs avatar Dec 08 '21 17:12 leolabs

Just posting this in case someone is in a similar situation. Encountered the exact same issue after doing upgrades, managed to solve it by downgrading babel-plugin-styled-components.

wg-daniel avatar Dec 10 '21 07:12 wg-daniel

@wg-daniel Thank you for the information, but do we really need babel-plugin-styled-components with usage of stitches? cc: @ben-rogerson

taneba avatar Dec 11 '21 02:12 taneba

Hi @taneba , @leolabs, and @wg-daniel

I just encountered this problem when setting up tests for a project that uses twin with stitches. I found a simple fix for this after some trial and error.

create a file jest.setup.js in your project dir and add the following

// jest.setup.js
import { styled } from './stitches.config'
global._styled = styled

then add the following to your jest.config.js:

 setupFilesAfterEnv: ['<rootDir>/jest.setup.js']

finally run

jest --clearCache

I tested it in the reproduction of @taneba, it fixes it there as well:

https://github.com/taneba/twin.examples/pull/1

nibtime avatar Feb 23 '22 23:02 nibtime

@nibtime Thanks for tackling this and that seems to be a simple workaround 💯

taneba avatar Mar 01 '22 01:03 taneba

Also had this problem when using twin.macro in Storybook. Adding the following seemed to fix it

// .storybook/preview.js
import { styled } from 'twin.macro'
global._styled = styled

xylophonehero avatar Mar 01 '22 09:03 xylophonehero

Here's a related link to the jest + react testing library example →

ben-rogerson avatar Dec 04 '22 06:12 ben-rogerson