|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { afterEach, describe, expect, it } from 'bun:test'; |
| 5 | + |
| 6 | +import { collectTestFiles, planFrontendTestRuns, usesMockModule } from './run-tests-plan'; |
| 7 | + |
| 8 | +describe('frontend test runner planning', () => { |
| 9 | + let tempDir: string | null = null; |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + if (tempDir) { |
| 13 | + rmSync(tempDir, { recursive: true, force: true }); |
| 14 | + tempDir = null; |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it('collects test files recursively in deterministic order', () => { |
| 19 | + tempDir = mkdtempSync(path.join(tmpdir(), 'sentris-test-plan-')); |
| 20 | + writeFileSync(path.join(tempDir, 'z.test.ts'), ''); |
| 21 | + writeFileSync(path.join(tempDir, 'component.ts'), ''); |
| 22 | + |
| 23 | + const nested = path.join(tempDir, 'nested'); |
| 24 | + mkdirSync(nested); |
| 25 | + writeFileSync(path.join(tempDir, 'a.spec.tsx'), ''); |
| 26 | + writeFileSync(path.join(tempDir, 'readme.md'), ''); |
| 27 | + writeFileSync(path.join(nested, 'b.test.ts'), ''); |
| 28 | + |
| 29 | + expect(collectTestFiles(tempDir).map((file) => path.relative(tempDir!, file))).toEqual([ |
| 30 | + 'a.spec.tsx', |
| 31 | + path.join('nested', 'b.test.ts'), |
| 32 | + 'z.test.ts', |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('detects direct mock.module usage', () => { |
| 37 | + tempDir = mkdtempSync(path.join(tmpdir(), 'sentris-test-plan-')); |
| 38 | + const mocked = path.join(tempDir, 'mocked.test.ts'); |
| 39 | + const plain = path.join(tempDir, 'plain.test.ts'); |
| 40 | + |
| 41 | + writeFileSync( |
| 42 | + mocked, |
| 43 | + "import { mock } from 'bun:test';\nmock.module('@/store', () => ({}));\n", |
| 44 | + ); |
| 45 | + writeFileSync(plain, "import { expect, it } from 'bun:test';\nit('works', () => {});\n"); |
| 46 | + |
| 47 | + expect(usesMockModule(mocked)).toBe(true); |
| 48 | + expect(usesMockModule(plain)).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it('batches adjacent non-mocked files while isolating mock.module files', () => { |
| 52 | + const files = [ |
| 53 | + 'src/a.test.ts', |
| 54 | + 'src/b.test.ts', |
| 55 | + 'src/c.test.ts', |
| 56 | + 'src/d.test.ts', |
| 57 | + 'src/e.test.ts', |
| 58 | + ]; |
| 59 | + |
| 60 | + const runs = planFrontendTestRuns(files, (file) => file === 'src/c.test.ts'); |
| 61 | + |
| 62 | + expect(runs).toEqual([ |
| 63 | + { |
| 64 | + label: 'batch 1 (2 files)', |
| 65 | + files: ['src/a.test.ts', 'src/b.test.ts'], |
| 66 | + isolated: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + label: 'src/c.test.ts', |
| 70 | + files: ['src/c.test.ts'], |
| 71 | + isolated: true, |
| 72 | + }, |
| 73 | + { |
| 74 | + label: 'batch 2 (2 files)', |
| 75 | + files: ['src/d.test.ts', 'src/e.test.ts'], |
| 76 | + isolated: false, |
| 77 | + }, |
| 78 | + ]); |
| 79 | + }); |
| 80 | +}); |
0 commit comments