|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import type { Configs } from '../../src/config.ts'; |
| 4 | +import { expect, rs, test } from 'rstack/test'; |
| 5 | +import { |
| 6 | + composeConfigLayers, |
| 7 | + resolveConfigLayers, |
| 8 | +} from '../../src/configLayers.ts'; |
| 9 | +import { resolveFmtConfig } from '../../src/fmt/config.ts'; |
| 10 | +import { resolveRstestConfig } from '../../src/rstestConfig.ts'; |
| 11 | + |
| 12 | +// TODO: Import the source directly after upgrading Rstest to fix |
| 13 | +// https://github.com/web-infra-dev/rstest/issues/1891. |
| 14 | +rs.mock( |
| 15 | + '../../src/rstestConfig.ts', |
| 16 | + () => |
| 17 | + createRequire(import.meta.url)( |
| 18 | + '../../dist/rstestConfig.js', |
| 19 | + ) as typeof import('../../src/rstestConfig.ts'), |
| 20 | +); |
| 21 | + |
| 22 | +const params = { command: 'build', env: 'production' } as const; |
| 23 | + |
| 24 | +test('preserves single definitions and leaves missing tools absent', async () => { |
| 25 | + const app = rs.fn(() => ({})); |
| 26 | + const base: Configs = { app, lint: [] }; |
| 27 | + const project: Configs = { fmt: {} }; |
| 28 | + |
| 29 | + expect(await composeConfigLayers([])).toEqual({}); |
| 30 | + expect(await composeConfigLayers([base])).toBe(base); |
| 31 | + const configs = await composeConfigLayers([base, project]); |
| 32 | + expect(configs).toEqual({ app, lint: base.lint, fmt: project.fmt }); |
| 33 | + expect(configs.lint).toBe(base.lint); |
| 34 | + expect(configs.fmt).toBe(project.fmt); |
| 35 | + expect(app).not.toHaveBeenCalled(); |
| 36 | +}); |
| 37 | + |
| 38 | +test('composes tool definitions lazily and resolves each factory once', async () => { |
| 39 | + const app = rs.fn(() => ({ source: { define: { APP: true } } })); |
| 40 | + const lib = rs.fn(() => ({ lib: [{ format: 'esm' as const }] })); |
| 41 | + const doc = rs.fn(() => Promise.resolve({ title: 'Shared' })); |
| 42 | + const lint = rs.fn(() => |
| 43 | + Promise.resolve([{ rules: { 'no-debugger': 'error' as const } }]), |
| 44 | + ); |
| 45 | + const fmt = rs.fn(() => ({ semi: false })); |
| 46 | + const staged = rs.fn(() => 'rs check'); |
| 47 | + const configs = await composeConfigLayers([ |
| 48 | + { app, lib, doc, lint, fmt, staged: { '*.ts': 'rs lint' } }, |
| 49 | + { |
| 50 | + app: {}, |
| 51 | + lib: {}, |
| 52 | + doc: { title: 'Project' }, |
| 53 | + lint: [], |
| 54 | + fmt: { singleQuote: true }, |
| 55 | + staged, |
| 56 | + }, |
| 57 | + ]); |
| 58 | + |
| 59 | + for (const factory of [app, lib, doc, lint, fmt, staged]) { |
| 60 | + expect(factory).not.toHaveBeenCalled(); |
| 61 | + } |
| 62 | + expect(await resolveConfigLayers([configs], 'app', params)).toEqual([ |
| 63 | + { source: { define: { APP: true } } }, |
| 64 | + ]); |
| 65 | + expect(lib).not.toHaveBeenCalled(); |
| 66 | + expect(doc).not.toHaveBeenCalled(); |
| 67 | + expect(await resolveConfigLayers([configs], 'lib', params)).toEqual([ |
| 68 | + { lib: [{ format: 'esm' }] }, |
| 69 | + ]); |
| 70 | + expect(await resolveConfigLayers([configs], 'doc')).toEqual([ |
| 71 | + { title: 'Project' }, |
| 72 | + ]); |
| 73 | + expect(await resolveConfigLayers([configs], 'lint')).toEqual([ |
| 74 | + [{ rules: { 'no-debugger': 'error' } }], |
| 75 | + ]); |
| 76 | + expect( |
| 77 | + await resolveFmtConfig({ |
| 78 | + layers: [configs], |
| 79 | + configFilePath: '/project/rstack.config.ts', |
| 80 | + cwd: '/other', |
| 81 | + }), |
| 82 | + ).toEqual({ |
| 83 | + rootPath: '/project', |
| 84 | + baseOptions: { semi: false, singleQuote: true }, |
| 85 | + overrides: [], |
| 86 | + ignorePatterns: [], |
| 87 | + }); |
| 88 | + expect(configs.staged).toBe(staged); |
| 89 | + expect(staged).not.toHaveBeenCalled(); |
| 90 | + expect(app).toHaveBeenCalledExactlyOnceWith(params); |
| 91 | + expect(lib).toHaveBeenCalledExactlyOnceWith(params); |
| 92 | + for (const factory of [doc, lint, fmt]) { |
| 93 | + expect(factory).toHaveBeenCalledExactlyOnceWith(); |
| 94 | + } |
| 95 | +}); |
| 96 | + |
| 97 | +test('defers test inheritance until the consumer resolves the composed config', async () => { |
| 98 | + const app = rs.fn(() => ({ source: { define: { SHARED: true } } })); |
| 99 | + const configs = await composeConfigLayers([ |
| 100 | + { app, test: { retry: 1 } }, |
| 101 | + { |
| 102 | + app: { source: { define: { PROJECT: true } } }, |
| 103 | + test: { projects: [{ name: 'a' }, { name: 'b' }] }, |
| 104 | + }, |
| 105 | + ]); |
| 106 | + const raw = await resolveConfigLayers([configs], 'test'); |
| 107 | + expect(raw).toEqual([{ retry: 1, projects: [{ name: 'a' }, { name: 'b' }] }]); |
| 108 | + expect(app).not.toHaveBeenCalled(); |
| 109 | + |
| 110 | + const config = await resolveRstestConfig([configs], params); |
| 111 | + const first = config.projects?.[0]; |
| 112 | + assert( |
| 113 | + first && typeof first !== 'string' && typeof first.extends === 'function', |
| 114 | + ); |
| 115 | + expect(config).toEqual({ |
| 116 | + retry: 1, |
| 117 | + projects: [ |
| 118 | + { name: 'a', extends: first.extends }, |
| 119 | + { name: 'b', extends: first.extends }, |
| 120 | + ], |
| 121 | + }); |
| 122 | + expect(await first.extends(first)).toMatchObject({ |
| 123 | + source: { define: { SHARED: true, PROJECT: true } }, |
| 124 | + }); |
| 125 | + expect(app).toHaveBeenCalledExactlyOnceWith(params); |
| 126 | +}); |
0 commit comments