Skip to content

Commit 3e912e7

Browse files
committed
test: create rules tests
1 parent a2dae43 commit 3e912e7

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Noop function.
3+
*
4+
* @returns {any}
5+
*/
6+
const noop = () => {};
7+
8+
/**
9+
* @type {ApiDocMetadataEntry}
10+
*/
11+
export const assertEntry = {
12+
api: 'assert',
13+
slug: 'assert',
14+
source_link: 'lib/assert.js',
15+
api_doc_source: 'doc/api/assert.md',
16+
added_in: undefined,
17+
deprecated_in: undefined,
18+
removed_in: undefined,
19+
n_api_version: undefined,
20+
changes: [
21+
{
22+
version: 'v9.9.0',
23+
'pr-url': 'https://github.com/nodejs/node/pull/17615',
24+
description: 'Added error diffs to the strict assertion mode.',
25+
},
26+
{
27+
version: 'v9.9.0',
28+
'pr-url': 'https://github.com/nodejs/node/pull/17002',
29+
description: 'Added strict assertion mode to the assert module.',
30+
},
31+
{
32+
version: ['v13.9.0', 'v12.16.2'],
33+
'pr-url': 'https://github.com/nodejs/node/pull/31635',
34+
description:
35+
'Changed "strict mode" to "strict assertion mode" and "legacy mode" to "legacy assertion mode" to avoid confusion with the more usual meaning of "strict mode".',
36+
},
37+
{
38+
version: 'v15.0.0',
39+
'pr-url': 'https://github.com/nodejs/node/pull/34001',
40+
description: "Exposed as `require('node:assert/strict')`.",
41+
},
42+
],
43+
heading: {
44+
type: 'heading',
45+
depth: 1,
46+
children: [
47+
{
48+
type: 'text',
49+
value: 'Assert',
50+
position: {
51+
start: { line: 1, column: 3, offset: 2 },
52+
end: { line: 1, column: 9, offset: 8 },
53+
},
54+
},
55+
],
56+
position: {
57+
start: { line: 1, column: 1, offset: 0 },
58+
end: { line: 1, column: 9, offset: 8 },
59+
},
60+
data: {
61+
text: 'Assert',
62+
name: 'Assert',
63+
depth: 1,
64+
slug: 'assert',
65+
type: 'property',
66+
},
67+
toJSON: noop,
68+
},
69+
stability: {
70+
type: 'root',
71+
children: [],
72+
toJSON: noop,
73+
},
74+
content: {
75+
type: 'root',
76+
children: [],
77+
},
78+
tags: [],
79+
introduced_in: 'v0.1.21',
80+
yaml_position: {
81+
start: { line: 7, column: 1, offset: 103 },
82+
end: { line: 7, column: 35, offset: 137 },
83+
},
84+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, it } from 'node:test';
2+
import { invalidChangeVersion } from '../../rules/invalid-change-version.mjs';
3+
import { deepEqual } from 'node:assert';
4+
import { assertEntry } from '../fixtures/entries.mjs';
5+
6+
describe('invalidChangeVersion', () => {
7+
it('should return an empty array if all change versions are valid', () => {
8+
const issues = invalidChangeVersion(assertEntry);
9+
10+
deepEqual(issues, []);
11+
});
12+
13+
it('should return an issue if a change version is invalid', () => {
14+
const issues = invalidChangeVersion({
15+
...assertEntry,
16+
changes: [...assertEntry.changes, { version: ['v13.9.0', 'REPLACEME'] }],
17+
});
18+
19+
deepEqual(issues, [
20+
{
21+
level: 'warn',
22+
location: {
23+
path: 'doc/api/assert.md',
24+
position: {
25+
end: {
26+
column: 35,
27+
line: 7,
28+
offset: 137,
29+
},
30+
start: {
31+
column: 1,
32+
line: 7,
33+
offset: 103,
34+
},
35+
},
36+
},
37+
message: 'Invalid version number: REPLACEME',
38+
},
39+
]);
40+
});
41+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, it } from 'node:test';
2+
import { deepEqual } from 'node:assert';
3+
import { missingChangeVersion } from '../../rules/missing-change-version.mjs';
4+
import { assertEntry } from '../fixtures/entries.mjs';
5+
6+
describe('missingChangeVersion', () => {
7+
it('should return an empty array if all change versions are non-empty', () => {
8+
const issues = missingChangeVersion(assertEntry);
9+
10+
deepEqual(issues, []);
11+
});
12+
13+
it('should return an issue if a change version is missing', () => {
14+
const issues = missingChangeVersion({
15+
...assertEntry,
16+
changes: [...assertEntry.changes, { version: undefined }],
17+
});
18+
19+
deepEqual(issues, [
20+
{
21+
level: 'warn',
22+
location: {
23+
column: 1,
24+
line: 7,
25+
path: 'doc/api/assert.md',
26+
},
27+
message: 'Missing change version',
28+
},
29+
]);
30+
});
31+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, it } from 'node:test';
2+
import { missingIntroducedIn } from '../../rules/missing-introduced-in.mjs';
3+
import { deepEqual } from 'assert';
4+
import { assertEntry } from '../fixtures/entries.mjs';
5+
6+
describe('missingIntroducedIn', () => {
7+
it('should return an empty array if the introduced_in field is not missing', () => {
8+
const issues = missingIntroducedIn(assertEntry);
9+
10+
deepEqual(issues, []);
11+
});
12+
13+
it('should return an empty array if the heading depth is not equal to 1', () => {
14+
const issues = missingIntroducedIn({
15+
...assertEntry,
16+
heading: { ...assertEntry.heading, depth: 2 },
17+
});
18+
19+
deepEqual(issues, []);
20+
});
21+
22+
it('should return an issue if the introduced_in property is missing', () => {
23+
const issues = missingIntroducedIn({
24+
...assertEntry,
25+
introduced_in: undefined,
26+
});
27+
28+
deepEqual(issues, [
29+
{
30+
level: 'info',
31+
location: {
32+
path: 'doc/api/assert.md',
33+
},
34+
message: "Missing 'introduced_in' field in the API doc entry",
35+
},
36+
]);
37+
});
38+
});

0 commit comments

Comments
 (0)