Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: set up component testing environment with Storybook, Jest, and Chromatic CI job #55

Merged
merged 12 commits into from
Aug 28, 2022

Conversation

dazedbear
Copy link
Owner

@dazedbear dazedbear commented Aug 27, 2022

Goal

Based on the testing strategy (#56), add the below changes

截圖 2022-08-28 下午11 28 02

Knowledge Transfer / Notes

storybook main.ts compatibility issue with nextjs and jest

Although this storybook official guide tell you that you can use .storybook/main.ts instead of .storybook/main.js. However, it's broken due to the below issue when running Jest testing.

I have found a solution yet, so just keep using .storybook/main.js for now.

ERR! /Users/xxxxxx/Projects/xxxxxx/xxxxxx/.storybook/main.ts:14
ERR! export {};
ERR! ^^^^^^
ERR! 
ERR! SyntaxError: Unexpected token 'export'

css parsing issue in Jest

If your test files contain import 'src/styles/global.css like importing pure stylesheet or css modules, Jest cannot parse them correctly, especially when you use tailwind css. This issue can be reproduce when running component snapshot test with @storybook/testing-react addons.

Please refer to the below docs to mock those css files with moduleNameMapper.

react-testing-library issue

Cannot find module 'react-dom/client' from '../../node_modules/@testing-library/react/dist/pure.js'

Per testing-library/react-testing-library#1038, @testing-library/react-testing-library v13 only supports React 18 which introduced react-dom/client. Downgrade to use v12 for a short-term fix.

Tailwind CSS style loading issue in storybook

If you configure important in your tailwind.config.js, you may encounter the issue that all styles are missing in the storybook.

截圖 2022-08-28 下午11 33 10

Since we cannot modify the #root id of storybook (per storybookjs/storybook#11296),
creating a decorator to add a wrapper with id app can fix this issue.

https://storybook.js.org/docs/react/writing-stories/decorators#wrap-stories-with-extra-markup

// .storybook/preview.tsx

import '../src/styles/global.css'
import React from 'react'

const withAppContainer = (Story) => (
  <div id="app">
    <Story />
  </div>
)

export const decorators = [withAppContainer]

Reference

Chromatic

Storybook

Storybook Addons

React Testing

Tailwind CSS

TypeScript

Jest

@vercel
Copy link

vercel bot commented Aug 27, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
dazedbear-github-io ✅ Ready (Inspect) Visit Preview Aug 28, 2022 at 3:48PM (UTC)

@dazedbear dazedbear changed the title Component test env test: set up environment for Storybook, snapshot testing, and Chromatic CI job Aug 27, 2022
@dazedbear dazedbear changed the title test: set up environment for Storybook, snapshot testing, and Chromatic CI job test: set up component testing environment with Storybook, Jest, and Chromatic CI job Aug 28, 2022
@dazedbear dazedbear marked this pull request as ready for review August 28, 2022 15:29
@dazedbear dazedbear merged commit e2a3c39 into main Aug 28, 2022
github-actions bot pushed a commit that referenced this pull request Aug 28, 2022
## [4.18.1](v4.18.0...v4.18.1) (2022-08-28)

### Testing

* set up component testing environment with Storybook, Jest, and Chromatic CI job ([#55](#55)) ([e2a3c39](e2a3c39))
@dazedbear dazedbear deleted the component-test-env branch September 10, 2022 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant