Skip to content

Commit 8699aea

Browse files
api-links: add test fixtures (#211)
* api-links: add test fixtures Signed-off-by: flakey5 <[email protected]> * use snapshot api --------- Signed-off-by: flakey5 <[email protected]> Co-authored-by: Augustin Mauroy <[email protected]>
1 parent 36fe7a7 commit 8699aea

13 files changed

+176
-4
lines changed

Diff for: .prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Tests files
2+
src/generators/api-links/test/fixtures/
3+
*.snapshot

Diff for: eslint.config.mjs

+3
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
];

Diff for: package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
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",
13-
"test:watch": "node --test --watch",
1413
"test:coverage": "node --experimental-test-coverage --test",
14+
"test:update-snapshots": "node --test --test-update-snapshots",
15+
"test:watch": "node --test --watch",
1516
"prepare": "husky",
1617
"run": "node bin/cli.mjs",
1718
"watch": "node --watch bin/cli.mjs"

Diff for: src/generators/api-links/test/fixtures.test.mjs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, it } from 'node:test';
2+
import { readdir } from 'node:fs/promises';
3+
import { basename, extname, join } from 'node:path';
4+
import astJs from '../../ast-js/index.mjs';
5+
import apiLinks from '../index.mjs';
6+
7+
const FIXTURES_DIRECTORY = join(import.meta.dirname, 'fixtures');
8+
const fixtures = await readdir(FIXTURES_DIRECTORY);
9+
10+
const sourceFiles = fixtures
11+
.filter(fixture => extname(fixture) === '.js')
12+
.map(fixture => join(FIXTURES_DIRECTORY, fixture));
13+
14+
describe('api links', () => {
15+
describe('should work correctly for all fixtures', () => {
16+
sourceFiles.forEach(sourceFile => {
17+
it(`${basename(sourceFile)}`, async t => {
18+
const astJsResult = await astJs.generate(undefined, {
19+
input: [sourceFile],
20+
});
21+
22+
const actualOutput = await apiLinks.generate(astJsResult, {});
23+
24+
for (const [k, v] of Object.entries(actualOutput)) {
25+
actualOutput[k] = v.replace(/.*(?=lib\/)/, '');
26+
}
27+
28+
t.assert.snapshot(actualOutput);
29+
});
30+
});
31+
});
32+
});
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
exports[`api links > should work correctly for all fixtures > buffer.js 1`] = `
2+
{
3+
"buffer.Buffer": "lib/buffer.js#L5",
4+
"buf.instanceMethod": "lib/buffer.js#L8"
5+
}
6+
`;
7+
8+
exports[`api links > should work correctly for all fixtures > class.js 1`] = `
9+
{
10+
"Class": "lib/class.js#L5",
11+
"new Class": "lib/class.js#L6",
12+
"class.method": "lib/class.js#L7"
13+
}
14+
`;
15+
16+
exports[`api links > should work correctly for all fixtures > exports.js 1`] = `
17+
{
18+
"exports.fn1": "lib/exports.js#L8",
19+
"exports.fn2": "lib/exports.js#L10",
20+
"exports.Buffer": "lib/exports.js#L5",
21+
"exports.fn3": "lib/exports.js#L12"
22+
}
23+
`;
24+
25+
exports[`api links > should work correctly for all fixtures > mod.js 1`] = `
26+
{
27+
"mod.foo": "lib/mod.js#L5"
28+
}
29+
`;
30+
31+
exports[`api links > should work correctly for all fixtures > prototype.js 1`] = `
32+
{
33+
"prototype.Class": "lib/prototype.js#L5",
34+
"Class.classMethod": "lib/prototype.js#L8",
35+
"class.instanceMethod": "lib/prototype.js#L9"
36+
}
37+
`;
38+
39+
exports[`api links > should work correctly for all fixtures > reverse.js 1`] = `
40+
{
41+
"asserts": "lib/reverse.js#L8",
42+
"asserts.ok": "lib/reverse.js#L5",
43+
"asserts.strictEqual": "lib/reverse.js#L12"
44+
}
45+
`;
46+
47+
exports[`api links > should work correctly for all fixtures > root.js 1`] = `
48+
{}
49+
`;

Diff for: src/generators/api-links/test/fixtures/buffer.js

+12
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+
};

Diff for: src/generators/api-links/test/fixtures/class.js

+12
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+
};

Diff for: src/generators/api-links/test/fixtures/exports.js

+13
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;

Diff for: src/generators/api-links/test/fixtures/mod.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
// A module may export one or more methods.
4+
5+
function foo() {
6+
}
7+
8+
9+
module.exports = {
10+
foo
11+
};

Diff for: src/generators/api-links/test/fixtures/prototype.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// An exported class using classic prototype syntax.
4+
5+
function Class() {
6+
}
7+
8+
Class.classMethod = function() {}
9+
Class.prototype.instanceMethod = function() {}
10+
11+
module.exports = {
12+
Class
13+
};

Diff for: src/generators/api-links/test/fixtures/reverse.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// Parallel assignment to the exported variable and module.exports.
4+
5+
function ok() {
6+
}
7+
8+
const asserts = module.exports = ok;
9+
10+
asserts.ok = ok;
11+
12+
asserts.strictEqual = function() {
13+
}

Diff for: src/generators/api-links/test/fixtures/root.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
// Set root member
4+
let foo = true;
5+
foo = false;
6+
7+
// Return outside of function
8+
if (!foo) {
9+
return;
10+
}

Diff for: src/generators/api-links/utils/extractExports.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function handleVariableDeclaration(node, basename, nameToLineNumberMap) {
179179
switch (lhs.object.name) {
180180
case 'exports': {
181181
nameToLineNumberMap[`${basename}.${lhs.property.name}`] =
182-
node.start.line;
182+
node.loc.start.line;
183183

184184
break;
185185
}

0 commit comments

Comments
 (0)