Skip to content

Commit

Permalink
test: some more test (#43)
Browse files Browse the repository at this point in the history
* test: some more test

* fix: lint

* fix: minor changes
  • Loading branch information
MadaraUchiha-314 authored Mar 10, 2024
1 parent d099581 commit 5424b6f
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
6 changes: 1 addition & 5 deletions packages/rollup-plugin-module-federation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname, sep } from 'node:path';
import { fileURLToPath } from 'node:url';
import { resolve, sep } from 'node:path';
import { EOL } from 'node:os';

import { asyncWalk } from 'estree-walker';
Expand Down Expand Up @@ -64,9 +63,6 @@ const FEDERATION_RUNTIME_PLUGIN = '__module_federation_runtime__plugin__';

const MODULE_VERSION_UNSPECIFIED: string = '0.0.0';

const __filename: string = fileURLToPath(import.meta.url);
const __dirname: string = dirname(__filename);

export function getFederatedImportStatementForNode(
node: NodesToRewrite,
{
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup-plugin-module-federation/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function getChunkNameForModule({
* Returning an empty string might cause undefined behavior.
*/
if (!sanitizedModuleNameOrPath) {
throw Error(`Invalid module name provided: ${sanitizeModuleName}`);
throw Error(`Invalid module name provided: ${sanitizedModuleNameOrPath}`);
}
if (type === 'shared') {
return generateShareFilename(sanitizedModuleNameOrPath, false);
Expand Down
138 changes: 138 additions & 0 deletions packages/rollup-plugin-module-federation/tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getExposesConfig,
getRemotesConfig,
getRequiredVersionForModule,
getInitConfig,
} from '../src/utils.ts';

jest.mock('node:fs');
Expand Down Expand Up @@ -56,6 +57,13 @@ describe('utils.ts', () => {
})).toThrowError('Generating chunk name for invalid is not supported');
});

test('getChunkNameForModule throws error for invalid type', () => {
expect(() => getChunkNameForModule({
sanitizedModuleNameOrPath: null,
type: 'invalid',
})).toThrowError('Invalid module name provided: null');
});

test('getFileNameFromChunkName creates file path from chunk name', () => {
expect(getFileNameFromChunkName('chunk-name')).toBe('./chunk-name.js');
});
Expand Down Expand Up @@ -130,6 +138,22 @@ describe('getSharedConfig', () => {
expect(getSharedConfig(shared)).toEqual(expected);
});

test('should handle array input', () => {
const shared = [
{
module1: { import: 'import1', otherKey: 'otherValue' },
},
{
module2: 'import2',
},
];
const expected = {
module1: { import: 'import1', otherKey: 'otherValue' },
module2: { import: 'import2' },
};
expect(getSharedConfig(shared)).toEqual(expected);
});

test('should throw error for invalid array input', () => {
const shared = ['module1', 123];
expect(() => getSharedConfig(shared)).toThrowError();
Expand Down Expand Up @@ -160,6 +184,22 @@ describe('getExposesConfig', () => {
expect(getExposesConfig(input)).toEqual(expected);
});

test('should handle array input', () => {
const input = [
{
module1: 'module1',
},
{
module2: 'module2',
},
];
const expected = {
module1: { import: 'module1' },
module2: { import: 'module2' },
};
expect(getExposesConfig(input)).toEqual(expected);
});

test('should throw error for invalid array item', () => {
const input = ['module1', 123];
expect(() => getExposesConfig(input)).toThrowError();
Expand Down Expand Up @@ -289,3 +329,101 @@ describe('getRequiredVersionForModule', () => {
expect(result).toBe(false);
});
});

describe('getInitConfig', () => {
test('should return the empty configuration when no shared or remote modules are present', () => {
const name = 'testName';
const shared = {};
const remotes = {};
const federatedModuleInfo = {};

const remoteType = 'module';

const result = getInitConfig(
name,
shared,
remotes,
federatedModuleInfo,
remoteType,
);

expect(result).toEqual({
name: 'testName',
shared: {},
plugins: [],
remotes: [],
});
});

test('should return the correct configuration when remote modules are present', () => {
const name = 'testName';
const shared = {
sharedPkg1: {
import: 'sharedPkg1',
},
sharedPkg2: {
import: false,
},
};
const remotes = {
remote1: {
external: 'remote1',
},
};
const federatedModuleInfo = {
sharedPkg1: {
moduleNameOrPath: 'sharedPkg1',
versionInfo: { requiredVersion: '1.0.0' },
},
sharedPkg2: {
moduleNameOrPath: 'sharedPkg2',
versionInfo: { requiredVersion: '1.0.0' },
},
};
const remoteType = 'module';

const result = getInitConfig(
name,
shared,
remotes,
federatedModuleInfo,
remoteType,
);

expect(result).toEqual({
name: 'testName',
shared: {
sharedPkg1: {
version: undefined,
shareConfig: {
singleton: undefined,
requiredVersion: '1.0.0',
eager: undefined,
},
scope: undefined,
lib: expect.any(Function),
},
sharedPkg2: {
version: undefined,
strategy: 'loaded-first',
shareConfig: {
singleton: undefined,
requiredVersion: '1.0.0',
eager: undefined,
},
scope: undefined,
lib: expect.any(Function),
},
},
plugins: [],
remotes: [
{
name: 'remote1',
entry: 'remote1',
shareScope: undefined,
type: 'esm',
},
],
});
});
});

0 comments on commit 5424b6f

Please sign in to comment.