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

Spike/scripts jest tests #2376

Merged
merged 14 commits into from
Aug 29, 2024
Merged

Spike/scripts jest tests #2376

merged 14 commits into from
Aug 29, 2024

Conversation

chrismclarke
Copy link
Member

@chrismclarke chrismclarke commented Aug 25, 2024

PR Checklist

  • PR title descriptive (can be used in release notes)

Description

The core repo has quite specific requirement for Node >=18 and <=20.7.0 due to issues with tests outlined in #2185 and #2372.

Attempts had been made with #2186 to fix the implementation of mock-fs, however getting any solution working within the current testing infrastructure was proving difficult.

This PR instead aims to replace the core testing infrastructure (based on Jasmine) with Jest, which in turn has better support for mocking the methods that need replacement.

Main Changes

  • Remove jasmine from the scripts test workspace and replace with jasmine
  • Refactor test logging methods to better support parallel operations
  • Fix broken tests
  • Replace broken mock-fs with memfs
  • Add lint rules for test best practices (displayed in code editor but not run on CI)

Review Notes

The changes are pretty hard to follow as there's a mix of find/replace and bits of refactoring. Ultimately the main thing to check is that the test suite still runs and passes locally.

yarn workspace scripts test

There shouldn't really be any scope for knock-ons to the main scripts themselves or any apps

Dev Notes

Jest was selected as a successor to Jasmine as ultimately that is the direction the angular team say that they are headed in (https://blog.angular.dev/moving-angular-cli-to-jest-and-web-test-runner-ef85ef69ceca) and also significantly the most popular tools used nowadays (Jasmine is no longer even tracked in stateofjs surveys, and gets mentioned by 16% of respondents in the additional section)

image

Not a huge amount should be different from a test-authoring perspective, focused tests are now defined by it.only(...) instead of fit(...) but most spies and mocks have similar syntax. It is also possible to only run tests on a single file by name, e.g. yarn workspace scripts test -t base.spec.ts (comments have been added to a handful of files to indicate).

One key difference from a developer perspective is that by default jest runs tests in parallel. This had a number of breaking changes for a number of our tests that read/write and clear local test file folders and log files. I started to refactor tests to work around this (using virtual file systems where possible, in-memory logging options and various other fixes), although eventually realised you could pass a runInBand flag to jest to run sequentially rather than in parallel. Interestingly enough this also speeds up the test suite by around 50% (a lot of duplicate cache data gets moved across parallel processes), so I think it's fine to just keep this way.

As for virtual filesystems, mock-fs has been replaced by memfs. The syntax is slightly different so see the updated examples in the code

Future TODOs

Git Issues

Closes #2186

Screenshots/Videos

Test suites passing with Jest Runner on Node 20.17.0
image

Copy link
Collaborator

@jfmcquade jfmcquade 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 from the PR description, and I can confirm that the tests run as expected for me.

Screenshot 2024-08-27 at 17 24 17

I can't speak to the details of the code changes (or the extent of work that ended up being required!), but happy to merge.

Not sure of the best way to track the future TODOs. I've broken out one into #2378 and assigned myself as I think it'd be sensible to acquaint myself with the changes.

@chrismclarke
Copy link
Member Author

Thanks @jfmcquade

Thanks for adding the follow-up issue, for the remaining tasks:

Consider refactor app tests to use Jest
As this depends on future angular updates (and will likely be promoted when ready) I think we can just leave to resurface organically when angular starts to push more in that direction.

Start testing support for Node 22
Will likely start happening anyway, I'll plan to swap my go-to dev environment to Node 22 later this year and document any issues I come up against, so again no need to create standalone issue.

Update docs, check nvmrc and node switching hooks
I'll do this now and open a follow-up PR. Probably won't dive too much into nvmrc/husky hooks as the use depends a lot on OS used and node tooling (e.g. nvm vs fnm on windows/mac all behave differently), so can probably just update as specific developers request and raise PRs

@chrismclarke chrismclarke merged commit 20f3d16 into master Aug 29, 2024
6 checks passed
@chrismclarke chrismclarke deleted the spike/scripts-jest-tests branch August 29, 2024 16:01
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.

2 participants