Skip to content

Commit 9f4ce5c

Browse files
committed
api-links: add test fixtures
Signed-off-by: flakey5 <[email protected]>
1 parent f51c583 commit 9f4ce5c

File tree

19 files changed

+168
-3
lines changed

19 files changed

+168
-3
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/generators/api-links/test/fixtures/

eslint.config.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,7 @@ export default [
4747
// @see https://eslint.org/docs/latest/rules to learn more about these rules
4848
pluginJs.configs.recommended,
4949
eslintConfigPrettier,
50+
{
51+
ignores: ['src/generators/api-links/test/fixtures/**'],
52+
},
5053
];

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"url": "git+https://github.com/nodejs/api-docs-tooling.git"
66
},
77
"scripts": {
8-
"lint": "eslint .",
9-
"lint:fix": "eslint --fix .",
8+
"lint": "eslint . --no-warn-ignored",
9+
"lint:fix": "eslint --fix . --no-warn-ignored",
1010
"format": "prettier .",
1111
"format:write": "prettier --write .",
1212
"test": "node --test",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { equal, notStrictEqual, ok } from 'node:assert';
2+
import { describe, it } from 'node:test';
3+
import { readFile, readdir } from 'node:fs/promises';
4+
import { basename, extname, join } from 'node:path';
5+
import astJs from '../../ast-js/index.mjs';
6+
import apiLinks from '../index.mjs';
7+
8+
const FIXTURES_DIRECTORY = join(import.meta.dirname, 'fixtures');
9+
const fixtures = await readdir(FIXTURES_DIRECTORY);
10+
11+
const sourceFiles = fixtures
12+
.filter(fixture => extname(fixture) === '.js')
13+
.map(fixture => join(FIXTURES_DIRECTORY, fixture));
14+
15+
describe('api links', () => {
16+
describe('should work correctly for all fixtures', () => {
17+
sourceFiles.forEach(sourceFile => {
18+
it(`${basename(sourceFile)}`, async () => {
19+
const astJsResult = await astJs.generate(undefined, {
20+
input: [sourceFile],
21+
});
22+
23+
const actualOutput = await apiLinks.generate(astJsResult, {});
24+
25+
const expectedOutput = JSON.parse(
26+
await readFile(sourceFile.replace('.js', '.json'), 'utf8')
27+
);
28+
29+
for (const [k, v] of Object.entries(expectedOutput)) {
30+
notStrictEqual(actualOutput[k], undefined, `missing ${k}`);
31+
ok(
32+
actualOutput[k].endsWith(`/${v}`),
33+
`expected ${v}, got ${actualOutput[k]}`
34+
);
35+
36+
delete actualOutput[k];
37+
}
38+
39+
equal(
40+
Object.keys(actualOutput).length,
41+
0,
42+
'actual output has extra keys'
43+
);
44+
});
45+
});
46+
});
47+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
// Buffer instance methods are exported as 'buf'.
4+
5+
function Buffer() {
6+
}
7+
8+
Buffer.prototype.instanceMethod = function() {}
9+
10+
module.exports = {
11+
Buffer
12+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"buffer.Buffer": "buffer.js#L5",
3+
"buf.instanceMethod": "buffer.js#L8"
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
// An exported class using ES2015 class syntax.
4+
5+
class Class {
6+
constructor() {};
7+
method() {};
8+
}
9+
10+
module.exports = {
11+
Class
12+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Class": "class.js#L5",
3+
"new Class": "class.js#L6",
4+
"class.method": "class.js#L7"
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// Support `exports` as an alternative to `module.exports`.
4+
5+
function Buffer() {};
6+
7+
exports.Buffer = Buffer;
8+
exports.fn1 = function fn1() {};
9+
10+
var fn2 = exports.fn2 = function() {};
11+
12+
function fn3() {};
13+
exports.fn3 = fn3;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"exports.Buffer": "exports.js#L5",
3+
"exports.fn1": "exports.js#L8",
4+
"exports.fn2": "exports.js#L10",
5+
"exports.fn3": "exports.js#L12"
6+
}

0 commit comments

Comments
 (0)