Skip to content

test(angular-rspack): refine unit tests of create-config.ts #8

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
101 changes: 56 additions & 45 deletions packages/angular-rspack/src/lib/config/create-config.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createConfig, withConfigurations } from './create-config';
import { beforeEach, expect } from 'vitest';
import { AngularRspackPluginOptions } from '../models';
import { NgRspackPlugin } from '../plugins/ng-rspack';

describe('createConfig', () => {
const configBase: AngularRspackPluginOptions = {
Expand Down Expand Up @@ -41,10 +42,51 @@ describe('createConfig', () => {
]);
}
);
});

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

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

describe('withConfigurations', () => {
const runWithConfigurations = () => {
return withConfigurations(
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 = withConfigurations(
{ options: configBase },
{
development: {
Expand All @@ -61,50 +103,19 @@ describe('createConfig', () => {
},
}
);
};

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

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 = runWithConfigurations();

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,
})
);
}
);
});
const ngRspackPlugin = c[0].plugins?.[0] as NgRspackPlugin;
expect(ngRspackPlugin.pluginOptions).toStrictEqual(
expect.objectContaining({
browser: `./src/${fileNameSegment}.main.ts`,
skipTypeChecking,
})
);
}
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FileReplacement } from '@ng-rspack/compiler';
import { resolveFileReplacements } from './normalize-options';


describe('resolveFileReplacements', () => {

it('should resolve file replacements', () => {
const fileReplacements: FileReplacement[] = [
{ replace: 'replace', with: 'with' },
];
const root = '/root';
expect(resolveFileReplacements(fileReplacements, root)).toEqual([
{ replace: '/root/replace', with: '/root/with' },
]);

})

})
1 change: 1 addition & 0 deletions packages/angular-rspack/vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
globals: true,
environment: 'node',
include: ['src/**/*.unit.test.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
setupFiles: ['../../testing/vitest-setup/src/lib/fs-memfs.setup-file.ts'],
coverage: {
provider: 'v8',
reporter: ['text', 'lcov'],
Expand Down
6 changes: 5 additions & 1 deletion testing/vitest-setup/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
},
"include": ["vite.config.ts", "src/**/*.ts", "src/**/*.unit.test.ts"]
"include": ["vite.config.ts", "src/**/*.ts", "src/**/*.unit.test.ts"],
"references": [{
"path": "../utils/tsconfig.json"
}
]
}