Skip to content

test(angular-rspack): add unit tests for normalize options #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion e2e/fixtures/rspack-csr-css/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@
],
"references": [
{
"path": "../../../testing/utils/tsconfig.lib.json"
"path": "../../../packages/compiler/tsconfig.lib.json"
},
{
"path": "../../../packages/angular-rspack-compiler/tsconfig.lib.json"
},
{
"path": "../../../testing/vitest-setup/tsconfig.lib.json"
},
{
"path": "../../../testing/setup/tsconfig.lib.json"
},
{
"path": "../../../testing/utils/tsconfig.lib.json"
},
{
"path": "../../../packages/angular-rspack/tsconfig.lib.json"
}
Expand Down
8 changes: 7 additions & 1 deletion e2e/fixtures/rspack-csr-css/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
"include": [],
"references": [
{
"path": "../../../testing/utils"
"path": "../../../packages/compiler"
},
{
"path": "../../../packages/angular-rspack-compiler"
},
{
"path": "../../../testing/vitest-setup"
},
{
"path": "../../../testing/setup"
},
{
"path": "../../../testing/utils"
},
{
"path": "../../../packages/angular-rspack"
},
Expand Down
8 changes: 7 additions & 1 deletion e2e/fixtures/rspack-ssr-css/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@
],
"references": [
{
"path": "../../../testing/utils/tsconfig.lib.json"
"path": "../../../packages/compiler/tsconfig.lib.json"
},
{
"path": "../../../packages/angular-rspack-compiler/tsconfig.lib.json"
},
{
"path": "../../../testing/vitest-setup/tsconfig.lib.json"
},
{
"path": "../../../testing/setup/tsconfig.lib.json"
},
{
"path": "../../../testing/utils/tsconfig.lib.json"
},
{
"path": "../../../packages/angular-rspack/tsconfig.lib.json"
}
Expand Down
8 changes: 7 additions & 1 deletion e2e/fixtures/rspack-ssr-css/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@
"include": [],
"references": [
{
"path": "../../../testing/utils"
"path": "../../../packages/compiler"
},
{
"path": "../../../packages/angular-rspack-compiler"
},
{
"path": "../../../testing/vitest-setup"
},
{
"path": "../../../testing/setup"
},
{
"path": "../../../testing/utils"
},
{
"path": "../../../packages/angular-rspack"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/angular-rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"express": "4.21.1"
},
"devDependencies": {
"@ng-rspack/testing-setup": "workspace:*"
"@ng-rspack/testing-utils": "workspace:*",
"@ng-rspack/testing-setup": "workspace:*",
"@ng-rspack/testing-vitest-setup": "workspace:*"
},
"peerDependencies": {
"@rspack/core": ">=1.0.5 <2.0.0",
Expand Down
151 changes: 81 additions & 70 deletions packages/angular-rspack/src/lib/config/create-config.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { _createConfig, createConfig } from './create-config';
import { beforeEach, expect } from 'vitest';
import { AngularRspackPluginOptions } from '../models';
import { NgRspackPlugin } from '../plugins/ng-rspack';

describe('createConfig', () => {
describe('_createConfig', () => {
const configBase: AngularRspackPluginOptions = {
root: '',
browser: './src/main.ts',
Expand Down Expand Up @@ -41,10 +42,76 @@ describe('createConfig', () => {
]);
}
);
});

describe('createConfig', () => {
const configBase: AngularRspackPluginOptions = {
root: '',
browser: './src/main.ts',
index: './src/index.html',
tsConfig: './tsconfig.base.json',
inlineStyleLanguage: 'css',
polyfills: [],
styles: [],
assets: [],
fileReplacements: [],
scripts: [],
aot: true,
hasServer: false,
skipTypeChecking: false,
};

it('should create config from options', () => {
expect(createConfig({ options: configBase })).toStrictEqual([
expect.objectContaining({
mode: 'development',
devServer: expect.objectContaining({
port: 4200,
}),
plugins: [
{
pluginOptions: {
...configBase,
useTsProjectReferences: false,
polyfills: ['zone.js'],
devServer: {
port: 4200,
},
},
},
],
}),
]);
});

it('should allow changing the devServer port', () => {
expect(
createConfig({
options: {
...configBase,
devServer: {
port: 8080,
},
},
})
).toStrictEqual([
expect.objectContaining({
devServer: expect.objectContaining({
port: 8080,
}),
}),
]);
});

describe('createConfig', () => {
const runCreateConfig = () => {
return createConfig(
it.each([
['development', 'dev', true],
['production', 'prod', false],
])(
'should create config for mode "development" if env variable NGRS_CONFIG is "%s"',
(configuration, fileNameSegment, skipTypeChecking) => {
vi.stubEnv('NGRS_CONFIG', configuration);

const c = createConfig(
{ options: configBase },
{
development: {
Expand All @@ -61,75 +128,19 @@ describe('createConfig', () => {
},
}
);
};

it('should create config from options', () => {
expect(createConfig({ options: configBase })).toStrictEqual([
expect(c).toStrictEqual([
expect.objectContaining({
mode: 'development',
devServer: expect.objectContaining({
port: 4200,
}),
plugins: [
{
pluginOptions: {
...configBase,
useTsProjectReferences: false,
polyfills: ['zone.js'],
devServer: {
port: 4200,
},
},
},
],
plugins: [expect.any(NgRspackPlugin)],
}),
]);
});

it('should allow changing the devServer port', () => {
expect(
createConfig({
options: {
...configBase,
devServer: {
port: 8080,
},
},
})
).toStrictEqual([
const ngRspackPlugin = c[0].plugins?.[0] as NgRspackPlugin;
expect(ngRspackPlugin.pluginOptions).toStrictEqual(
expect.objectContaining({
devServer: expect.objectContaining({
port: 8080,
}),
}),
]);
});

it.each([
['development', 'dev', true],
['production', 'prod', false],
])(
'should create config for mode "development" if env variable NGRS_CONFIG is "%s"',
(configuration, fileNameSegment, skipTypeChecking) => {
vi.stubEnv('NGRS_CONFIG', configuration);

const config = runCreateConfig();

const plugins = config[0].plugins;
const NgRspackPlugin = plugins?.find(
(plugin) => plugin?.constructor.name === 'NgRspackPlugin'
);
expect(NgRspackPlugin).toBeDefined();
expect(
// @ts-expect-error - TS cannot index correctly because of multiple potential types
NgRspackPlugin['pluginOptions'] as AngularRspackPluginOptions
).toEqual(
expect.objectContaining({
browser: `./src/${fileNameSegment}.main.ts`,
skipTypeChecking,
})
);
}
);
});
browser: `./src/${fileNameSegment}.main.ts`,
skipTypeChecking,
})
);
}
);
});
65 changes: 47 additions & 18 deletions packages/angular-rspack/src/lib/models/normalize-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ export function resolveFileReplacements(
}));
}

export type HasServerOptions = Pick<
AngularRspackPluginOptions,
'server' | 'ssrEntry' | 'root'
>;

export function getHasServer({
server,
ssrEntry,
root,
}: Pick<AngularRspackPluginOptions, 'server' | 'ssrEntry' | 'root'>): boolean {
}: HasServerOptions): boolean {
return !!(
server &&
ssrEntry &&
Expand All @@ -33,6 +38,22 @@ export function getHasServer({
);
}

export const DEFAULT_NORMALIZED_OPTIONS: Omit<
AngularRspackPluginOptions,
'root' | 'tsConfig' | 'fileReplacements' | 'ssrEntry' | 'server' | 'hasServer'
> = {
index: './src/index.html',
browser: './src/main.ts',
polyfills: [],
assets: ['./public'],
styles: ['./src/styles.css'],
scripts: [],
aot: true,
inlineStyleLanguage: 'css',
skipTypeChecking: false,
useTsProjectReferences: false,
} as const;

export function normalizeOptions(
options: Partial<AngularRspackPluginOptions> = {}
): AngularRspackPluginOptions {
Expand All @@ -41,32 +62,40 @@ export function normalizeOptions(
fileReplacements = [],
server,
ssrEntry,
devServer,
...restOptions
} = options;

return {
...DEFAULT_NORMALIZED_OPTIONS,
...restOptions,
root,
index: options.index ?? './src/index.html',
browser: options.browser ?? './src/main.ts',
...(server ? { server } : {}),
...(ssrEntry ? { ssrEntry } : {}),
polyfills: options.polyfills ?? [],
assets: options.assets ?? ['./public'],
styles: options.styles ?? ['./src/styles.css'],
scripts: options.scripts ?? [],
fileReplacements: resolveFileReplacements(fileReplacements, root),
aot: options.aot ?? true,
inlineStyleLanguage: options.inlineStyleLanguage ?? 'css',
tsConfig: options.tsConfig ?? join(root, 'tsconfig.app.json'),
hasServer: getHasServer({ server, ssrEntry, root }),
skipTypeChecking: options.skipTypeChecking ?? false,
useTsProjectReferences: options.useTsProjectReferences ?? false,
devServer: options.devServer
fileReplacements: resolveFileReplacements(fileReplacements, root),
...normalizeOptionsServerOptions({ server, ssrEntry, root }),
devServer: devServer
? {
...options.devServer,
port: options.devServer.port ?? 4200,
...devServer,
port: devServer.port ?? 4200,
}
: {
port: 4200,
},
};
}

export function normalizeOptionsServerOptions({
server,
ssrEntry,
root,
}: Pick<AngularRspackPluginOptions, 'root' | 'server' | 'ssrEntry'>) {
if (!getHasServer({ server, ssrEntry, root })) {
return { hasServer: false };
}

return {
hasServer: true,
server,
ssrEntry,
};
}
Loading
Loading