fix: prefer source plugin-sdk root alias in tests

This commit is contained in:
Peter Steinberger
2026-03-22 12:24:13 -07:00
parent f85cfc8b6c
commit e64dbb00b3
2 changed files with 22 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ let monolithicSdk = null;
let diagnosticEventsModule = null;
const jitiLoaders = new Map();
const pluginSdkSubpathsCache = new Map();
const shouldPreferSourceInTests = Boolean(process.env.VITEST) || process.env.NODE_ENV === "test";
function emptyPluginConfigSchema() {
function error(message) {
@@ -142,7 +143,7 @@ function loadMonolithicSdk() {
}
const distCandidate = path.resolve(__dirname, "..", "..", "dist", "plugin-sdk", "compat.js");
if (fs.existsSync(distCandidate)) {
if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) {
try {
monolithicSdk = getJiti(true)(distCandidate);
return monolithicSdk;
@@ -168,7 +169,7 @@ function loadDiagnosticEventsModule() {
"infra",
"diagnostic-events.js",
);
if (fs.existsSync(distCandidate)) {
if (!shouldPreferSourceInTests && fs.existsSync(distCandidate)) {
try {
diagnosticEventsModule = getJiti(true)(distCandidate);
return diagnosticEventsModule;

View File

@@ -22,6 +22,7 @@ type EmptySchema = {
function loadRootAliasWithStubs(options?: {
distExists?: boolean;
env?: Record<string, string | undefined>;
monolithicExports?: Record<string | symbol, unknown>;
}) {
let createJitiCalls = 0;
@@ -33,7 +34,11 @@ function loadRootAliasWithStubs(options?: {
};
const wrapper = vm.runInNewContext(
`(function (exports, require, module, __filename, __dirname) {${rootAliasSource}\n})`,
{},
{
process: {
env: options?.env ?? {},
},
},
{ filename: rootAliasPath },
) as (
exports: Record<string, unknown>,
@@ -157,6 +162,19 @@ describe("plugin-sdk root alias", () => {
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(true);
});
it("prefers source loading under vitest even when compat resolves to dist", () => {
const lazyModule = loadRootAliasWithStubs({
distExists: true,
env: { VITEST: "1" },
monolithicExports: {
slowHelper: () => "loaded",
},
});
expect((lazyModule.moduleExports.slowHelper as () => string)()).toBe("loaded");
expect(lazyModule.createJitiOptions.at(-1)?.tryNative).toBe(false);
});
it("forwards delegateCompactionToRuntime through the compat-backed root alias", () => {
const delegateCompactionToRuntime = () => "delegated";
const lazyModule = loadRootAliasWithStubs({