Skip to content

Commit b3567df

Browse files
committed
Add tests.
- Use vitest. - Add tests to CI workflow. - Add `catch-spacing` rule tests. - Start adding tests taken from `test/index.js`.
1 parent e05c289 commit b3567df

File tree

6 files changed

+185
-2
lines changed

6 files changed

+185
-2
lines changed

.github/workflows/main.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ jobs:
2222
- run: npm install
2323
- name: Run eslint
2424
run: npm run lint
25+
test:
26+
runs-on: ubuntu-latest
27+
timeout-minutes: 10
28+
strategy:
29+
matrix:
30+
node-version: [24.x]
31+
steps:
32+
- uses: actions/checkout@v4
33+
with:
34+
persist-credentials: false
35+
- name: Use Node.js ${{ matrix.node-version }}
36+
uses: actions/setup-node@v4
37+
with:
38+
node-version: ${{ matrix.node-version }}
39+
- run: npm install
40+
- run: npm run test

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"./examples/**/*.js"
2222
],
2323
"scripts": {
24-
"test": "eslint",
24+
"test": "vitest",
2525
"lint": "eslint --ignore-pattern 'test/**/*.js'"
2626
},
2727
"repository": {
@@ -54,7 +54,8 @@
5454
"devDependencies": {
5555
"eslint": "^9.38.0",
5656
"eslint-plugin-eslint-plugin": "^7.0.0",
57-
"eslint-plugin-import": "^2.32.0"
57+
"eslint-plugin-import": "^2.32.0",
58+
"vitest": "^4.0.18"
5859
},
5960
"peerDependencies": {
6061
"eslint": "^9.38.0"

test/000-rules.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Test digitalbazaar eslint rules.
3+
*/
4+
import catchSpacing from '../rules/catch-spacing.js';
5+
import {RuleTester} from 'eslint';
6+
7+
const ruleTester = new RuleTester({
8+
languageOptions: {
9+
ecmaVersion: 2022,
10+
sourceType: 'module'
11+
}
12+
});
13+
14+
ruleTester.run('catch-spacing', catchSpacing, {
15+
valid: [
16+
{
17+
code: 'try {} catch(e) {}'
18+
},
19+
{
20+
code: 'try {} catch {}'
21+
}
22+
],
23+
invalid: [
24+
{
25+
code: 'try {} catch (e) {}',
26+
errors: [{messageId: 'unexpectedSpace'}],
27+
output: 'try {} catch(e) {}'
28+
},
29+
{
30+
code: 'try {} catch (e){}',
31+
errors: [{messageId: 'unexpectedSpace'}],
32+
output: 'try {} catch(e){}'
33+
},
34+
{
35+
code: 'try {} catch{}',
36+
errors: [{messageId: 'missingSpace'}],
37+
output: 'try {} catch {}'
38+
}
39+
]
40+
});

test/001-config.test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Test digitalbazaar eslint configs.
3+
*/
4+
import {describe, expect, it} from 'vitest';
5+
import {ESLint} from 'eslint';
6+
import {join} from 'node:path';
7+
8+
describe('@digitalbazaar/eslint-config base', () => {
9+
const eslint = new ESLint({
10+
overrideConfigFile: join(import.meta.dirname, '../configs/base.js')
11+
});
12+
13+
it('incorrect import sort all', async () => {
14+
const results = await eslint.lintText(`
15+
// incorrect all
16+
import * as bAll from 'b.js';
17+
import * as aAll from 'a.js';
18+
`);
19+
const messages = results[0].messages;
20+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(true);
21+
});
22+
23+
it('correct import sort all', async () => {
24+
const results = await eslint.lintText(`
25+
// correct all
26+
import * as aaAll from 'a.js';
27+
import * as bbAll from 'b.js';
28+
`);
29+
const messages = results[0].messages;
30+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(false);
31+
});
32+
33+
it('incorrect import sort multiple', async () => {
34+
const results = await eslint.lintText(`
35+
// incorrect multiple
36+
import {zed, rat, girl} from 'baboon2000.js';
37+
`);
38+
const messages = results[0].messages;
39+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(true);
40+
});
41+
42+
it('correct import sort multiple', async () => {
43+
const results = await eslint.lintText(`
44+
// correct multiple
45+
import {girl2, rat2, zed2} from 'baboon2000.js';
46+
`);
47+
const messages = results[0].messages;
48+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(false);
49+
});
50+
51+
it('incorrect import sort single', async () => {
52+
const results = await eslint.lintText(`
53+
// incorrect single
54+
import {default1} from 'defaulter';
55+
import aDefault1 from 'adefaulter';
56+
`);
57+
const messages = results[0].messages;
58+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(true);
59+
});
60+
61+
it('correct import sort single', async () => {
62+
const results = await eslint.lintText(`
63+
// correct single
64+
import aDefault2 from 'adefaulter';
65+
import {default2} from 'defaulter';
66+
`);
67+
const messages = results[0].messages;
68+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(false);
69+
});
70+
71+
it('incorrect import sort no members', async () => {
72+
const results = await eslint.lintText(`
73+
// imports with no members must go last
74+
import 'no-members';
75+
import * as baboon1 from 'baboon-lib-1';
76+
`);
77+
const messages = results[0].messages;
78+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(true);
79+
});
80+
81+
it('correct import sort no members', async () => {
82+
const results = await eslint.lintText(`
83+
// imports with no members must go last
84+
import * as baboon2 from 'baboon-lib-1';
85+
import 'no-members';
86+
`);
87+
const messages = results[0].messages;
88+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(false);
89+
});
90+
91+
it('correct import sort mixed', async () => {
92+
const results = await eslint.lintText(`
93+
/*
94+
* import * as all goes first
95+
* import {multiple, members} goes second
96+
* import singleMember goes third
97+
* import {singleMember} goes third
98+
* import 'no-members' goes last
99+
*/
100+
import * as baboon3 from 'babbon-lib-3';
101+
import {multi1, multi2, multi3} from 'multi-x-3';
102+
import single3 from 'has-default-export';
103+
import {single4} from 'has-single-named-export';
104+
import 'no-export';
105+
`);
106+
const messages = results[0].messages;
107+
expect(messages.some(m => m.ruleId === 'sort-imports')).toBe(false);
108+
});
109+
110+
// see more examples in index.js
111+
});

vitest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {defineConfig} from 'vitest/config';
2+
3+
export default defineConfig({
4+
test: {
5+
setupFiles: ['./vitest.setup.js']
6+
}
7+
});

vitest.setup.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {RuleTester} from 'eslint';
2+
import {afterAll, describe, it} from 'vitest';
3+
4+
// Assign Vitest's functions to RuleTester's static properties
5+
RuleTester.afterAll = afterAll;
6+
RuleTester.describe = describe;
7+
RuleTester.it = it;
8+
RuleTester.itOnly = it.only;

0 commit comments

Comments
 (0)