Skip to content

Commit 7e82d31

Browse files
authored
Add Vitest config (#373)
* Add Vitest config * Update test
1 parent ff9780d commit 7e82d31

File tree

11 files changed

+324
-13
lines changed

11 files changed

+324
-13
lines changed

eslint.config.mjs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// @ts-check
22

33
import base from '@metamask/eslint-config';
4-
import jest from '@metamask/eslint-config-jest';
54
import nodejs from '@metamask/eslint-config-nodejs';
65
import typescript from '@metamask/eslint-config-typescript';
6+
import vitest from '@metamask/eslint-config-vitest';
77
// eslint-disable-next-line import-x/no-unresolved
88
import tseslint from 'typescript-eslint';
99

@@ -29,15 +29,7 @@ const config = tseslint.config(
2929

3030
{
3131
files: ['**/*.test.mjs'],
32-
extends: jest,
33-
rules: {
34-
'no-shadow': [
35-
'error',
36-
{
37-
allow: ['describe', 'it', 'expect'],
38-
},
39-
],
40-
},
32+
extends: vitest,
4133
},
4234

4335
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
"@lavamoat/allow-scripts": "^3.0.4",
2727
"@metamask/auto-changelog": "^3.4.4",
2828
"@metamask/eslint-config": "^13.0.0",
29-
"@metamask/eslint-config-jest": "workspace:^",
3029
"@metamask/eslint-config-nodejs": "^13.0.0",
3130
"@metamask/eslint-config-typescript": "workspace:^",
31+
"@metamask/eslint-config-vitest": "workspace:^",
3232
"@metamask/utils": "^9.1.0",
3333
"@types/eslint__js": "^8.42.3",
3434
"@types/node": "^22.5.5",

packages/vitest/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
[Unreleased]: https://github.com/MetaMask/eslint-config/

packages/vitest/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 MetaMask
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/vitest/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# `@metamask/eslint-config-vitest`
2+
3+
MetaMask's [Vitest](https://vitest.dev/) ESLint configuration.
4+
5+
## Usage
6+
7+
```bash
8+
yarn add --dev \
9+
@metamask/eslint-config@^13.0.0 \
10+
@metamask/eslint-config-vitest@^0.0.0 \
11+
@vitest/eslint-plugin@^1.1.4 \
12+
eslint@^9.11.0 \
13+
eslint-config-prettier@^9.1.0 \
14+
eslint-plugin-import-x@^4.3.0 \
15+
eslint-plugin-jsdoc@^50.2.4 \
16+
eslint-plugin-prettier@^5.2.1 \
17+
eslint-plugin-promise@^7.1.0 \
18+
prettier@^3.3.3
19+
```
20+
21+
The order in which you extend ESLint rules matters.
22+
The `@metamask/*` eslint configs should be added to the config array _last_,
23+
with `@metamask/eslint-config` first, and `@metamask/eslint-config-*` in any
24+
order thereafter.
25+
26+
```js
27+
import base from '@metamask/eslint-config';
28+
import vitest from '@metamask/eslint-config-vitest';
29+
30+
const config = {
31+
// Any custom shared config should be added here.
32+
// ...
33+
34+
// This should be added last unless you know what you're doing.
35+
...base,
36+
...vitest,
37+
38+
{
39+
// Your overrides here.
40+
}
41+
};
42+
```

packages/vitest/package.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
"name": "@metamask/eslint-config-vitest",
3+
"version": "0.0.0",
4+
"description": "Shareable MetaMask ESLint config for Vitest.",
5+
"homepage": "https://github.com/MetaMask/eslint-config#readme",
6+
"bugs": {
7+
"url": "https://github.com/MetaMask/eslint-config/issues"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "https://github.com/MetaMask/eslint-config.git"
12+
},
13+
"license": "MIT",
14+
"type": "module",
15+
"exports": {
16+
".": {
17+
"import": {
18+
"types": "./src/index.d.mts",
19+
"default": "./src/index.mjs"
20+
}
21+
}
22+
},
23+
"main": "./src/index.mjs",
24+
"types": "./src/index.d.mts",
25+
"files": [
26+
"src/",
27+
"!src/**/*.test.mjs"
28+
],
29+
"scripts": {
30+
"lint:changelog": "auto-changelog validate",
31+
"publish": "npm publish",
32+
"test": "eslint ."
33+
},
34+
"dependencies": {
35+
"@eslint/js": "^9.11.0",
36+
"globals": "^15.9.0"
37+
},
38+
"devDependencies": {
39+
"@jest/globals": "^29.7.0",
40+
"@metamask/auto-changelog": "^3.4.4",
41+
"@metamask/eslint-config": "^13.0.0",
42+
"@vitest/eslint-plugin": "^1.1.4",
43+
"eslint": "^9.11.0",
44+
"eslint-config-prettier": "^9.1.0",
45+
"eslint-plugin-import-x": "^4.3.0",
46+
"eslint-plugin-jsdoc": "^50.2.4",
47+
"eslint-plugin-prettier": "^5.2.1",
48+
"jest": "^29.7.0",
49+
"prettier": "^3.3.3",
50+
"vitest": "^2.1.1"
51+
},
52+
"peerDependencies": {
53+
"@metamask/eslint-config": "^13.0.0",
54+
"@vitest/eslint-plugin": "^1.1.4",
55+
"eslint": "^9.11.0"
56+
},
57+
"engines": {
58+
"node": "^18.18 || >=20"
59+
},
60+
"publishConfig": {
61+
"access": "public",
62+
"registry": "https://registry.npmjs.org/"
63+
}
64+
}

packages/vitest/rules-snapshot.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"vitest/consistent-test-it": ["error", { "fn": "it" }],
3+
"vitest/expect-expect": "error",
4+
"vitest/no-alias-methods": "error",
5+
"vitest/no-commented-out-tests": "error",
6+
"vitest/no-conditional-expect": "error",
7+
"vitest/no-conditional-in-test": "error",
8+
"vitest/no-disabled-tests": "error",
9+
"vitest/no-duplicate-hooks": "error",
10+
"vitest/no-focused-tests": "error",
11+
"vitest/no-identical-title": "error",
12+
"vitest/no-import-node-test": "error",
13+
"vitest/no-interpolation-in-snapshots": "error",
14+
"vitest/no-mocks-import": "error",
15+
"vitest/no-restricted-matchers": [
16+
"error",
17+
{
18+
"resolves": "Use `expect(await promise)` instead.",
19+
"toBeFalsy": "Avoid `toBeFalsy`.",
20+
"toBeTruthy": "Avoid `toBeTruthy`.",
21+
"toMatchSnapshot": "Use `toMatchInlineSnapshot()` instead.",
22+
"toThrowErrorMatchingSnapshot": "Use `toThrowErrorMatchingInlineSnapshot()` instead."
23+
}
24+
],
25+
"vitest/no-standalone-expect": "error",
26+
"vitest/no-test-prefixes": "error",
27+
"vitest/no-test-return-statement": "error",
28+
"vitest/prefer-hooks-on-top": "error",
29+
"vitest/prefer-lowercase-title": ["error", { "ignore": ["describe"] }],
30+
"vitest/prefer-spy-on": "error",
31+
"vitest/prefer-strict-equal": "error",
32+
"vitest/prefer-to-be": "error",
33+
"vitest/prefer-to-contain": "error",
34+
"vitest/prefer-to-have-length": "error",
35+
"vitest/prefer-todo": "error",
36+
"vitest/require-local-test-context-for-concurrent-snapshots": "error",
37+
"vitest/require-to-throw-message": "error",
38+
"vitest/require-top-level-describe": "error",
39+
"vitest/valid-describe-callback": "error",
40+
"vitest/valid-expect": ["error", { "alwaysAwait": true }],
41+
"vitest/valid-title": "error"
42+
}

packages/vitest/src/index.d.mts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare module '@metamask/eslint-config-vitest' {
2+
import type { Linter } from 'eslint';
3+
4+
const config: Linter.Config[];
5+
export default config;
6+
}

packages/vitest/src/index.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import vitest from '@vitest/eslint-plugin';
2+
3+
/**
4+
* @type {import('eslint').Linter.Config[]}
5+
*/
6+
const config = [
7+
vitest.configs.recommended,
8+
9+
{
10+
name: '@metamask/eslint-config-vitest',
11+
12+
files: [
13+
'**/*.test.js',
14+
'**/*.spec.js',
15+
'**/*.test.mjs',
16+
'**/*.spec.mjs',
17+
'**/*.test.cjs',
18+
'**/*.spec.cjs',
19+
'**/*.test.ts',
20+
'**/*.spec.ts',
21+
'**/*.test.tsx',
22+
'**/*.spec.tsx',
23+
'**/*.test.mts',
24+
'**/*.spec.mts',
25+
'**/*.test.cts',
26+
'**/*.spec.cts',
27+
'**/*.test.mtsx',
28+
'**/*.spec.mtsx',
29+
'**/*.test.ctsx',
30+
'**/*.spec.ctsx',
31+
],
32+
33+
rules: {
34+
'vitest/consistent-test-it': ['error', { fn: 'it' }],
35+
'vitest/no-alias-methods': 'error',
36+
'vitest/no-commented-out-tests': 'error',
37+
'vitest/no-conditional-expect': 'error',
38+
'vitest/no-conditional-in-test': 'error',
39+
'vitest/no-disabled-tests': 'error',
40+
'vitest/no-duplicate-hooks': 'error',
41+
'vitest/no-focused-tests': 'error',
42+
'vitest/no-interpolation-in-snapshots': 'error',
43+
'vitest/no-mocks-import': 'error',
44+
'vitest/no-standalone-expect': 'error',
45+
'vitest/no-test-prefixes': 'error',
46+
'vitest/no-test-return-statement': 'error',
47+
'vitest/prefer-hooks-on-top': 'error',
48+
'vitest/prefer-lowercase-title': ['error', { ignore: ['describe'] }],
49+
'vitest/prefer-spy-on': 'error',
50+
'vitest/prefer-strict-equal': 'error',
51+
'vitest/prefer-to-be': 'error',
52+
'vitest/prefer-to-contain': 'error',
53+
'vitest/prefer-to-have-length': 'error',
54+
'vitest/prefer-todo': 'error',
55+
'vitest/require-to-throw-message': 'error',
56+
'vitest/require-top-level-describe': 'error',
57+
'vitest/valid-expect': ['error', { alwaysAwait: true }],
58+
'vitest/no-restricted-matchers': [
59+
'error',
60+
{
61+
resolves: 'Use `expect(await promise)` instead.',
62+
toBeFalsy: 'Avoid `toBeFalsy`.',
63+
toBeTruthy: 'Avoid `toBeTruthy`.',
64+
toMatchSnapshot: 'Use `toMatchInlineSnapshot()` instead.',
65+
toThrowErrorMatchingSnapshot:
66+
'Use `toThrowErrorMatchingInlineSnapshot()` instead.',
67+
},
68+
],
69+
},
70+
},
71+
];
72+
73+
export default config;

packages/vitest/src/index.test.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ESLint } from 'eslint';
2+
import { describe, it, expect } from 'vitest';
3+
4+
import config from '.';
5+
6+
describe('index', () => {
7+
it('is a valid ESLint config', async () => {
8+
const api = new ESLint({
9+
baseConfig: config,
10+
});
11+
12+
const result = await api.lintText(`export {};\n`);
13+
14+
expect(result[0].messages).toStrictEqual([]);
15+
expect(result[0].warningCount).toBe(0);
16+
expect(result[0].errorCount).toBe(0);
17+
});
18+
});

0 commit comments

Comments
 (0)