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

Migrate to Vitest #260

Merged
merged 5 commits into from
Mar 24, 2025
Merged

Migrate to Vitest #260

merged 5 commits into from
Mar 24, 2025

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Feb 26, 2025

This replaces Jest with Vitest. Vitest has better support for ESM (which we use in some repositories), works with Prettier 3, and generally "just works," without too much additional configuration.

Examples

We are using Vitest in a few repos, including:

Copy link

socket-security bot commented Feb 26, 2025

Report too large to display inline

View full report↗︎

Copy link

socket-security bot commented Feb 26, 2025

🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎

To accept the risk, merge this PR and you will not be notified again.

Alert Package NoteSourceCI
Network access npm/[email protected] 🚫
Network access npm/[email protected] 🚫
Shell access npm/[email protected] 🚫
Network access npm/[email protected] 🚫
Network access npm/[email protected] 🚫

Ignoring: npm/[email protected], npm/[email protected], npm/[email protected], npm/[email protected]

View full report↗︎

Next steps

What is network access?

This module accesses the network.

Packages should remove all network access that is functionally unnecessary. Consumers should audit network access to ensure legitimate use.

What is shell access?

This module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.

Packages should avoid accessing the shell which can reduce portability, and make it easier for malicious shell access to be introduced.

Take a deeper look at the dependency

Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev.

Remove the package

If you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency.

Mark a package as acceptable risk

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of ecosystem/package-name@version specifiers. e.g. @SocketSecurity ignore npm/[email protected] or ignore all packages with @SocketSecurity ignore-all

Copy link
Member Author

Choose a reason for hiding this comment

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

This file demonstrates that Vitest has support for type tests out of the box. I figured it may be helpful to make developers aware of this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this is amazing, I had no clue!

@@ -1,6 +1,8 @@
import { describe, it, expect } from 'vitest';
Copy link
Member Author

Choose a reason for hiding this comment

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

Vitest doesn't inject globals like describe, it, expect by default. We could enable a setting for this, but I think importing is preferred over using globals.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed!

Copy link
Member Author

Choose a reason for hiding this comment

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

Since Vite uses esbuild under the hood, tests aren't type checked by default. Using a separate tsconfig seemed to be the most reliable way to get type checking and tests working.

For example, if a test in a test-d.ts file fails, Vitest outputs the following error:

 FAIL  src/index.test-d.ts > greeter > returns a string
TypeCheckError: Type 'number' does not satisfy the constraint '"Expected number, Actual string"'.
 ❯ src/index.test-d.ts:7:49
      5| describe('greeter', () => {
      6|   it('returns a string', () => {
      7|     expectTypeOf(greeter('Huey')).toEqualTypeOf<number>();
       |                                                 ^
      8|   });
      9| });

And type errors in regular test files are detected as well, as unhandled source errors:

TypeCheckError: Type 'string' is not assignable to type 'number'.
 ❯ src/index.test.ts:7:11
      5| describe('greeter', () => {
      6|   it('greets', () => {
      7|     const name: number = 'Huey';
       |           ^
      8|     const result = greeter(name);
      9|     expect(result).toBe('Hello, Huey!');

Copy link
Contributor

Choose a reason for hiding this comment

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

Great!

Comment on lines 12 to 13
"skipLibCheck": true,
"skipDefaultLibCheck": true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Vite's own declarations seem to rely on "dom" types. I don't think we should worry about Vite's declarations, and we can simply ignore checking those. These two options potentially speed up type checking slightly as well.

@Mrtenz
Copy link
Member Author

Mrtenz commented Feb 26, 2025

@SocketSecurity ignore npm/[email protected]
@SocketSecurity ignore npm/[email protected]

These are expected to use child_process as they are used by Vitest.

@SocketSecurity ignore npm/[email protected]

Network access is ok. Esbuild uses network access to download the right binary version depending on the platform.

@SocketSecurity ignore npm/[email protected]

New author is ok.

@Mrtenz Mrtenz marked this pull request as ready for review February 26, 2025 20:45
@Mrtenz Mrtenz requested a review from a team as a code owner February 26, 2025 20:45
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

This looks good, I just had a few questions.

@mcmire
Copy link
Contributor

mcmire commented Mar 4, 2025

@Mrtenz I'm seeing a peer dep warning when installing dependencies, is this important?

@metamask/metamask-module-template@workspace:. doesn't provide @typescript-eslint/utils (pdb740), requested by @vitest/eslint-plugin.

@Mrtenz
Copy link
Member Author

Mrtenz commented Mar 4, 2025

@Mrtenz I'm seeing a peer dep warning when installing dependencies, is this important?

@metamask/metamask-module-template@workspace:. doesn't provide @typescript-eslint/utils (pdb740), requested by @vitest/eslint-plugin.

It's installed regardless by other dependencies, but if it's a peer dependency, it makes sense to explicitly add it. We should update the installation instructions in eslint-config to mention this.

Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

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

Looks good!

.depcheckrc.json Outdated
Copy link
Contributor

Choose a reason for hiding this comment

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

This reminds me, we need to convert this file YAML so we can add comments to this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, but out of scope for this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Of course, I meant to say that. I created a ticket here: #265

@Mrtenz Mrtenz merged commit 5560ebb into main Mar 24, 2025
20 of 21 checks passed
@Mrtenz Mrtenz deleted the mrtenz/vitest branch March 24, 2025 09:13
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.

3 participants