Skip to content

Commit 587d012

Browse files
JammingBengioboa
andauthored
fix: skip all federation plugins in test env (#595)
--------- Co-authored-by: gioboa <giorgiob.boa@gmail.com>
1 parent dc0e52c commit 587d012

5 files changed

Lines changed: 105 additions & 5 deletions

File tree

src/__tests__/index.test.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Plugin } from 'vite';
2-
import { beforeEach, describe, expect, it, vi } from 'vitest';
2+
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
33
import { getLoadShareImportId } from '../virtualModules/virtualShared_preBuild';
44

55
const { hasPackageDependencyMock, mfWarn } = vi.hoisted(() => ({
@@ -99,6 +99,37 @@ function getModuleFederationVitePlugin(): Plugin {
9999
return plugin;
100100
}
101101

102+
describe('federation in test environment', () => {
103+
const originalEnv = process.env;
104+
105+
beforeEach(() => {
106+
process.env = {};
107+
});
108+
109+
afterAll(() => {
110+
process.env = originalEnv;
111+
});
112+
113+
it('returns empty plugin array when in test environment', () => {
114+
process.env.NODE_ENV = 'test';
115+
const plugins = federation({
116+
name: 'host',
117+
filename: 'remoteEntry.js',
118+
});
119+
expect(plugins).toEqual([]);
120+
});
121+
122+
it('returns plugins when MFE_VITE_NO_TEST_ENV_CHECK is true', () => {
123+
process.env.NODE_ENV = 'test';
124+
process.env.MFE_VITE_NO_TEST_ENV_CHECK = 'true';
125+
const plugins = federation({
126+
name: 'host',
127+
filename: 'remoteEntry.js',
128+
});
129+
expect(plugins.length).toBeGreaterThan(0);
130+
});
131+
});
132+
102133
describe('module-federation-esm-shims', () => {
103134
beforeEach(() => {
104135
vi.clearAllMocks();
@@ -213,7 +244,10 @@ describe('vite:module-federation-early-init', () => {
213244
};
214245

215246
const configHook = typeof plugin.config === 'function' ? plugin.config : plugin.config?.handler;
216-
configHook?.call({ meta: {} } as any, config, { command: 'serve', mode: 'test' });
247+
configHook?.call({ meta: {} } as any, config, {
248+
command: 'serve',
249+
mode: 'test',
250+
});
217251

218252
expect(config.optimizeDeps.include).toContain(getPreBuildLibImportId('vue'));
219253
expect(config.optimizeDeps.include).toContain(getLoadShareImportId('vue', false, 'serve'));
@@ -253,7 +287,10 @@ describe('vite:module-federation-early-init', () => {
253287
};
254288

255289
const configHook = typeof plugin.config === 'function' ? plugin.config : plugin.config?.handler;
256-
configHook?.call({ meta: {} } as any, config, { command: 'build', mode: 'test' });
290+
configHook?.call({ meta: {} } as any, config, {
291+
command: 'build',
292+
mode: 'test',
293+
});
257294

258295
expect(config.define.ENV_TARGET).toBe('undefined');
259296
});
@@ -273,7 +310,10 @@ describe('vite:module-federation-early-init', () => {
273310
};
274311

275312
const configHook = typeof plugin.config === 'function' ? plugin.config : plugin.config?.handler;
276-
configHook?.call({ meta: {} } as any, config, { command: 'build', mode: 'test' });
313+
configHook?.call({ meta: {} } as any, config, {
314+
command: 'build',
315+
mode: 'test',
316+
});
277317

278318
expect(config.define.ENV_TARGET).toBe('"node"');
279319
});
@@ -304,7 +344,10 @@ describe('vite:module-federation-early-init with import: false', () => {
304344
};
305345

306346
const configHook = typeof plugin.config === 'function' ? plugin.config : plugin.config?.handler;
307-
configHook?.call({ meta: {} } as any, config, { command: 'serve', mode: 'test' });
347+
configHook?.call({ meta: {} } as any, config, {
348+
command: 'serve',
349+
mode: 'test',
350+
});
308351

309352
// Should not include prebuild or loadShare for import: false deps
310353
const includeStr = config.optimizeDeps.include.join(',');

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { proxySharedModule } from './plugins/pluginProxySharedModule_preBuild';
1616
import { pluginRemoteNamedExports } from './plugins/pluginRemoteNamedExports';
1717
import pluginVarRemoteEntry from './plugins/pluginVarRemoteEntry';
1818
import aliasToArrayPlugin from './utils/aliasToArrayPlugin';
19+
import { isTestEnv } from './utils/isTestEnv';
1920
import { resolveProxyAlias } from './utils/bundleHelpers';
2021
import {
2122
isFederationControlChunk,
@@ -167,6 +168,7 @@ function createEarlyVirtualModulesPlugin(options: NormalizedModuleFederationOpti
167168
}
168169

169170
function federation(mfUserOptions: ModuleFederationOptions): Plugin[] {
171+
if (isTestEnv()) return [];
170172
const options = normalizeModuleFederationOptions(mfUserOptions);
171173
const isVinext = hasPackageDependency('vinext');
172174
const { name, remotes, shared, filename, hostInitInjectLocation } = options;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { afterAll, beforeEach, describe, expect, it } from 'vitest';
2+
import { isTestEnv } from '../isTestEnv';
3+
4+
describe('isTestEnv', () => {
5+
const originalEnv = process.env;
6+
7+
beforeEach(() => {
8+
process.env = {};
9+
});
10+
11+
afterAll(() => {
12+
process.env = originalEnv;
13+
});
14+
15+
it('returns true when NODE_ENV is test', () => {
16+
process.env.NODE_ENV = 'test';
17+
expect(isTestEnv()).toBe(true);
18+
});
19+
20+
it('returns true when VITEST is set', () => {
21+
process.env.VITEST = 'true';
22+
expect(isTestEnv()).toBe(true);
23+
});
24+
25+
it('returns true when JEST_WORKER_ID is set', () => {
26+
process.env.JEST_WORKER_ID = '1';
27+
expect(isTestEnv()).toBe(true);
28+
});
29+
30+
it('returns false when no test env vars are set', () => {
31+
expect(isTestEnv()).toBe(false);
32+
});
33+
34+
it('returns false when MFE_VITE_NO_TEST_ENV_CHECK is true, even in test env', () => {
35+
process.env.NODE_ENV = 'test';
36+
process.env.MFE_VITE_NO_TEST_ENV_CHECK = 'true';
37+
expect(isTestEnv()).toBe(false);
38+
});
39+
});

src/utils/isTestEnv.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Detects whether the current process is running in a test environment
3+
* Set `MFE_VITE_NO_TEST_ENV_CHECK=true` to load federation plugins during tests.
4+
*/
5+
export function isTestEnv() {
6+
if (process.env.MFE_VITE_NO_TEST_ENV_CHECK === 'true') return false;
7+
8+
return (
9+
process.env.NODE_ENV === 'test' ||
10+
process.env.VITEST != null ||
11+
process.env.JEST_WORKER_ID != null
12+
);
13+
}

vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ export default defineConfig({
44
test: {
55
globals: true,
66
exclude: ['**/e2e/**', '**/node_modules/**'],
7+
env: {
8+
MFE_VITE_NO_TEST_ENV_CHECK: 'true',
9+
},
710
},
811
});

0 commit comments

Comments
 (0)