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

Build: Migrate unit tests from Jest to Vitest #24415

Merged
merged 116 commits into from Dec 20, 2023
Merged

Conversation

yannbf
Copy link
Member

@yannbf yannbf commented Oct 9, 2023

What I did

This migrates the monorepo unit tests from Jest to Vitest. This will make it easier to convert our codebase to ESM, and vitest has some good DX which we can take advantage of.

I wrote a codemod to do most of the manual labor and you can find it here: https://go.intuita.io/IX3EFT

The codemod takes care of:
  1. JSDOM environment comments
// from
/**
 * @jest-environment jsdom
 */

// to
/**
 * @vitest-environment jsdom
 */
  1. Type reference comments
// from
/// <reference types="@types/jest" />;

// to
/// <reference types="vitest" />;
  1. All sorts of spy/mock/fn migrations
// from
jest.mock('@storybook/preview-api');
const value = jest.requireActual('@storybook/preview-api');
const value2 = jest.createMockFromModule('@storybook/preview-api');
const myMock = jest.fn();
const spy = jest.spyOn(EventsPackageExport, 'bla');

// to
import { vi } from "vitest";

vi.mock('@storybook/preview-api');
const value = await vi.importActual('@storybook/preview-api');
const value2 = vi.mock('@storybook/preview-api')
const myMock = vi.fn();
const spy = vi.spyOn(EventsPackageExport, 'bla');
  1. All sorts of TypeScript type migrations
// from
const mockedApi = api as unknown as jest.Mocked<api.API>;
const toIdMock = toId as jest.Mock<ReturnType<typeof toId>>;
let mockChannel: { on: jest.Mock; emit?: jest.Mock };
(helpers.isNxProject as jest.SpyInstance).mockResolvedValue(true);

// to
import { Mock, Mocked, SpyInstance } from "vitest";

const mockedApi = api as unknown as Mocked<api.API>;
const toIdMock = toId as Mock<ReturnType<typeof toId>>;
let mockChannel: { on: Mock; emit?: Mock };
(helpers.isNxProject as SpyInstance).mockResolvedValue(true);

Apart from adding missing imports for describe, it, test, beforeEach, beforeAll, afterEach, afterAll, etc.

To sum the changes up:

  • Codemod ran across all files in the codebase
  • storyshots and storyshots-puppeteer packages are deleted. (they are deprecated, and in Storybook 8 they would be removed. Else we'd need to do a rework on them for proper ESM support)
  • Mocking mechanism changed e.g.
  • A lot of snapshots are updated
  • Some package.json exports were changed
  • jest config files were turned into vitest config files
  • jest-os-detection (for windowsOnly tests) usage was replaced with a small local utility
  • jest-specific-snapshot was replaced with Vitest's own snapshot utility

Solved problems

We encountered and overcame some challenges in this conversion, see collapsed details below if you're interested.

details

Out of memory issue - Solved!

When running the tests, the process hangs for about 3min and then presents the following error message:
image

This has to be investigated and fixed.

Fixed by 6d35099

Emotion loading twice - Solved!

It seems that @emotion/react is being loaded in two different contexts, which causes the theme to be lost, and some tests to fail. It's unclear what is causing this. This is breaking tests in addons/interactions, addons/a11y, and ui/blocks, and ui/manager.

Call counts in StoryIndexGenerator tests are off by one - Solved!

AssertionError: expected "N" to be called 6 times, but got 5 times. It's unclear what caused this, or whether it is truly broken. This breaks tests in lib/core-server.

Skipped tests

We couldn't figure out an emotion theming issue for a couple of tests, so chose to temporarily skip them to unblock the rest of the conversion.

  • addons/a11y/src/components/A11YPanel.test.tsx
  • addons/a11y/src/components/VisionSimulator.test.tsx

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Run yarn test or yarn test:watch in the code directory.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

@yannbf yannbf added build Internal-facing build tooling & test updates ci:normal labels Oct 9, 2023
@ndelangen ndelangen merged commit bf498ea into next Dec 20, 2023
56 of 58 checks passed
@ndelangen ndelangen deleted the norbert/vitest-for-monorepo branch December 20, 2023 00:10
@github-actions github-actions bot mentioned this pull request Dec 20, 2023
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Internal-facing build tooling & test updates ci:normal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants