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

chore: migrate from jest to vitest #4396

Merged
merged 43 commits into from
Jul 29, 2024
Merged

Conversation

cardoso
Copy link
Contributor

@cardoso cardoso commented Jul 21, 2024

Details

This PR aims to migrate a few packages to vitest, starting with the easier ones.

I'll try to have 1 commit per package so it's easy to follow and choose up to which one to merge.

I'm trying not to touch jest tests at first, but some will break:

usage of jest global namespace (eg: jest.fn() -> vi.fn() ) and incompatible apis (eg: jest.setTimeout(n) -> vi.setConfig({ testTimeout: n })).

Closes #4153

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

@cardoso cardoso requested a review from a team as a code owner July 21, 2024 12:22
@nolanlawson
Copy link
Collaborator

Thanks a lot for the contribution! To be clear: is this PR a work-in-progress, or is it ready for review?

@cardoso
Copy link
Contributor Author

cardoso commented Jul 23, 2024

@nolanlawson its ready for review. I think tests not migrated here will require deeper refactoring.

The jest tests are failing. I'm not sure if it's better to exclude the migrated ones from jest or to keep the the old test files alongside.

@nolanlawson
Copy link
Collaborator

I think ideally we'd like to do a clean break from Jest. It would be extra maintenance to have some tests in Jest and others in Vitest.

@cardoso
Copy link
Contributor Author

cardoso commented Jul 24, 2024

@nolanlawson I'll give it a shot this weekend at fully migrating from jest.

@cardoso
Copy link
Contributor Author

cardoso commented Jul 24, 2024

Current status: 34 out of 41 suites migrated. 1169 out of 1321 tests running.

Jest (master branch):

Test Suites: 41 passed, 41 total
Tests:       1321 passed, 1321 total
Snapshots:   5 passed, 5 total
Time:        8.34 s, estimated 10 s

Vitest

Test Files  34 passed (34)
      Tests  1169 passed (1169)
   Start at  19:16:46
   Duration  5.15s (transform 2.86s, setup 148ms, collect 10.58s, tests 15.54s, environment 865ms, prepare 2.76s)

@@ -77,7 +77,7 @@ describe('customRendererConfig normalization', () => {
},
})
).toThrowErrorMatchingInlineSnapshot(
`"LWC1150: customRendererConfig contains duplicate entry for use element tag"`
`[Error: LWC1150: customRendererConfig contains duplicate entry for use element tag]`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's much easier to change the test here than make it work with vitest hopefully this is fine.

@@ -95,7 +95,7 @@ describe('customRendererConfig normalization', () => {
},
})
).toThrowErrorMatchingInlineSnapshot(
`"LWC1151: customRendererConfig should not contain a custom element tag, but found lightning-input"`
`[Error: LWC1151: customRendererConfig should not contain a custom element tag, but found lightning-input]`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing

@nolanlawson
Copy link
Collaborator

Figured out the issue with coverage tests – you actually can't define them in vitest.shared.js! They have to go in a top-level vitest.config.js instead:

Also, some of the configuration options are not allowed in a project config. Most notably:

  • coverage: coverage is done for the whole workspace

@nolanlawson
Copy link
Collaborator

/nucleus test

@nolanlawson
Copy link
Collaborator

/nucleus test

@nolanlawson
Copy link
Collaborator

/nucleus test

@nolanlawson
Copy link
Collaborator

Both Jest and Vitest report 41 test suites and 1327 tests passed.

@nolanlawson
Copy link
Collaborator

/nucleus test

@@ -34,21 +34,21 @@
"prettier": "v3 requires ESM, and we use prettier in our Jest tests. Jest does not support ESM yet."
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed, only needed for Jest

@nolanlawson nolanlawson requested a review from wjhsf July 29, 2024 18:01
Comment on lines 10 to 17
// Workaround to fix `const enum`, which is required because we use e.g. `APIFeature` from `@lwc/shared`
// See https://github.com/vitest-dev/vitest/discussions/3964
// Using `src` also ensures that the test coverage is accurately reported
alias: Object.fromEntries(
Object.keys(pkg.dependencies ?? {})
.filter((dep) => dep.startsWith('@lwc/'))
.map((dep) => [dep, `${dep}/src`])
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter if we define an alias for a package that's not actually used? Could we just move this to the root config and define aliases for all @lwc/ packages?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing this, but it messed with the lwc tests because it tries to import from dist.

I can give this another shot, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! 3b2954e

packages/@lwc/babel-plugin-component/vitest.config.mjs Outdated Show resolved Hide resolved
@nolanlawson
Copy link
Collaborator

/nucleus test

@nolanlawson nolanlawson merged commit 9ce4667 into salesforce:master Jul 29, 2024
13 of 18 checks passed
@nolanlawson
Copy link
Collaborator

Thanks a bunch @cardoso! 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch from Jest to Vitest
4 participants