From 9cda439fefa3775e1919ac2f81e2e9d7da88471c Mon Sep 17 00:00:00 2001 From: avivkeller Date: Wed, 4 Jun 2025 17:04:39 -0400 Subject: [PATCH 1/2] chore(test): organize test folders --- .prettierignore | 2 +- eslint.config.mjs | 2 +- src/{test => __tests__}/metadata.test.mjs | 0 .../{test => __tests__}/fixtures.test.mjs | 0 .../fixtures.test.mjs.snapshot | 0 .../{test => __tests__}/fixtures/buffer.js | 0 .../{test => __tests__}/fixtures/class.js | 0 .../{test => __tests__}/fixtures/exports.js | 0 .../{test => __tests__}/fixtures/mod.js | 6 +- .../{test => __tests__}/fixtures/prototype.js | 0 .../{test => __tests__}/fixtures/reverse.js | 0 .../{test => __tests__}/fixtures/root.js | 0 src/generators/jsx-ast/test/utils.test.mjs | 82 ------------------- .../jsx-ast/utils/__tests__/ast.test.mjs | 20 +++++ .../utils/__tests__/buildBarProps.test.mjs | 31 +++++++ .../utils/__tests__/buildContent.test.mjs | 26 ++++++ .../jsx-ast/utils/__tests__/utils.mjs | 22 +++++ .../utils/__tests__/converter.test.mjs} | 2 +- .../fixtures/descriptors.mjs | 0 .../{tests => __tests__}/fixtures/issues.mjs | 0 .../{tests => __tests__}/fixtures/tree.mjs | 0 .../{tests => __tests__}/index.test.mjs | 0 .../__tests__}/console.test.mjs | 14 ++-- .../__tests__}/github.test.mjs | 14 ++-- .../duplicate-stability-nodes.test.mjs | 2 +- .../invalid-change-version-subprocess.mjs} | 2 +- .../invalid-change-version.test.mjs | 4 +- .../__tests__}/missing-introduced-in.test.mjs | 0 src/utils/{tests => __tests__}/unist.test.mjs | 0 .../__tests__/index.test.mjs} | 2 +- .../__tests__/index.test.mjs} | 2 +- 31 files changed, 128 insertions(+), 105 deletions(-) rename src/{test => __tests__}/metadata.test.mjs (100%) rename src/generators/api-links/{test => __tests__}/fixtures.test.mjs (100%) rename src/generators/api-links/{test => __tests__}/fixtures.test.mjs.snapshot (100%) rename src/generators/api-links/{test => __tests__}/fixtures/buffer.js (100%) rename src/generators/api-links/{test => __tests__}/fixtures/class.js (100%) rename src/generators/api-links/{test => __tests__}/fixtures/exports.js (100%) rename src/generators/api-links/{test => __tests__}/fixtures/mod.js (76%) rename src/generators/api-links/{test => __tests__}/fixtures/prototype.js (100%) rename src/generators/api-links/{test => __tests__}/fixtures/reverse.js (100%) rename src/generators/api-links/{test => __tests__}/fixtures/root.js (100%) delete mode 100644 src/generators/jsx-ast/test/utils.test.mjs create mode 100644 src/generators/jsx-ast/utils/__tests__/ast.test.mjs create mode 100644 src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs create mode 100644 src/generators/jsx-ast/utils/__tests__/buildContent.test.mjs create mode 100644 src/generators/jsx-ast/utils/__tests__/utils.mjs rename src/{test/man-page.test.mjs => generators/man-page/utils/__tests__/converter.test.mjs} (99%) rename src/linter/{tests => __tests__}/fixtures/descriptors.mjs (100%) rename src/linter/{tests => __tests__}/fixtures/issues.mjs (100%) rename src/linter/{tests => __tests__}/fixtures/tree.mjs (100%) rename src/linter/{tests => __tests__}/index.test.mjs (100%) rename src/linter/{tests/reporters => reporters/__tests__}/console.test.mjs (72%) rename src/linter/{tests/reporters => reporters/__tests__}/github.test.mjs (70%) rename src/linter/{tests/rules => rules/__tests__}/duplicate-stability-nodes.test.mjs (98%) rename src/linter/{tests/fixtures/invalidChangeVersion-environment.mjs => rules/__tests__/fixtures/invalid-change-version-subprocess.mjs} (89%) rename src/linter/{tests/rules => rules/__tests__}/invalid-change-version.test.mjs (97%) rename src/linter/{tests/rules => rules/__tests__}/missing-introduced-in.test.mjs (100%) rename src/utils/{tests => __tests__}/unist.test.mjs (100%) rename src/utils/{tests/parser.test.mjs => parser/__tests__/index.test.mjs} (99%) rename src/utils/{tests/queries.test.mjs => queries/__tests__/index.test.mjs} (98%) diff --git a/.prettierignore b/.prettierignore index c53ce87a..0f042efe 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,3 @@ # Tests files -src/generators/api-links/test/fixtures/ +src/generators/api-links/__tests__/fixtures/ *.snapshot diff --git a/eslint.config.mjs b/eslint.config.mjs index 015a612a..11b6858e 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -7,7 +7,7 @@ export default [ pluginJs.configs.recommended, importX.flatConfigs.recommended, { - ignores: ['out/', 'src/generators/api-links/test/fixtures/'], + ignores: ['out/', 'src/generators/api-links/__tests__/fixtures/'], }, { files: ['**/*.mjs'], diff --git a/src/test/metadata.test.mjs b/src/__tests__/metadata.test.mjs similarity index 100% rename from src/test/metadata.test.mjs rename to src/__tests__/metadata.test.mjs diff --git a/src/generators/api-links/test/fixtures.test.mjs b/src/generators/api-links/__tests__/fixtures.test.mjs similarity index 100% rename from src/generators/api-links/test/fixtures.test.mjs rename to src/generators/api-links/__tests__/fixtures.test.mjs diff --git a/src/generators/api-links/test/fixtures.test.mjs.snapshot b/src/generators/api-links/__tests__/fixtures.test.mjs.snapshot similarity index 100% rename from src/generators/api-links/test/fixtures.test.mjs.snapshot rename to src/generators/api-links/__tests__/fixtures.test.mjs.snapshot diff --git a/src/generators/api-links/test/fixtures/buffer.js b/src/generators/api-links/__tests__/fixtures/buffer.js similarity index 100% rename from src/generators/api-links/test/fixtures/buffer.js rename to src/generators/api-links/__tests__/fixtures/buffer.js diff --git a/src/generators/api-links/test/fixtures/class.js b/src/generators/api-links/__tests__/fixtures/class.js similarity index 100% rename from src/generators/api-links/test/fixtures/class.js rename to src/generators/api-links/__tests__/fixtures/class.js diff --git a/src/generators/api-links/test/fixtures/exports.js b/src/generators/api-links/__tests__/fixtures/exports.js similarity index 100% rename from src/generators/api-links/test/fixtures/exports.js rename to src/generators/api-links/__tests__/fixtures/exports.js diff --git a/src/generators/api-links/test/fixtures/mod.js b/src/generators/api-links/__tests__/fixtures/mod.js similarity index 76% rename from src/generators/api-links/test/fixtures/mod.js rename to src/generators/api-links/__tests__/fixtures/mod.js index 72606121..22f51eb6 100644 --- a/src/generators/api-links/test/fixtures/mod.js +++ b/src/generators/api-links/__tests__/fixtures/mod.js @@ -2,10 +2,8 @@ // A module may export one or more methods. -function foo() { -} - +function foo() {} module.exports = { - foo + foo, }; diff --git a/src/generators/api-links/test/fixtures/prototype.js b/src/generators/api-links/__tests__/fixtures/prototype.js similarity index 100% rename from src/generators/api-links/test/fixtures/prototype.js rename to src/generators/api-links/__tests__/fixtures/prototype.js diff --git a/src/generators/api-links/test/fixtures/reverse.js b/src/generators/api-links/__tests__/fixtures/reverse.js similarity index 100% rename from src/generators/api-links/test/fixtures/reverse.js rename to src/generators/api-links/__tests__/fixtures/reverse.js diff --git a/src/generators/api-links/test/fixtures/root.js b/src/generators/api-links/__tests__/fixtures/root.js similarity index 100% rename from src/generators/api-links/test/fixtures/root.js rename to src/generators/api-links/__tests__/fixtures/root.js diff --git a/src/generators/jsx-ast/test/utils.test.mjs b/src/generators/jsx-ast/test/utils.test.mjs deleted file mode 100644 index 3ca31a30..00000000 --- a/src/generators/jsx-ast/test/utils.test.mjs +++ /dev/null @@ -1,82 +0,0 @@ -import assert from 'node:assert/strict'; -import test from 'node:test'; - -import remarkParse from 'remark-parse'; -import remarkStringify from 'remark-stringify'; -import { unified } from 'unified'; - -import { AST_NODE_TYPES } from '../constants.mjs'; -import { createJSXElement } from '../utils/ast.mjs'; -import { - buildSideBarDocPages, - buildMetaBarProps, -} from '../utils/buildBarProps.mjs'; -import buildContent from '../utils/buildContent.mjs'; - -const sampleEntry = { - api: 'sample-api', - heading: { - depth: 2, - data: { name: 'SampleFunc', slug: 'sample-func', type: 'function' }, - }, - content: { - type: 'root', - children: [ - { type: 'text', value: 'Example text for testing reading time.' }, - ], - }, - added_in: 'v1.0.0', - source_link: '/src/index.js', - changes: [ - { - version: 'v1.1.0', - description: 'Improved performance', - 'pr-url': 'https://github.com/org/repo/pull/123', - }, - ], -}; - -test('buildSideBarDocPages returns expected format', () => { - const grouped = new Map([['sample-api', [sampleEntry]]]); - const result = buildSideBarDocPages(grouped, [sampleEntry]); - - assert.equal(result.length, 1); - assert.equal(result[0].title, 'SampleFunc'); - assert.equal(result[0].doc, 'sample-api.html'); - assert.deepEqual(result[0].headings, [['SampleFunc', '#sample-func']]); -}); - -test('buildMetaBarProps includes expected fields', () => { - const result = buildMetaBarProps(sampleEntry, [sampleEntry]); - - assert.equal(result.addedIn, 'v1.0.0'); - assert.deepEqual(result.viewAs, [['JSON', 'sample-api.json']]); - assert.ok(result.readingTime.startsWith('1 min')); - assert.ok(result.editThisPage.endsWith('sample-api.md')); - assert.deepEqual(result.headings, [{ depth: 2, value: 'SampleFunc' }]); -}); - -test('createJSXElement builds correct JSX tree', () => { - const el = createJSXElement('TestComponent', { - inline: false, - children: 'Some content', - dataAttr: { test: true }, - }); - - assert.equal(el.type, AST_NODE_TYPES.MDX.JSX_BLOCK_ELEMENT); - assert.equal(el.name, 'TestComponent'); - assert.ok(Array.isArray(el.children)); - assert.ok(el.attributes.some(attr => attr.name === 'dataAttr')); -}); - -test('buildContent processes entries and includes JSX wrapper elements', () => { - const processor = unified().use(remarkParse).use(remarkStringify); - const tree = buildContent([sampleEntry], sampleEntry, {}, processor); - - const article = tree.children.find( - child => child.name === AST_NODE_TYPES.JSX.ARTICLE - ); - assert.ok(article); - assert.ok(article.children.some(c => c.name === AST_NODE_TYPES.JSX.SIDE_BAR)); - assert.ok(article.children.some(c => c.name === AST_NODE_TYPES.JSX.FOOTER)); -}); diff --git a/src/generators/jsx-ast/utils/__tests__/ast.test.mjs b/src/generators/jsx-ast/utils/__tests__/ast.test.mjs new file mode 100644 index 00000000..8c4c12b4 --- /dev/null +++ b/src/generators/jsx-ast/utils/__tests__/ast.test.mjs @@ -0,0 +1,20 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { AST_NODE_TYPES } from '../../constants.mjs'; +import { createJSXElement } from '../ast.mjs'; + +describe('AST utilities', () => { + it('should build correct JSX tree with createJSXElement', () => { + const el = createJSXElement('TestComponent', { + inline: false, + children: 'Some content', + dataAttr: { test: true }, + }); + + assert.equal(el.type, AST_NODE_TYPES.MDX.JSX_BLOCK_ELEMENT); + assert.equal(el.name, 'TestComponent'); + assert.ok(Array.isArray(el.children)); + assert.ok(el.attributes.some(attr => attr.name === 'dataAttr')); + }); +}); diff --git a/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs b/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs new file mode 100644 index 00000000..aec6f9b7 --- /dev/null +++ b/src/generators/jsx-ast/utils/__tests__/buildBarProps.test.mjs @@ -0,0 +1,31 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import { SAMPLE } from './utils.mjs'; +import { buildSideBarDocPages, buildMetaBarProps } from '../buildBarProps.mjs'; + +describe('buildBarProps utilities', () => { + describe('buildSideBarDocPages', () => { + it('should return expected format', () => { + const grouped = new Map([['sample-api', [SAMPLE]]]); + const result = buildSideBarDocPages(grouped, [SAMPLE]); + + assert.equal(result.length, 1); + assert.equal(result[0].title, 'SampleFunc'); + assert.equal(result[0].doc, 'sample-api.html'); + assert.deepEqual(result[0].headings, [['SampleFunc', '#sample-func']]); + }); + }); + + describe('buildMetaBarProps', () => { + it('should include expected fields', () => { + const result = buildMetaBarProps(SAMPLE, [SAMPLE]); + + assert.equal(result.addedIn, 'v1.0.0'); + assert.deepEqual(result.viewAs, [['JSON', 'sample-api.json']]); + assert.ok(result.readingTime.startsWith('1 min')); + assert.ok(result.editThisPage.endsWith('sample-api.md')); + assert.deepEqual(result.headings, [{ depth: 2, value: 'SampleFunc' }]); + }); + }); +}); diff --git a/src/generators/jsx-ast/utils/__tests__/buildContent.test.mjs b/src/generators/jsx-ast/utils/__tests__/buildContent.test.mjs new file mode 100644 index 00000000..176038df --- /dev/null +++ b/src/generators/jsx-ast/utils/__tests__/buildContent.test.mjs @@ -0,0 +1,26 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; + +import remarkParse from 'remark-parse'; +import remarkStringify from 'remark-stringify'; +import { unified } from 'unified'; + +import { SAMPLE } from './utils.mjs'; +import { AST_NODE_TYPES } from '../../constants.mjs'; +import buildContent from '../buildContent.mjs'; + +describe('buildContent', () => { + it('should process entries and include JSX wrapper elements', () => { + const processor = unified().use(remarkParse).use(remarkStringify); + const tree = buildContent([SAMPLE], SAMPLE, {}, processor); + + const article = tree.children.find( + child => child.name === AST_NODE_TYPES.JSX.ARTICLE + ); + assert.ok(article); + assert.ok( + article.children.some(c => c.name === AST_NODE_TYPES.JSX.SIDE_BAR) + ); + assert.ok(article.children.some(c => c.name === AST_NODE_TYPES.JSX.FOOTER)); + }); +}); diff --git a/src/generators/jsx-ast/utils/__tests__/utils.mjs b/src/generators/jsx-ast/utils/__tests__/utils.mjs new file mode 100644 index 00000000..786f3508 --- /dev/null +++ b/src/generators/jsx-ast/utils/__tests__/utils.mjs @@ -0,0 +1,22 @@ +export const SAMPLE = { + api: 'sample-api', + heading: { + depth: 2, + data: { name: 'SampleFunc', slug: 'sample-func', type: 'function' }, + }, + content: { + type: 'root', + children: [ + { type: 'text', value: 'Example text for testing reading time.' }, + ], + }, + added_in: 'v1.0.0', + source_link: '/src/index.js', + changes: [ + { + version: 'v1.1.0', + description: 'Improved performance', + 'pr-url': 'https://github.com/org/repo/pull/123', + }, + ], +}; diff --git a/src/test/man-page.test.mjs b/src/generators/man-page/utils/__tests__/converter.test.mjs similarity index 99% rename from src/test/man-page.test.mjs rename to src/generators/man-page/utils/__tests__/converter.test.mjs index 351f4534..505a9b70 100644 --- a/src/test/man-page.test.mjs +++ b/src/generators/man-page/utils/__tests__/converter.test.mjs @@ -8,7 +8,7 @@ import { flagValueToMandoc, convertOptionToMandoc, convertEnvVarToMandoc, -} from '../generators/man-page/utils/converter.mjs'; +} from '../converter.mjs'; const textNode = text => u('text', text); diff --git a/src/linter/tests/fixtures/descriptors.mjs b/src/linter/__tests__/fixtures/descriptors.mjs similarity index 100% rename from src/linter/tests/fixtures/descriptors.mjs rename to src/linter/__tests__/fixtures/descriptors.mjs diff --git a/src/linter/tests/fixtures/issues.mjs b/src/linter/__tests__/fixtures/issues.mjs similarity index 100% rename from src/linter/tests/fixtures/issues.mjs rename to src/linter/__tests__/fixtures/issues.mjs diff --git a/src/linter/tests/fixtures/tree.mjs b/src/linter/__tests__/fixtures/tree.mjs similarity index 100% rename from src/linter/tests/fixtures/tree.mjs rename to src/linter/__tests__/fixtures/tree.mjs diff --git a/src/linter/tests/index.test.mjs b/src/linter/__tests__/index.test.mjs similarity index 100% rename from src/linter/tests/index.test.mjs rename to src/linter/__tests__/index.test.mjs diff --git a/src/linter/tests/reporters/console.test.mjs b/src/linter/reporters/__tests__/console.test.mjs similarity index 72% rename from src/linter/tests/reporters/console.test.mjs rename to src/linter/reporters/__tests__/console.test.mjs index 1fe1209b..31e3dc4e 100644 --- a/src/linter/tests/reporters/console.test.mjs +++ b/src/linter/reporters/__tests__/console.test.mjs @@ -1,8 +1,12 @@ -import assert from 'node:assert'; +import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; -import reporter from '../../reporters/console.mjs'; -import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs'; +import { + errorIssue, + infoIssue, + warnIssue, +} from '../../__tests__/fixtures/issues.mjs'; +import reporter from '../console.mjs'; const testCases = [ { @@ -30,8 +34,8 @@ describe('console', () => { reporter(issue); - assert.strictEqual(mock.callCount(), 1); - assert.deepStrictEqual(mock.calls[0].arguments, [expected]); + assert.equal(mock.callCount(), 1); + assert.deepEqual(mock.calls[0].arguments, [expected]); }); }); }); diff --git a/src/linter/tests/reporters/github.test.mjs b/src/linter/reporters/__tests__/github.test.mjs similarity index 70% rename from src/linter/tests/reporters/github.test.mjs rename to src/linter/reporters/__tests__/github.test.mjs index 5a02a03d..d6bd64dd 100644 --- a/src/linter/tests/reporters/github.test.mjs +++ b/src/linter/reporters/__tests__/github.test.mjs @@ -1,8 +1,12 @@ -import assert from 'node:assert'; +import assert from 'node:assert/strict'; import { describe, it } from 'node:test'; -import github from '../../reporters/github.mjs'; -import { errorIssue, infoIssue, warnIssue } from '../fixtures/issues.mjs'; +import { + errorIssue, + infoIssue, + warnIssue, +} from '../../__tests__/fixtures/issues.mjs'; +import github from '../github.mjs'; describe('github', () => { it('should write to stdout with the correct fn based on the issue level', t => { @@ -12,13 +16,13 @@ describe('github', () => { github(warnIssue); github(errorIssue); - assert.strictEqual(process.stdout.write.mock.callCount(), 3); + assert.equal(process.stdout.write.mock.callCount(), 3); const callsArgs = process.stdout.write.mock.calls.map(call => call.arguments[0].trim() ); - assert.deepStrictEqual(callsArgs, [ + assert.deepEqual(callsArgs, [ '::notice file=doc/api/test.md,line=1,endLine=1::This is a INFO issue', '::warning file=doc/api/test.md,line=1,endLine=1::This is a WARN issue', '::error file=doc/api/test.md,line=1,endLine=1::This is a ERROR issue', diff --git a/src/linter/tests/rules/duplicate-stability-nodes.test.mjs b/src/linter/rules/__tests__/duplicate-stability-nodes.test.mjs similarity index 98% rename from src/linter/tests/rules/duplicate-stability-nodes.test.mjs rename to src/linter/rules/__tests__/duplicate-stability-nodes.test.mjs index b3f4a010..db1d5760 100644 --- a/src/linter/tests/rules/duplicate-stability-nodes.test.mjs +++ b/src/linter/rules/__tests__/duplicate-stability-nodes.test.mjs @@ -2,7 +2,7 @@ import { deepStrictEqual, strictEqual } from 'node:assert'; import { describe, it, mock } from 'node:test'; import { LINT_MESSAGES } from '../../constants.mjs'; -import { duplicateStabilityNodes } from '../../rules/duplicate-stability-nodes.mjs'; +import { duplicateStabilityNodes } from '../duplicate-stability-nodes.mjs'; // Mock data structure for creating test entries const createStabilityNode = (value, line = 1, column = 1) => ({ diff --git a/src/linter/tests/fixtures/invalidChangeVersion-environment.mjs b/src/linter/rules/__tests__/fixtures/invalid-change-version-subprocess.mjs similarity index 89% rename from src/linter/tests/fixtures/invalidChangeVersion-environment.mjs rename to src/linter/rules/__tests__/fixtures/invalid-change-version-subprocess.mjs index fb7f70be..7c1f4048 100644 --- a/src/linter/tests/fixtures/invalidChangeVersion-environment.mjs +++ b/src/linter/rules/__tests__/fixtures/invalid-change-version-subprocess.mjs @@ -3,7 +3,7 @@ import { mock } from 'node:test'; import dedent from 'dedent'; -import { invalidChangeVersion } from '../../rules/invalid-change-version.mjs'; +import { invalidChangeVersion } from '../../invalid-change-version.mjs'; const yamlContent = dedent`