|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { createRequire } from 'node:module'; |
| 3 | +import { expect, rs, test } from 'rstack/test'; |
| 4 | +import { |
| 5 | + composeConfigLayers, |
| 6 | + resolveConfigLayers, |
| 7 | +} from '../../src/configLayers.ts'; |
| 8 | +import { resolveFmtConfig } from '../../src/fmt/config.ts'; |
| 9 | +import { resolveRstestConfig } from '../../src/rstestConfig.ts'; |
| 10 | + |
| 11 | +// TODO: Import the source directly after upgrading Rstest to fix |
| 12 | +// https://github.com/web-infra-dev/rstest/issues/1891. |
| 13 | +rs.mock( |
| 14 | + '../../src/rstestConfig.ts', |
| 15 | + () => |
| 16 | + createRequire(import.meta.url)( |
| 17 | + '../../dist/rstestConfig.js', |
| 18 | + ) as typeof import('../../src/rstestConfig.ts'), |
| 19 | +); |
| 20 | + |
| 21 | +test('preserves single definitions and leaves missing tools absent', async () => { |
| 22 | + const app = rs.fn(() => ({})); |
| 23 | + const base = { app }; |
| 24 | + const fmt = {}; |
| 25 | + |
| 26 | + expect(await composeConfigLayers([])).toEqual({}); |
| 27 | + expect(await composeConfigLayers([base])).toBe(base); |
| 28 | + const configs = await composeConfigLayers([base, { fmt }]); |
| 29 | + expect(configs).toEqual({ app, fmt }); |
| 30 | + expect(configs.fmt).toBe(fmt); |
| 31 | + expect(app).not.toHaveBeenCalled(); |
| 32 | +}); |
| 33 | + |
| 34 | +test('resolves only the requested tool and preserves staged task generators', async () => { |
| 35 | + const app = rs.fn(() => ({})); |
| 36 | + const fmt = rs.fn(() => ({ semi: false })); |
| 37 | + const staged = rs.fn(() => 'rs check'); |
| 38 | + const configs = await composeConfigLayers([ |
| 39 | + { app, fmt, staged: { '*.ts': 'rs lint' } }, |
| 40 | + { app: {}, fmt: { singleQuote: true }, staged }, |
| 41 | + ]); |
| 42 | + |
| 43 | + expect(fmt).not.toHaveBeenCalled(); |
| 44 | + expect( |
| 45 | + await resolveFmtConfig({ |
| 46 | + layers: [configs], |
| 47 | + configFilePath: '/project/rstack.config.ts', |
| 48 | + cwd: '/other', |
| 49 | + }), |
| 50 | + ).toEqual({ |
| 51 | + rootPath: '/project', |
| 52 | + baseOptions: { semi: false, singleQuote: true }, |
| 53 | + overrides: [], |
| 54 | + ignorePatterns: [], |
| 55 | + }); |
| 56 | + expect(fmt).toHaveBeenCalledExactlyOnceWith(); |
| 57 | + expect(app).not.toHaveBeenCalled(); |
| 58 | + expect(configs.staged).toBe(staged); |
| 59 | + expect(staged).not.toHaveBeenCalled(); |
| 60 | +}); |
| 61 | + |
| 62 | +test('applies automatic inheritance after merging test definitions', async () => { |
| 63 | + const app = rs.fn(() => ({ source: { define: { SHARED: true } } })); |
| 64 | + const configs = await composeConfigLayers([ |
| 65 | + { app, test: { retry: 1 } }, |
| 66 | + { |
| 67 | + app: { source: { define: { PROJECT: true } } }, |
| 68 | + test: { testTimeout: 5000 }, |
| 69 | + }, |
| 70 | + ]); |
| 71 | + |
| 72 | + expect(await resolveConfigLayers([configs], 'test')).toEqual([ |
| 73 | + { retry: 1, testTimeout: 5000 }, |
| 74 | + ]); |
| 75 | + expect(app).not.toHaveBeenCalled(); |
| 76 | + |
| 77 | + const params = { command: 'build', env: 'production' } as const; |
| 78 | + const config = await resolveRstestConfig([configs], params); |
| 79 | + assert(typeof config.extends === 'function'); |
| 80 | + expect(config).toEqual({ |
| 81 | + retry: 1, |
| 82 | + testTimeout: 5000, |
| 83 | + extends: config.extends, |
| 84 | + }); |
| 85 | + expect(await config.extends(config)).toMatchObject({ |
| 86 | + source: { define: { SHARED: true, PROJECT: true } }, |
| 87 | + }); |
| 88 | + expect(app).toHaveBeenCalledExactlyOnceWith(params); |
| 89 | +}); |
0 commit comments