Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/docusaurusConfigPreprocessor.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { registerHooks } from 'node:module';
import path from 'node:path';
import test from 'node:test';
import assert from 'node:assert/strict';
import { resolve } from './testHelpers/resolveExtensionlessTs.mjs';

// docusaurus.config.ts imports its ./lib/* helpers without file extensions,
// which only Docusaurus's own webpack bundler resolves. Register a
// test-only hook (see testHelpers/resolveExtensionlessTs.mjs) so this file
// can import the real docusaurus.config.ts directly, instead of duplicating
// its markdown.preprocessor logic.
registerHooks({ resolve });

const { default: config } = await import('../docusaurus.config.ts');
const { preprocessor } = config.markdown;
const importedReadmePath = path.resolve('README.md');

test('markdown.preprocessor (root README.md import)', async (t) => {
await t.test('strips .md from /docs and /analyses links', () => {
const result = preprocessor({
fileContent:
'See [docs](/docs/foo.md) and [analyses](/analyses/bar.md#section).',
filePath: importedReadmePath,
});

assert.equal(
result,
'See [docs](/docs/foo/) and [analyses](/analyses/bar/#section).',
);
});

await t.test('leaves non-README imports untouched', () => {
const fileContent = 'See [docs](/docs/foo.md).';
const result = preprocessor({
fileContent,
filePath: path.resolve('docs/other.md'),
});

assert.equal(result, fileContent);
});
});
34 changes: 34 additions & 0 deletions lib/testHelpers/resolveExtensionlessTs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Minimal Node.js module-resolution hook used only by tests.
//
// docusaurus.config.ts imports its ./lib/* helpers without file extensions
// (e.g. `./lib/getBuildContext`), which Docusaurus's own webpack bundler
// resolves fine but Node's native ESM loader does not. This hook teaches
// Node to fall back to `<specifier>.ts` (and `<specifier>/index.ts` for
// directory imports) so docusaurus.config.ts can be imported directly in
// `node --test`, without changing any production import statement.
import { existsSync, statSync } from 'node:fs';
import { fileURLToPath } from 'node:url';

export function resolve(specifier, context, nextResolve) {
try {
return nextResolve(specifier, context);
} catch (err) {
if (!specifier.startsWith('.')) throw err;

if (err.code === 'ERR_MODULE_NOT_FOUND') {
const candidate = new URL(specifier + '.ts', context.parentURL);
if (existsSync(fileURLToPath(candidate))) {
return nextResolve(specifier + '.ts', context);
}
}

if (err.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
const dirPath = fileURLToPath(new URL(specifier, context.parentURL));
if (existsSync(dirPath) && statSync(dirPath).isDirectory()) {
return nextResolve(specifier + '/index.ts', context);
}
}

throw err;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"precheck:links": "npm run build",
"seq": "bash -c 'for cmd in \"$@\"; do npm run $cmd || exit 1; done' - ",
"serve": "npm run docus:serve",
"test:coverage": "c8 --reporter=text --reporter=lcov --check-coverage --lines 99 --statements 99 --branches 97 --functions 100 npm run test:unit",
"test:coverage": "c8 --reporter=text --reporter=lcov --exclude 'docusaurus.config.ts' --exclude 'lib/testHelpers/**' --check-coverage --lines 99 --statements 99 --branches 97 --functions 100 npm run test:unit",
"test:unit": "node --experimental-strip-types --experimental-test-module-mocks --test 'lib/**/*.test.mts' 'scripts/**/*.test.mjs'",
"test": "npm run check && npm run test:unit",
"typecheck": "tsc",
Expand Down
Loading