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

Vitest 🚀 #4628

Merged
merged 36 commits into from
Mar 5, 2025
Merged

Vitest 🚀 #4628

merged 36 commits into from
Mar 5, 2025

Conversation

marcelgerber
Copy link
Member

@marcelgerber marcelgerber commented Mar 5, 2025

This makes our tests run way smoother & more awesome by switching from Jest to Vitest!

This means we don't need any compilation step any more, and can watch test files with incredible speed. This is super nice!


Review notes:

  • Don't look at the commits, they're often messy
  • Most test files are barely changed at all - basically only adding import { it, expect, describe } from "vitest" to the test
  • I changed the .jsdom.test.tsx? test files to be named .test.tsx?, too - and instead added a @vitest-environment jsdom comment at the top. It also turned out that many of them didn't even need the JSDom env at all - for those I've removed it.
  • Likewise, I renamed some files from tsx to ts if they are not actually TSX.
  • I had to disable 3 test suites that were mocking DB or S3 API calls: This is due to a constraint in mocking, where if methods a, b are both defined in module.ts, and a calls b, then you can't mock b from the outside (or, well, it won't have any effect).
    • we can solve these by refactoring this a bunch, and splitting the DB calls and the business logic into two separate files
  • I'll add comments to the relevant parts of this PR, to aid the review a bit

Copy link

socket-security bot commented Mar 5, 2025

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report↗︎

Comment on lines 35 to 39
- name: Run tsc build
run: yarn buildTsc

- name: Run tsc buildTests
run: yarn lerna run buildTests
Copy link
Member Author

Choose a reason for hiding this comment

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

Added side effect: The istJustJavascript and dist/tests dirs are pretty much things of the past.
I haven't entirely gone rid of them at this point, but we're pretty close now!

Copy link
Contributor

Choose a reason for hiding this comment

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

When I last looked into this, i.e. when I introduced tsx, the main reason to keep istJustJavascript was that tsx runs esbuild every time, so we pay the cost of the transpilation on every restart of the process, e.g. the admin server, and also I wasn't sure how much ongoing overhead it incurs during runtime.

I'm now more inclined to think it would be fine to use it in prod, but it warrants more investigation. See also their FAQ.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes - this is certainly not something I want to change in this PR :)

Was just thinking that jest was one of the main consumers of itsJustJavascript, and that's now gone.

Copy link
Member Author

Choose a reason for hiding this comment

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

Mostly whitespace changes in here (review with ?w=1), and then also some changes to how the mocking of googleapis works.

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 test suite I had to disable (using it.skip), because the mocking doesn't work: see PR description.

Comment on lines -10 to -12
// https://github.com/d3/d3-geo-projection/issues/202
// todo: would be nice to get propert types for this and not have to run different code during testing
const projectionToUseDuringJestTesting = geoConicConformal()
Copy link
Member Author

Choose a reason for hiding this comment

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

Could just get rid of this without any issues, so 🤷🏻

@owidbot
Copy link
Contributor

owidbot commented Mar 5, 2025

Quick links (staging server):

Site Dev Site Preview Admin Wizard Docs

Login: ssh owid@staging-site-vitest

SVG tester:

Number of differences (default views): 0 ✅
Number of differences (all views): 0 ✅

Edited: 2025-03-05 12:33:59 UTC
Execution time: 1.21 seconds

Copy link
Member Author

@marcelgerber marcelgerber Mar 5, 2025

Choose a reason for hiding this comment

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

For whatever reason, we had both a MarimekkoChart.jsdom.test.tsx file and a MarimekkoChart.test.tsx - and both of them neither required jsdom nor were they TSX 😬

So, I merged them into one single file. Nothing changed otherwise.

@@ -7,17 +11,16 @@ import {
slugify,
} from "@ourworldindata/utils"
import { fireEvent, render, screen } from "@testing-library/react"
import "@testing-library/jest-dom"
import "@testing-library/jest-dom/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.

Despite jest being in the name, this really is the way to setup testing-library for vitest.

@marcelgerber marcelgerber marked this pull request as ready for review March 5, 2025 12:37
@marcelgerber marcelgerber requested a review from rakyi March 5, 2025 12:37
Copy link
Contributor

@rakyi rakyi left a comment

Choose a reason for hiding this comment

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

Very cool. The future is bright!

Do you have a plan for what to do about the disabled test suites? I'm worried we might forget about them for a long time.

Could we schedule to fix them, e.g. for the next cooldown? Or should we consider deleting them instead, if they are not very valuable (I don't know whether they are).

Comment on lines 35 to 39
- name: Run tsc build
run: yarn buildTsc

- name: Run tsc buildTests
run: yarn lerna run buildTests
Copy link
Contributor

Choose a reason for hiding this comment

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

When I last looked into this, i.e. when I introduced tsx, the main reason to keep istJustJavascript was that tsx runs esbuild every time, so we pay the cost of the transpilation on every restart of the process, e.g. the admin server, and also I wasn't sure how much ongoing overhead it incurs during runtime.

I'm now more inclined to think it would be fine to use it in prod, but it warrants more investigation. See also their FAQ.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having a Vitest configuration shared across the team would be nice. Could we add one?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, sure, although I must say that I don't really use Launch configurations myself.
I'm also gonna share the vitest VS Code extension, which is really quite cool and useful!
But, feel free to add a launch config yourself, if you feel like it.

Copy link

Report too large to display inline

View full report↗︎

@marcelgerber
Copy link
Member Author

Do you have a plan for what to do about the disabled test suites? I'm worried we might forget about them for a long time.

Could we schedule to fix them, e.g. for the next cooldown? Or should we consider deleting them instead, if they are not very valuable (I don't know whether they are).

I actually could re-enable two of them, it was relatively easy to do for them.
For redirects I split the file straightforwardly into two files (one for logic and one for DB), and for pageOverrides I just mocked a method one step up the chain.

The only thing that's still non-trivial is Variable. I'll create a follow-up issue so we don't forget about it.

@marcelgerber
Copy link
Member Author

Thanks for your fast review, btw, really appreciate it!

@marcelgerber marcelgerber merged commit 83321cb into master Mar 5, 2025
21 of 22 checks passed
@marcelgerber marcelgerber deleted the vitest branch March 5, 2025 17:43
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