|
| 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 | +}); |
0 commit comments