Skip to content

Commit

Permalink
test: migrate to vitest (renovatebot#34475)
Browse files Browse the repository at this point in the history
Co-authored-by: HonkingGoose <[email protected]>
  • Loading branch information
viceice and HonkingGoose authored Feb 26, 2025
1 parent 2315523 commit 997c235
Show file tree
Hide file tree
Showing 419 changed files with 4,217 additions and 2,458 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"orta.vscode-jest",
"vitest.explorer",
"editorconfig.editorconfig",
"github.vscode-github-actions"
]
Expand Down
10 changes: 5 additions & 5 deletions .github/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,31 @@ Use these commands to help run your tests:
- To run a single test folder, specify the path

```bash
pnpm jest platform/gitlab
pnpm vitest platform/gitlab
```

- To run against a single test file, specify down to the filename (suffix is not necessary)

```bash
pnpm jest platform/gitlab/index
pnpm vitest platform/gitlab/index
```

- To run a single test batch, the `-t` value must be part of the `describe` value of the test batch

```bash
pnpm jest platform/gitlab/index -t "getJsonFile"
pnpm vitest platform/gitlab/index -t "getJsonFile"
```

- To run a single test, the `-t` value must be part of the `it` value of the test batch

```bash
pnpm jest platform/gitlab/index -t "returns file content from given repo"
pnpm vitest platform/gitlab/index -t "returns file content from given repo"
```

And some options:

- `--verbose=false` to avoid the test list
- `--collectCoverage=false` to avoid collecting coverage, faster for the part you need the test to pass
- `--coverage=false` to avoid collecting coverage, faster for the part you need the test to pass

## Do not force push to your pull request branch

Expand Down
4 changes: 2 additions & 2 deletions .github/label-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
Hi there,
You're skipping code tests with `istanbul ignore`.
You're skipping code tests with `v8 ignore`.
Please only skip tests if:
Expand All @@ -260,7 +260,7 @@
```ts
// istanbul ignore next: typescript strict null check
// v8 ignore next: typescript strict null check
if (!url) {
return null;
}
Expand Down
17 changes: 8 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ jobs:
node-version: ${{ env.NODE_VERSION }}
os: ${{ runner.os }}

- name: Cache jest
- name: Cache vitest
uses: actions/cache@0c907a75c2c80ebcb7f088228285e798b750cf8f # v4.2.1
with:
path: .cache/jest
path: .cache/vitest
key: |
jest-cache-${{
vitest-cache-${{
runner.os
}}-${{
env.NODE_VERSION
Expand All @@ -398,8 +398,7 @@ jobs:
run: |
for shard in ${{ matrix.shards }};
do
TEST_SHARD="$shard" pnpm jest \
--ci \
TEST_SHARD="$shard" pnpm vitest \
--test-timeout ${{ matrix.test-timeout-milliseconds }} \
--coverage ${{ matrix.coverage }}
done
Expand Down Expand Up @@ -488,10 +487,10 @@ jobs:
- name: Check coverage threshold
run: |
pnpm nyc check-coverage -t ./coverage/nyc \
--branches 100 \
--functions 100 \
--lines 100 \
--statements 100
--branches 97 \
--functions 98 \
--lines 97 \
--statements 97
# Catch-all required check for test matrix and coverage
test-success:
Expand Down
2 changes: 1 addition & 1 deletion .ls-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ignore:
- .venv
- CODE_OF_CONDUCT.md
- dist
- jest.config.ts
- vitest.config.ts
- node_modules
- SECURITY.md
- test/e2e/node_modules
Expand Down
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"orta.vscode-jest",
"vitest.explorer",
"editorconfig.editorconfig",
"github.vscode-github-actions"
]
Expand Down
82 changes: 10 additions & 72 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,84 +29,22 @@
{
"type": "node",
"request": "launch",
"name": "Jest Current File",
"runtimeExecutable": "pnpm",
"program": "jest",
"name": "Debug Current Test File",
"autoAttachChildProcesses": true,
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
"args": [
"--runInBand",
"--collectCoverage=false",
"--testTimeout=100000000",
"--runTestsByPath",
"run",
"--no-file-parallelism",
"--no-coverage",
"--test-timeout=0",
"--hook-timeout=0",
"${relativeFile}"
],
"env": {
"NODE_ENV": "test",
"LOG_LEVEL": "debug"
},
"console": "integratedTerminal",
"runtimeArgs": ["--preserve-symlinks"],
"skipFiles": ["<node_internals>/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Jest All",
"runtimeExecutable": "pnpm",
"program": "jest",
"args": [
"--runInBand",
"--collectCoverage=false",
"--testTimeout=100000000"
],
"env": {
"NODE_ENV": "test",
"LOG_LEVEL": "debug"
},
"console": "integratedTerminal",
"runtimeArgs": ["--preserve-symlinks"],
"skipFiles": ["<node_internals>/**/*.js"]
},
{
"type": "node",
"request": "launch",
"name": "Jest Current Folder",
"runtimeExecutable": "pnpm",
"program": "jest",
"args": [
"--runInBand",
"--collectCoverage=false",
"--testTimeout=100000000",
"--roots=${workspaceFolder}/${relativeFileDirname}"
],
"console": "integratedTerminal",
"runtimeArgs": ["--preserve-symlinks"],
"skipFiles": ["<node_internals>/**/*.js"]
},
{
"type": "node",
"name": "vscode-jest-tests.v2",
"request": "launch",
"console": "integratedTerminal",
"internalConsoleOptions": "openOnSessionStart",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "node",
"runtimeArgs": ["--experimental-vm-modules"],
"program": "node_modules/jest/bin/jest.js",
"args": [
"--runInBand",
"--watchAll=false",
"--testTimeout=100000000",
"--coverage=false",
"--runTestsByPath",
"${jest.testFile}",
"--testNamePattern",
"${jest.testNamePattern}"
],
"env": {
"NODE_ENV": "test",
"LOG_LEVEL": "trace",
"GIT_ALLOW_PROTOCOL": "file"
}
"console": "integratedTerminal"
}
]
}
5 changes: 1 addition & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
".releaserc": "json"
},
"omnisharp.autoStart": false,
"jest.runMode": "on-demand",
"jest.jestCommandLine": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"jest.nodeEnv": {
"NODE_ENV": "test",
"vitest.nodeEnv": {
"LOG_LEVEL": "trace",
"GIT_ALLOW_PROTOCOL": "file"
},
Expand Down
14 changes: 7 additions & 7 deletions docs/development/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Some good branch names:

- `feat/13732-cacache-cleanup`
- `fix/15431-gitea-automerge-strategy`
- `refactor/jest-reset-mocks`
- `refactor/vitest-reset-mocks`
- `docs/rewrite-packageRules-section`

Avoid branch names like `patch-1`.
Expand All @@ -29,14 +29,14 @@ Read the [GitHub Docs, renaming a branch](https://docs.github.com/en/repositorie
- Prefer `interface` over `type` for TypeScript type declarations
- Avoid [Enums](https://github.com/renovatebot/renovate/issues/13743), use unions or [immutable objects](https://github.com/renovatebot/renovate/blob/5043379847818ac1fa71ff69c098451975e95710/lib/modules/versioning/pep440/range.ts#L8-L20) instead
- Always add unit tests for full code coverage
- Only use `istanbul` comments for unreachable code coverage that is needed for `codecov` completion
- Use descriptive `istanbul` comments
- Only use `v8` comments for unreachable code coverage that is needed for `codecov` completion
- Use descriptive `v8` comments
- Avoid cast or prefer `x as T` instead of `<T>x` cast
- Prefer `satisfies` operator over `as`, read the [TypeScript release notes for `satisfies` operator](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html#the-satisfies-operator) to learn more
- Avoid `Boolean` instead use `is` functions from `@sindresorhus/is` package, for example: `is.string`

```ts
// istanbul ignore next: can never happen
// v8 ignore next: can never happen
```

### Functions
Expand Down Expand Up @@ -280,12 +280,12 @@ if (end) {

- Separate the _Arrange_, _Act_ and _Assert_ phases with newlines
- Use `it.each` rather than `test.each`
- Prefer [Tagged Template Literal](https://jestjs.io/docs/api#2-testeachtablename-fn-timeout) style for `it.each`, Prettier will help with formatting
- Prefer [Tagged Template Literal](https://vitest.dev/api/#test-each) style for `it.each`, Prettier will help with formatting
- See [Example](https://github.com/renovatebot/renovate/blob/768e178419437a98f5ce4996bafd23f169e530b4/lib/modules/platform/util.spec.ts#L8-L18)
- Mock Date/Time when testing a Date/Time dependent module
- For `Luxon` mocking see [Example](https://github.com/renovatebot/renovate/blob/5043379847818ac1fa71ff69c098451975e95710/lib/modules/versioning/distro.spec.ts#L7-L10)
- Prefer `jest.spyOn` for mocking single functions, or mock entire modules
- Avoid overwriting functions, for example: (`func = jest.fn();`)
- Prefer `vi.spyOn` for mocking single functions, or mock entire modules
- Avoid overwriting functions, for example: (`func = vi.fn();`)
- Prefer `toEqual`
- Use `toMatchObject` for huge objects when only parts need to be tested
- Avoid `toMatchSnapshot`, only use it for:
Expand Down
2 changes: 1 addition & 1 deletion docs/development/issue-labeling.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Add a label `auto:logs` to indicate that there's a problem with the logs, and th

Add a label `auto:needs-details` to discussions which need more details to move forward.

Add a label `auto:no-coverage-ignore` if PR authors avoid needed unit tests by istanbul ignoring code with the `// istanbul ignore` comment.
Add a label `auto:no-coverage-ignore` if PR authors avoid needed unit tests by v8 ignoring code with the `// v8 ignore` comment.

Add a label `auto:no-done-comments` if PR authors unnecessary "Done" comments, or type comments to ask for a review instead of requesting a new review through GitHub's UI.

Expand Down
12 changes: 6 additions & 6 deletions docs/development/local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,21 @@ If this is working then in future you can create other test repos to verify your

You can run `pnpm test` locally to test your code.
We test all PRs using the same tests, run on GitHub Actions.
`pnpm test` runs an `eslint` check, a `prettier` check, a `type` check and then all the unit tests using `jest`.
`pnpm test` runs an `eslint` check, a `prettier` check, a `type` check and then all the unit tests using `vitest`.

Refactor PRs should ideally not change or remove tests (adding tests is OK).

### Jest
### Vitest

Run the Jest unit tests with the `pnpm jest` command.
You can also run a subset of the Jest tests using file matching, e.g. `pnpm jest composer` or `pnpm jest workers/repository/update/branch`.
Run the Vitest unit tests with the `pnpm vitest` command.
You can also run a subset of the Vitest tests using file matching, e.g. `pnpm vitest composer` or `pnpm vitest workers/repository/update/branch`.
If you get a test failure due to a "snapshot" mismatch, and you are sure that you need to update the snapshot, then you can append `-u` to the end.
e.g. `pnpm jest composer -u` would update the saved snapshots for _all_ tests in `**/composer/**`.
e.g. `pnpm vitest composer -u` would update the saved snapshots for _all_ tests in `**/composer/**`.

### Coverage

The Renovate project maintains 100% test coverage, so any Pull Request will fail if it does not have full coverage for code.
Using `// istanbul ignore` is not ideal, but can be a pragmatic solution if adding more tests wouldn't really prove anything.
Using `// v8 ignore` is not ideal, but can be a pragmatic solution if adding more tests wouldn't really prove anything.

To view the current test coverage locally, open up `coverage/index.html` in your browser.

Expand Down
33 changes: 19 additions & 14 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import eslintContainerbase from '@containerbase/eslint-plugin';
import js from '@eslint/js';
import eslintConfigPrettier from 'eslint-config-prettier';
import eslintPluginImport from 'eslint-plugin-import';
import jest from 'eslint-plugin-jest';
import vitest from '@vitest/eslint-plugin';
import eslintPluginPromise from 'eslint-plugin-promise';
import globals from 'globals';
import tseslint from 'typescript-eslint';
Expand Down Expand Up @@ -40,7 +40,6 @@ export default tseslint.config(
eslintPluginImport.flatConfigs.warnings,
eslintPluginImport.flatConfigs.recommended,
eslintPluginImport.flatConfigs.typescript,
jest.configs['flat/recommended'],
eslintPluginPromise.configs['flat/recommended'],
eslintContainerbase.configs.all,
{
Expand Down Expand Up @@ -88,7 +87,12 @@ export default tseslint.config(
'import/no-extraneous-dependencies': [
'error',
{
devDependencies: ['eslint.config.mjs', 'test/**/*', '**/*.spec.ts'],
devDependencies: [
'*.config.mjs',
'*.config.ts',
'test/**/*',
'**/*.spec.ts',
],
},
],

Expand All @@ -114,7 +118,7 @@ export default tseslint.config(
'import/no-unresolved': [
'error',
{
ignore: ['^mdast$'],
ignore: ['^mdast$', `^jest-mock-extended$`],
},
],

Expand Down Expand Up @@ -227,13 +231,22 @@ export default tseslint.config(
{
files: ['**/*.spec.ts', 'test/**'],

plugins: { vitest },

languageOptions: {
globals: {
...globals.jest,
...globals.vitest,
},
},

settings: {
vitest: {
typecheck: true,
},
},

rules: {
...vitest.configs.recommended.rules,
'no-template-curly-in-string': 0,
'prefer-destructuring': 0,
'prefer-promise-reject-errors': 0,
Expand All @@ -243,14 +256,6 @@ export default tseslint.config(
'@typescript-eslint/no-object-literal-type-assertion': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/unbound-method': 0,

'jest/valid-title': [
0,
{
ignoreTypeOfDescribeName: true,
},
],

'max-classes-per-file': 0,
'class-methods-use-this': 0,
},
Expand Down Expand Up @@ -304,7 +309,7 @@ export default tseslint.config(
languageOptions: {
globals: {
...Object.fromEntries(
Object.entries(globals.jest).map(([key]) => [key, 'off']),
Object.entries(globals.vitest).map(([key]) => [key, 'off']),
),
},
},
Expand Down
Loading

0 comments on commit 997c235

Please sign in to comment.