diff --git a/packages/angular-rspack/src/lib/config/create-config.unit.test.ts b/packages/angular-rspack/src/lib/config/create-config.unit.test.ts index 9cb0e60..04a151d 100644 --- a/packages/angular-rspack/src/lib/config/create-config.unit.test.ts +++ b/packages/angular-rspack/src/lib/config/create-config.unit.test.ts @@ -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 = { @@ -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: { @@ -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, + }) + ); + } + ); }); diff --git a/packages/angular-rspack/src/lib/models/normalize-options.unit.test.ts b/packages/angular-rspack/src/lib/models/normalize-options.unit.test.ts new file mode 100644 index 0000000..5e61836 --- /dev/null +++ b/packages/angular-rspack/src/lib/models/normalize-options.unit.test.ts @@ -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' }, + ]); + + }) + +}) diff --git a/packages/angular-rspack/vitest.config.mts b/packages/angular-rspack/vitest.config.mts index f324f96..2585799 100644 --- a/packages/angular-rspack/vitest.config.mts +++ b/packages/angular-rspack/vitest.config.mts @@ -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'], diff --git a/testing/vitest-setup/tsconfig.spec.json b/testing/vitest-setup/tsconfig.spec.json index 5eae43d..cc79cb9 100644 --- a/testing/vitest-setup/tsconfig.spec.json +++ b/testing/vitest-setup/tsconfig.spec.json @@ -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" + } + ] }