Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(cli): catalog-heavy tests load their code under test natively (#10328)

Christopher Chedeau755206df279b
diff --git a/packages/cli/api/component/doc-links.test.mjs b/packages/cli/api/component/doc-links.test.mjs
index 0a7f0c4b50..cdc8c8e7a5 100644
--- a/packages/cli/api/component/doc-links.test.mjs
+++ b/packages/cli/api/component/doc-links.test.mjs
@@ -13,11 +13,19 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
-import {component} from './component.mjs';
-import {CORE_PACKAGE, linkComponentResponse} from './_adapter.mjs';
-import {docs} from '../docs/docs.mjs';
-import {search} from '../search/search.mjs';
-import {runCli} from '../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./component.mjs')} */
+const {component} = require('./component.mjs');
+/** @type {typeof import('./_adapter.mjs')} */
+const {CORE_PACKAGE, linkComponentResponse} = require('./_adapter.mjs');
+/** @type {typeof import('../docs/docs.mjs')} */
+const {docs} = require('../docs/docs.mjs');
+/** @type {typeof import('../search/search.mjs')} */
+const {search} = require('../search/search.mjs');
+/** @type {typeof import('../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../test-utils/run-cli.mjs');
const SLOW = 60_000;
diff --git a/packages/cli/api/component/integration-import-agreement.test.mjs b/packages/cli/api/component/integration-import-agreement.test.mjs
index bef57e33e5..77177c1374 100644
--- a/packages/cli/api/component/integration-import-agreement.test.mjs
+++ b/packages/cli/api/component/integration-import-agreement.test.mjs
@@ -24,9 +24,17 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
-import {component} from './component.mjs';
-import {search} from '../search/search.mjs';
-import {resolveIntegrationImportPath} from '../../foundation/discovery/component-discovery.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./component.mjs')} */
+const {component} = require('./component.mjs');
+/** @type {typeof import('../search/search.mjs')} */
+const {search} = require('../search/search.mjs');
+/** @type {typeof import('../../foundation/discovery/component-discovery.mjs')} */
+const {
+ resolveIntegrationImportPath,
+} = require('../../foundation/discovery/component-discovery.mjs');
const SLOW = 30_000;
diff --git a/packages/cli/api/integration/template-conflict-compatibility.test.mjs b/packages/cli/api/integration/template-conflict-compatibility.test.mjs
index 053ffff92d..3b5884083a 100644
--- a/packages/cli/api/integration/template-conflict-compatibility.test.mjs
+++ b/packages/cli/api/integration/template-conflict-compatibility.test.mjs
@@ -10,8 +10,15 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
-import {discoverCoreTemplates} from '../../foundation/discovery/template-adapter.mjs';
-import {integrationTemplateConflicts} from './authoring-checks.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../foundation/discovery/template-adapter.mjs')} */
+const {
+ discoverCoreTemplates,
+} = require('../../foundation/discovery/template-adapter.mjs');
+/** @type {typeof import('./authoring-checks.mjs')} */
+const {integrationTemplateConflicts} = require('./authoring-checks.mjs');
let tmpDir;
diff --git a/packages/cli/api/search/_entries.test.mjs b/packages/cli/api/search/_entries.test.mjs
index 907f1aeb22..5631138555 100644
--- a/packages/cli/api/search/_entries.test.mjs
+++ b/packages/cli/api/search/_entries.test.mjs
@@ -8,8 +8,13 @@
import {describe, it, expect} from 'vitest';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {gatherCandidates, loadSearchData} from './_adapter.mjs';
-import {HASHED_FIELDS, buildTable, entryTable} from './_entries.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./_adapter.mjs')} */
+const {gatherCandidates, loadSearchData} = require('./_adapter.mjs');
+/** @type {typeof import('./_entries.mjs')} */
+const {HASHED_FIELDS, buildTable, entryTable} = require('./_entries.mjs');
const cwd = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
diff --git a/packages/cli/api/search/_main-searcher.test.mjs b/packages/cli/api/search/_main-searcher.test.mjs
index 8bd06a4cc0..bfbb1b9347 100644
--- a/packages/cli/api/search/_main-searcher.test.mjs
+++ b/packages/cli/api/search/_main-searcher.test.mjs
@@ -13,7 +13,11 @@ import http from 'node:http';
import https from 'node:https';
import net from 'node:net';
import {fileURLToPath} from 'node:url';
-import {search, searchCore} from './search.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search, searchCore} = require('./search.mjs');
const REPO = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
diff --git a/packages/cli/api/search/_order.test.mjs b/packages/cli/api/search/_order.test.mjs
index c735c95c33..792dae2738 100644
--- a/packages/cli/api/search/_order.test.mjs
+++ b/packages/cli/api/search/_order.test.mjs
@@ -9,8 +9,13 @@
import {describe, it, expect} from 'vitest';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {search, searchCore} from './search.mjs';
-import {bandOf} from './_order.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search, searchCore} = require('./search.mjs');
+/** @type {typeof import('./_order.mjs')} */
+const {bandOf} = require('./_order.mjs');
const cwd = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
diff --git a/packages/cli/api/search/search.family-members.test.mjs b/packages/cli/api/search/search.family-members.test.mjs
index e54327e77d..037f23d622 100644
--- a/packages/cli/api/search/search.family-members.test.mjs
+++ b/packages/cli/api/search/search.family-members.test.mjs
@@ -11,7 +11,11 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
-import {search} from './search.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search} = require('./search.mjs');
const SLOW = 60_000;
diff --git a/packages/cli/api/search/search.family-word.test.mjs b/packages/cli/api/search/search.family-word.test.mjs
index be2236a455..be41743f8d 100644
--- a/packages/cli/api/search/search.family-word.test.mjs
+++ b/packages/cli/api/search/search.family-word.test.mjs
@@ -11,7 +11,11 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
-import {search} from './search.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search} = require('./search.mjs');
const SLOW = 60_000;
diff --git a/packages/cli/api/search/search.integration-block.test.mjs b/packages/cli/api/search/search.integration-block.test.mjs
index 44736c0155..c032702276 100644
--- a/packages/cli/api/search/search.integration-block.test.mjs
+++ b/packages/cli/api/search/search.integration-block.test.mjs
@@ -13,8 +13,13 @@ import os from 'node:os';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
-import {search} from './search.mjs';
-import {build} from '../build/build.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search} = require('./search.mjs');
+/** @type {typeof import('../build/build.mjs')} */
+const {build} = require('../build/build.mjs');
const SLOW = 60_000;
const packages = path.resolve(
diff --git a/packages/cli/api/search/search.test.mjs b/packages/cli/api/search/search.test.mjs
index affae4dba5..8b016b6512 100644
--- a/packages/cli/api/search/search.test.mjs
+++ b/packages/cli/api/search/search.test.mjs
@@ -24,10 +24,19 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {docs} from '../docs/docs.mjs';
-import {listAvailableThemes} from '../theme/_adapter.mjs';
-import {doc as themeAddCommandDoc} from '../../clients/cli/commands/theme-add.doc.mjs';
-import {
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../docs/docs.mjs')} */
+const {docs} = require('../docs/docs.mjs');
+/** @type {typeof import('../theme/_adapter.mjs')} */
+const {listAvailableThemes} = require('../theme/_adapter.mjs');
+/** @type {typeof import('../../clients/cli/commands/theme-add.doc.mjs')} */
+const {
+ doc: themeAddCommandDoc,
+} = require('../../clients/cli/commands/theme-add.doc.mjs');
+/** @type {typeof import('./search.mjs')} */
+const {
headingWithPhrase,
titleInQuery,
search,
@@ -35,8 +44,11 @@ import {
scoreQuery,
tokenizeQuery,
SEARCH_DOMAINS,
-} from './search.mjs';
-import {linkThisCliAsProjectCli} from '../../test-utils/release-fixtures.mjs';
+} = require('./search.mjs');
+/** @type {typeof import('../../test-utils/release-fixtures.mjs')} */
+const {
+ linkThisCliAsProjectCli,
+} = require('../../test-utils/release-fixtures.mjs');
const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..');
const cwd = REPO;
diff --git a/packages/cli/api/search/search.tie-order.test.mjs b/packages/cli/api/search/search.tie-order.test.mjs
index 7742de67cd..139acc1dc5 100644
--- a/packages/cli/api/search/search.tie-order.test.mjs
+++ b/packages/cli/api/search/search.tie-order.test.mjs
@@ -8,7 +8,11 @@
*/
import {describe, expect, it} from 'vitest';
-import {search} from './search.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./search.mjs')} */
+const {search} = require('./search.mjs');
describe('ties below the first result', () => {
it('a part of the first result leads its tie', async () => {
diff --git a/packages/cli/api/search/tree.test.mjs b/packages/cli/api/search/tree.test.mjs
index 5165319715..be843d93b3 100644
--- a/packages/cli/api/search/tree.test.mjs
+++ b/packages/cli/api/search/tree.test.mjs
@@ -9,10 +9,17 @@
import {describe, it, expect} from 'vitest';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {runTree} from '../../foundation/route/route.mjs';
-import {gatherCandidates, loadSearchData} from './_adapter.mjs';
-import {routeProbs, ROUTES} from './_query-words.mjs';
-import {SEARCH_TREE} from './tree.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../foundation/route/route.mjs')} */
+const {runTree} = require('../../foundation/route/route.mjs');
+/** @type {typeof import('./_adapter.mjs')} */
+const {gatherCandidates, loadSearchData} = require('./_adapter.mjs');
+/** @type {typeof import('./_query-words.mjs')} */
+const {routeProbs, ROUTES} = require('./_query-words.mjs');
+/** @type {typeof import('./tree.mjs')} */
+const {SEARCH_TREE} = require('./tree.mjs');
const REPO = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
diff --git a/packages/cli/api/template/discover-integration-templates.test.mjs b/packages/cli/api/template/discover-integration-templates.test.mjs
index 45390c0381..f503f71b00 100644
--- a/packages/cli/api/template/discover-integration-templates.test.mjs
+++ b/packages/cli/api/template/discover-integration-templates.test.mjs
@@ -15,7 +15,11 @@ import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
-import {discoverAllWithErrors} from './template.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./template.mjs')} */
+const {discoverAllWithErrors} = require('./template.mjs');
let tmpDir;
diff --git a/packages/cli/api/template/inline-wizard-template.test.mjs b/packages/cli/api/template/inline-wizard-template.test.mjs
index 88fb4286b8..15bc136f6c 100644
--- a/packages/cli/api/template/inline-wizard-template.test.mjs
+++ b/packages/cli/api/template/inline-wizard-template.test.mjs
@@ -9,7 +9,11 @@
*/
import {describe, expect, it} from 'vitest';
-import {template} from './template.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./template.mjs')} */
+const {template} = require('./template.mjs');
describe('inline wizard template interactions', () => {
it('ships step navigation without a dead skip action', async () => {
diff --git a/packages/cli/api/template/template-integration.test.mjs b/packages/cli/api/template/template-integration.test.mjs
index b3ee43d6dd..57fb70784b 100644
--- a/packages/cli/api/template/template-integration.test.mjs
+++ b/packages/cli/api/template/template-integration.test.mjs
@@ -12,14 +12,21 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
-import {
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./template.mjs')} */
+const {
discoverAllWithErrors,
discoverCoreTemplates,
template,
-} from './template.mjs';
-import {search} from '../search/search.mjs';
-import {build} from '../build/build.mjs';
-import {runCli} from '../../test-utils/run-cli.mjs';
+} = require('./template.mjs');
+/** @type {typeof import('../search/search.mjs')} */
+const {search} = require('../search/search.mjs');
+/** @type {typeof import('../build/build.mjs')} */
+const {build} = require('../build/build.mjs');
+/** @type {typeof import('../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../test-utils/run-cli.mjs');
let tmpDir;
let originalCwd;
diff --git a/packages/cli/clients/cli/commands/build.text-fields.test.mjs b/packages/cli/clients/cli/commands/build.text-fields.test.mjs
index 43bdeda190..1310416c34 100644
--- a/packages/cli/clients/cli/commands/build.text-fields.test.mjs
+++ b/packages/cli/clients/cli/commands/build.text-fields.test.mjs
@@ -8,7 +8,11 @@
import {describe, it, expect} from 'vitest';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {runCli} from '../../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../../test-utils/run-cli.mjs');
// Run against the monorepo root so @astryxdesign/core is discoverable.
const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..');
diff --git a/packages/cli/clients/cli/commands/dialog-adaptive-template.test.mjs b/packages/cli/clients/cli/commands/dialog-adaptive-template.test.mjs
index 46a013b7f4..4b2b783bdc 100644
--- a/packages/cli/clients/cli/commands/dialog-adaptive-template.test.mjs
+++ b/packages/cli/clients/cli/commands/dialog-adaptive-template.test.mjs
@@ -1,7 +1,11 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, expect, it} from 'vitest';
-import {component} from '../../../api/component/component.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../api/component/component.mjs')} */
+const {component} = require('../../../api/component/component.mjs');
const CWD = {cwd: '.'};
diff --git a/packages/cli/clients/cli/commands/external-showcase.test.mjs b/packages/cli/clients/cli/commands/external-showcase.test.mjs
index cf1c009bef..1080fb87d5 100644
--- a/packages/cli/clients/cli/commands/external-showcase.test.mjs
+++ b/packages/cli/clients/cli/commands/external-showcase.test.mjs
@@ -4,7 +4,14 @@ import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
-import {findShowcase, findRelatedBlocks} from '../../../api/template/template.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../api/template/template.mjs')} */
+const {
+ findShowcase,
+ findRelatedBlocks,
+} = require('../../../api/template/template.mjs');
// These tests verify that findShowcase and findRelatedBlocks can discover
// blocks from external packages that declare xds.blocks in package.json.
diff --git a/packages/cli/clients/cli/commands/hook.text-projection.test.mjs b/packages/cli/clients/cli/commands/hook.text-projection.test.mjs
index 8a9462f5b3..ef20ccee6e 100644
--- a/packages/cli/clients/cli/commands/hook.text-projection.test.mjs
+++ b/packages/cli/clients/cli/commands/hook.text-projection.test.mjs
@@ -6,8 +6,13 @@
*/
import {describe, it, expect} from 'vitest';
-import {runCli} from '../../../test-utils/run-cli.mjs';
-import {findRelatedBlocks} from '../../../api/template/template.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../../test-utils/run-cli.mjs');
+/** @type {typeof import('../../../api/template/template.mjs')} */
+const {findRelatedBlocks} = require('../../../api/template/template.mjs');
const HOOK = 'useAnnounce';
diff --git a/packages/cli/clients/cli/commands/integration-real-world.test.mjs b/packages/cli/clients/cli/commands/integration-real-world.test.mjs
index de48b6b77d..07f0c1a743 100644
--- a/packages/cli/clients/cli/commands/integration-real-world.test.mjs
+++ b/packages/cli/clients/cli/commands/integration-real-world.test.mjs
@@ -11,7 +11,11 @@ import {afterEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {spawnSync} from 'node:child_process';
-import {runCli} from '../../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../../test-utils/run-cli.mjs');
let rootDir;
const CLI_BIN = path.join(
diff --git a/packages/cli/clients/cli/commands/search.test.mjs b/packages/cli/clients/cli/commands/search.test.mjs
index a2083ecf44..60dd28e495 100644
--- a/packages/cli/clients/cli/commands/search.test.mjs
+++ b/packages/cli/clients/cli/commands/search.test.mjs
@@ -15,13 +15,21 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../api/search/search.mjs')} */
+const {
search,
scoreCandidate,
SEARCH_DOMAINS,
-} from '../../../api/search/search.mjs';
-import {runCli} from '../../../test-utils/run-cli.mjs';
-import {linkThisCliAsProjectCli} from '../../../test-utils/release-fixtures.mjs';
+} = require('../../../api/search/search.mjs');
+/** @type {typeof import('../../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../../test-utils/run-cli.mjs');
+/** @type {typeof import('../../../test-utils/release-fixtures.mjs')} */
+const {
+ linkThisCliAsProjectCli,
+} = require('../../../test-utils/release-fixtures.mjs');
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Run against the monorepo root so @astryxdesign/core is discoverable.
diff --git a/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs b/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs
index 21e089cf32..0475cdf89d 100644
--- a/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs
+++ b/packages/cli/clients/cli/commands/silent-success-refusals.test.mjs
@@ -1,7 +1,12 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path';
-import {runCli} from '../../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../../test-utils/run-cli.mjs');
+
let dir;
beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(),'astryx-ss-')); fs.writeFileSync(path.join(dir,'package.json'),JSON.stringify({name:'s',version:'1.0.0',dependencies:{'@astryxdesign/core':'0.6.3'}})); const c=path.join(dir,'node_modules','@astryxdesign','core'); fs.mkdirSync(c,{recursive:true}); fs.writeFileSync(path.join(c,'package.json'),JSON.stringify({name:'@astryxdesign/core',version:'0.6.3'})); });
afterEach(() => fs.rmSync(dir, {recursive:true,force:true}));
diff --git a/packages/cli/foundation/discovery/template-adapter.integration-isolation.test.mjs b/packages/cli/foundation/discovery/template-adapter.integration-isolation.test.mjs
index 42ab22650c..635d5057d0 100644
--- a/packages/cli/foundation/discovery/template-adapter.integration-isolation.test.mjs
+++ b/packages/cli/foundation/discovery/template-adapter.integration-isolation.test.mjs
@@ -12,8 +12,13 @@ import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
-import {discoverAll, discoverAllWithErrors} from './template-adapter.mjs';
-import {runCli} from '../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./template-adapter.mjs')} */
+const {discoverAll, discoverAllWithErrors} = require('./template-adapter.mjs');
+/** @type {typeof import('../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../test-utils/run-cli.mjs');
let tmpDir = '';
diff --git a/packages/cli/foundation/doc-compiler/bundle.test.mjs b/packages/cli/foundation/doc-compiler/bundle.test.mjs
index 489444d678..2b57de685d 100644
--- a/packages/cli/foundation/doc-compiler/bundle.test.mjs
+++ b/packages/cli/foundation/doc-compiler/bundle.test.mjs
@@ -8,12 +8,21 @@
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
-import {Project} from '../config/project.mjs';
-import {CLI_ROOT} from '../fs/paths.mjs';
-import {compileDocs} from './bundle.mjs';
-import {COMPILED_DOC_KINDS} from './compile.mjs';
-import {collectDocInputs} from './inputs.mjs';
-import {parseCompiledDocsBundle} from './ir.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../config/project.mjs')} */
+const {Project} = require('../config/project.mjs');
+/** @type {typeof import('../fs/paths.mjs')} */
+const {CLI_ROOT} = require('../fs/paths.mjs');
+/** @type {typeof import('./bundle.mjs')} */
+const {compileDocs} = require('./bundle.mjs');
+/** @type {typeof import('./compile.mjs')} */
+const {COMPILED_DOC_KINDS} = require('./compile.mjs');
+/** @type {typeof import('./inputs.mjs')} */
+const {collectDocInputs} = require('./inputs.mjs');
+/** @type {typeof import('./ir.mjs')} */
+const {parseCompiledDocsBundle} = require('./ir.mjs');
const REPO_ROOT = path.resolve(CLI_ROOT, '..', '..');
@@ -197,7 +206,8 @@ describe('compileDocs over a broken integration', () => {
expect(found.map(d => d.code)).toContain('not_json');
expect(found.map(d => d.code)).not.toContain('invalid_topic');
// The compiler's code never reaches the error a reader throws.
- const {lowerTopic} = await import('../../api/docs/_adapter.mjs');
+ /** @type {typeof import('../../api/docs/_adapter.mjs')} */
+ const {lowerTopic} = require('../../api/docs/_adapter.mjs');
const catalog = await (await Project.load(tmpDir)).docs();
const entry = /** @type {any} */ (catalog.resolve('acme-guide'));
const thrown = await lowerTopic(catalog, entry).catch(error => error);
diff --git a/packages/cli/foundation/doc-compiler/read.test.mjs b/packages/cli/foundation/doc-compiler/read.test.mjs
index 24fecffcca..0df0db7838 100644
--- a/packages/cli/foundation/doc-compiler/read.test.mjs
+++ b/packages/cli/foundation/doc-compiler/read.test.mjs
@@ -12,12 +12,24 @@ import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {pathToFileURL} from 'node:url';
-import {Project} from '../config/project.mjs';
-import {CLI_ROOT} from '../fs/paths.mjs';
-import {loadComponentDoc, loadDocs} from '../discovery/component-loader.mjs';
-import {compileDocFile, readDocView} from './read.mjs';
-import {importDocModule, importNativeModule} from './import.mjs';
-import {collectDocInputs} from './inputs.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../config/project.mjs')} */
+const {Project} = require('../config/project.mjs');
+/** @type {typeof import('../fs/paths.mjs')} */
+const {CLI_ROOT} = require('../fs/paths.mjs');
+/** @type {typeof import('../discovery/component-loader.mjs')} */
+const {
+ loadComponentDoc,
+ loadDocs,
+} = require('../discovery/component-loader.mjs');
+/** @type {typeof import('./read.mjs')} */
+const {compileDocFile, readDocView} = require('./read.mjs');
+/** @type {typeof import('./import.mjs')} */
+const {importDocModule, importNativeModule} = require('./import.mjs');
+/** @type {typeof import('./inputs.mjs')} */
+const {collectDocInputs} = require('./inputs.mjs');
const REPO_ROOT = path.resolve(CLI_ROOT, '..', '..');
diff --git a/packages/cli/foundation/integrations/provider-conflicts.test.mjs b/packages/cli/foundation/integrations/provider-conflicts.test.mjs
index a7d6d71d79..9a18796514 100644
--- a/packages/cli/foundation/integrations/provider-conflicts.test.mjs
+++ b/packages/cli/foundation/integrations/provider-conflicts.test.mjs
@@ -13,9 +13,15 @@ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
-import {Project} from '../config/project.mjs';
-import {doctor} from '../../api/doctor/doctor.mjs';
-import {runCli} from '../../test-utils/run-cli.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../config/project.mjs')} */
+const {Project} = require('../config/project.mjs');
+/** @type {typeof import('../../api/doctor/doctor.mjs')} */
+const {doctor} = require('../../api/doctor/doctor.mjs');
+/** @type {typeof import('../../test-utils/run-cli.mjs')} */
+const {runCli} = require('../../test-utils/run-cli.mjs');
const REPO = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
diff --git a/packages/cli/test-utils/native-require.mjs b/packages/cli/test-utils/native-require.mjs
new file mode 100644
index 0000000000..100ee1b955
--- /dev/null
+++ b/packages/cli/test-utils/native-require.mjs
@@ -0,0 +1,32 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Loads a test's code under test natively, as the CLI itself loads.
+ *
+ * @input The test file's `import.meta.url`.
+ * @output A `require` that resolves from the test file and loads ES modules
+ * with Node's own loader.
+ * @position Test helper for a test whose code under test reads the docs
+ * catalog. `native-require.test.mjs` checks every test that uses it.
+ */
+
+import {createRequire} from 'node:module';
+
+/**
+ * A `require` for the test file at `fromUrl`. The modules it loads, and every
+ * module they import, load as Node loads them.
+ *
+ * SYNC: through Vitest, each module that code under test loads is one request
+ * to Vitest's main process. The docs catalog loads about 1,300 modules, so the
+ * concurrent files of a full run wait there for seconds. Vitest's mocks and
+ * module resets do not reach a module that loads natively. So a test that
+ * uses this helper calls none of them, and `native-require.test.mjs` checks
+ * that. Each test file runs in a process of its own: the forks pool, with
+ * `isolate` on. So a native module never carries state from one file to the
+ * next. With `isolate` off, these files would share the CLI's module state.
+ * @param {string} fromUrl the test file's `import.meta.url`
+ * @returns {NodeJS.Require}
+ */
+export function requireNatively(fromUrl) {
+ return createRequire(fromUrl);
+}
diff --git a/packages/cli/test-utils/native-require.planted-mock.mjs b/packages/cli/test-utils/native-require.planted-mock.mjs
new file mode 100644
index 0000000000..f84b0da3d7
--- /dev/null
+++ b/packages/cli/test-utils/native-require.planted-mock.mjs
@@ -0,0 +1,21 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file A test that loads its code under test natively and also mocks and
+ * resets modules. `native-require.test.mjs` must report each call.
+ */
+
+import {vi} from 'vitest';
+import {requireNatively} from './native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../foundation/fs/paths.mjs')} */
+const {findCoreDir} = require('../foundation/fs/paths.mjs');
+
+vi.mock('../foundation/fs/paths.mjs', () => ({findCoreDir: () => null}));
+vi.doMock('node:fs');
+vi.importActual('../foundation/fs/paths.mjs');
+vi.importMock('../foundation/fs/paths.mjs');
+vi.resetModules();
+
+export {findCoreDir};
diff --git a/packages/cli/test-utils/native-require.test.mjs b/packages/cli/test-utils/native-require.test.mjs
new file mode 100644
index 0000000000..206bb89d0d
--- /dev/null
+++ b/packages/cli/test-utils/native-require.test.mjs
@@ -0,0 +1,126 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Every test that loads its code under test with `requireNatively`
+ * calls no Vitest module mock or reset. They do not reach a module that
+ * loads natively, so such a call would test nothing.
+ */
+
+import {describe, expect, it} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {parse} from '@babel/parser';
+
+const HERE = path.dirname(fileURLToPath(import.meta.url));
+const CLI = path.resolve(HERE, '..');
+const HELPER = path.join(HERE, 'native-require.mjs');
+
+/**
+ * Every test file under `dir`, outside dependency folders.
+ * @param {string} dir
+ * @returns {string[]}
+ */
+function testFiles(dir) {
+ return fs.readdirSync(dir, {withFileTypes: true}).flatMap(entry => {
+ if (entry.name === 'node_modules' || entry.name.startsWith('.')) return [];
+ const full = path.join(dir, entry.name);
+ if (entry.isDirectory()) return testFiles(full);
+ return entry.name.endsWith('.test.mjs') ? [full] : [];
+ });
+}
+
+/**
+ * Calls `visit` on `node` and on every node under it.
+ * @param {any} node
+ * @param {(node: any) => void} visit
+ */
+function walk(node, visit) {
+ if (node == null || typeof node !== 'object') return;
+ if (Array.isArray(node)) {
+ for (const child of node) walk(child, visit);
+ return;
+ }
+ if (typeof node.type === 'string') visit(node);
+ for (const [key, child] of Object.entries(node)) {
+ if (key !== 'loc' && key !== 'leadingComments') walk(child, visit);
+ }
+}
+
+/** The `vi` calls that mock, reset or load around Vitest's module graph. */
+const MODULE_CALLS = new Set([
+ 'mock',
+ 'doMock',
+ 'importActual',
+ 'importMock',
+ 'resetModules',
+]);
+
+/**
+ * Whether `file` imports the helper, and each call in it that
+ * {@link MODULE_CALLS} names, as `line: vi.mock`.
+ * @param {string} file
+ * @returns {{loadsNatively: boolean, mocks: string[]}}
+ */
+function nativeLoadsAndMocks(file) {
+ const ast = parse(fs.readFileSync(file, 'utf8'), {
+ sourceType: 'module',
+ plugins: ['importAttributes'],
+ });
+ const loadsNatively = ast.program.body.some(
+ (/** @type {any} */ node) =>
+ node.type === 'ImportDeclaration' &&
+ node.source.value.startsWith('.') &&
+ path.resolve(path.dirname(file), node.source.value) === HELPER,
+ );
+ /** @type {string[]} */
+ const mocks = [];
+ walk(ast.program, node => {
+ const callee = node.type === 'CallExpression' ? node.callee : null;
+ if (
+ callee?.type === 'MemberExpression' &&
+ callee.object.type === 'Identifier' &&
+ callee.object.name === 'vi' &&
+ callee.property.type === 'Identifier' &&
+ MODULE_CALLS.has(callee.property.name)
+ ) {
+ mocks.push(`${node.loc.start.line}: vi.${callee.property.name}`);
+ }
+ });
+ return {loadsNatively, mocks};
+}
+
+describe('requireNatively', () => {
+ it('is used by no test that mocks or resets a module', () => {
+ /** @type {Record<string, string[]>} */
+ const mocking = {};
+ let users = 0;
+ for (const file of testFiles(CLI)) {
+ if (!fs.readFileSync(file, 'utf8').includes('native-require.mjs')) {
+ continue;
+ }
+ const {loadsNatively, mocks} = nativeLoadsAndMocks(file);
+ if (!loadsNatively) continue;
+ users += 1;
+ if (mocks.length) mocking[path.relative(CLI, file)] = mocks;
+ }
+ // The scan finds the tests that use the helper, so no mocks means
+ // something.
+ expect(users).toBeGreaterThan(0);
+ expect(mocking).toEqual({});
+ });
+
+ it('finds each module mock and reset in a test that loads natively', () => {
+ const planted = path.join(HERE, 'native-require.planted-mock.mjs');
+ expect(nativeLoadsAndMocks(planted)).toEqual({
+ loadsNatively: true,
+ mocks: [
+ '15: vi.mock',
+ '16: vi.doMock',
+ '17: vi.importActual',
+ '18: vi.importMock',
+ '19: vi.resetModules',
+ ],
+ });
+ });
+});
diff --git a/packages/cli/test/drift/docs-drift.test.mjs b/packages/cli/test/drift/docs-drift.test.mjs
index 3ca1fc53a0..de21709fdc 100644
--- a/packages/cli/test/drift/docs-drift.test.mjs
+++ b/packages/cli/test/drift/docs-drift.test.mjs
@@ -6,7 +6,11 @@
*/
import {describe, it, expect} from 'vitest';
-import {runDrift} from './docs-drift.mjs';
+import {requireNatively} from '../../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('./docs-drift.mjs')} */
+const {runDrift} = require('./docs-drift.mjs');
describe('production authored-doc drift', () => {
it('every doc parses and stays in sync with its source of truth', async () => {
diff --git a/packages/cli/test/integration-check-failures.test.mjs b/packages/cli/test/integration-check-failures.test.mjs
index 362b1237ee..cdd3159005 100644
--- a/packages/cli/test/integration-check-failures.test.mjs
+++ b/packages/cli/test/integration-check-failures.test.mjs
@@ -25,14 +25,18 @@ import * as fs from 'node:fs';
import * as path from 'node:path';
import {fileURLToPath} from 'node:url';
import {afterAll, beforeAll, describe, expect, it} from 'vitest';
-import {
+import {requireNatively} from '../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../api/integration/check/check.mjs')} */
+const {
CONFLICT_CODES,
INTEGRATION_CHECKS,
NO_MANIFEST_CODE,
integrationCheck,
isCheckReport,
skippedCode,
-} from '../api/integration/check/check.mjs';
+} = require('../api/integration/check/check.mjs');
const HERE = path.dirname(fileURLToPath(import.meta.url));
const CLI = path.resolve(HERE, '..');
diff --git a/packages/cli/test/search-consumer-ranking.test.mjs b/packages/cli/test/search-consumer-ranking.test.mjs
index 14cbc3b678..3326c6000d 100644
--- a/packages/cli/test/search-consumer-ranking.test.mjs
+++ b/packages/cli/test/search-consumer-ranking.test.mjs
@@ -10,7 +10,11 @@
*/
import {describe, it, expect} from 'vitest';
-import {search} from '../api/search/search.mjs';
+import {requireNatively} from '../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../api/search/search.mjs')} */
+const {search} = require('../api/search/search.mjs');
// Each case: [query, expected-name-in-top-3, must-NOT-be-first]
/** @type {Array<[string, string, string?]>} */
diff --git a/packages/cli/test/template-icon-declarations.test.mjs b/packages/cli/test/template-icon-declarations.test.mjs
index 2620cc1b87..ac95cc06cd 100644
--- a/packages/cli/test/template-icon-declarations.test.mjs
+++ b/packages/cli/test/template-icon-declarations.test.mjs
@@ -10,8 +10,17 @@
import * as fs from 'node:fs';
import {describe, expect, it} from 'vitest';
-import {discoverCoreTemplates} from '../foundation/discovery/template-adapter.mjs';
-import {astryxDefinitionsByName} from '../foundation/discovery/icon-definition-discovery.mjs';
+import {requireNatively} from '../test-utils/native-require.mjs';
+
+const require = requireNatively(import.meta.url);
+/** @type {typeof import('../foundation/discovery/template-adapter.mjs')} */
+const {
+ discoverCoreTemplates,
+} = require('../foundation/discovery/template-adapter.mjs');
+/** @type {typeof import('../foundation/discovery/icon-definition-discovery.mjs')} */
+const {
+ astryxDefinitionsByName,
+} = require('../foundation/discovery/icon-definition-discovery.mjs');
/** The icon libraries the bundled templates import. */
const ICON_MODULE_RE =