-
Notifications
You must be signed in to change notification settings - Fork 3
CCM14615 Unit test quickening #249
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
Draft
Ian-Hodges
wants to merge
10
commits into
main
Choose a base branch
from
feature/CCM-14615_unit-test-quickening
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
02f61ee
CCM-14615: add temp workflow for unit tests only
Ian-Hodges 6d19863
CCM-14615: add temp workflow for unit tests only
Ian-Hodges 3ab0de7
CCM-14615: run parrallel using jest workspaces
Ian-Hodges 52c6caf
CCM-14615: run pytets in parrallel
Ian-Hodges ff5aced
Merge remote-tracking branch 'origin/main' into feature/CCM-14615_uni…
Ian-Hodges b6dadac
CCM-14615: more parrallels
Ian-Hodges c47ac23
CCM-14615: clean ups
Ian-Hodges 2a54862
CCM-14615: clean ups
Ian-Hodges 573d3d9
CCM-14615: a look at caching generated dependencies
Ian-Hodges 2a69f10
CCM-14615: a look at caching generated dependencies
Ian-Hodges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * Generates jest.config.cjs from the individual workspace jest.config.ts files. | ||
| */ | ||
|
|
||
| import { execFileSync } from 'node:child_process'; | ||
| import * as fs from 'node:fs'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| const repoRoot = path.resolve(__dirname, '..'); | ||
| const outputPath = path.join(repoRoot, 'jest.config.cjs'); | ||
|
|
||
| interface PackageJson { | ||
| workspaces?: string[]; | ||
| } | ||
|
|
||
| const rootPkg = JSON.parse( | ||
| fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'), | ||
| ) as PackageJson; | ||
|
|
||
| const workspaces: string[] = rootPkg.workspaces ?? []; | ||
|
|
||
| /** | ||
| * Inline TypeScript written to a temp .ts file and executed by tsx so each | ||
| * workspace jest.config.ts is evaluated in an isolated Node process with a | ||
| * fresh module registry (preventing shared mutable baseJestConfig state). | ||
| */ | ||
| const EVALUATOR = (configPath: string): string => ` | ||
| import config from ${JSON.stringify(configPath)}; | ||
| process.stdout.write(JSON.stringify(config)); | ||
| `; | ||
|
|
||
| /** Serialise a plain JS value to source code, indented with the given prefix. */ | ||
| function serialise(value: unknown, indent = ' '): string { | ||
| if (value === null) return 'null'; | ||
| if (value === undefined) return 'undefined'; | ||
| if (typeof value === 'string') return JSON.stringify(value); | ||
| if (typeof value === 'number' || typeof value === 'boolean') | ||
| return String(value); | ||
|
|
||
| if (Array.isArray(value)) { | ||
| if (value.length === 0) return '[]'; | ||
| const items = value | ||
| .map((v) => `${indent} ${serialise(v, indent + ' ')}`) | ||
| .join(',\n'); | ||
| return `[\n${items},\n${indent}]`; | ||
| } | ||
|
|
||
| if (typeof value === 'object') { | ||
| const entries = Object.entries(value as Record<string, unknown>).filter( | ||
| ([, v]) => v !== undefined, | ||
| ); | ||
| if (entries.length === 0) return '{}'; | ||
| const lines = entries | ||
| .map( | ||
| ([k, v]) => | ||
| `${indent} ${JSON.stringify(k)}: ${serialise(v, indent + ' ')}`, | ||
| ) | ||
| .join(',\n'); | ||
| return `{\n${lines},\n${indent}}`; | ||
| } | ||
|
|
||
| return String(value); | ||
| } | ||
|
|
||
| interface ProjectEntry { | ||
| workspace: string; | ||
| config: Record<string, unknown>; | ||
| } | ||
|
|
||
| function main(): void { | ||
| const projects: ProjectEntry[] = []; | ||
|
|
||
| for (const ws of workspaces) { | ||
| const wsDir = path.join(repoRoot, ws); | ||
|
|
||
| const hasCjs = fs.existsSync(path.join(wsDir, 'jest.config.cjs')); | ||
| const hasTs = fs.existsSync(path.join(wsDir, 'jest.config.ts')); | ||
|
|
||
| if (hasCjs && !hasTs) { | ||
| throw new Error( | ||
| `${ws} has jest.config.cjs but no jest.config.ts. ` + | ||
| `Migrate it to jest.config.ts so the generator can handle it uniformly.`, | ||
| ); | ||
| } | ||
|
|
||
| if (!hasTs) { | ||
| // No Jest config → no Jest tests (e.g. src/digital-letters-events) | ||
| continue; | ||
| } | ||
|
|
||
| // Evaluate the workspace config in an isolated tsx subprocess so that the | ||
| // shared mutable `baseJestConfig` object is freshly initialised for every | ||
| // workspace. Dynamic import() in the parent process would share the cached | ||
| // module instance and accumulate mutations. | ||
| const configPath = path.join(wsDir, 'jest.config.ts'); | ||
| const tsxBin = path.join(repoRoot, 'node_modules', '.bin', 'tsx'); | ||
| const tmpFile = path.join(os.tmpdir(), `jest-config-eval-${Date.now()}.ts`); | ||
| let json: string; | ||
| try { | ||
| fs.writeFileSync(tmpFile, EVALUATOR(configPath), 'utf8'); | ||
| json = execFileSync(tsxBin, [tmpFile], { | ||
| cwd: repoRoot, | ||
| encoding: 'utf8', | ||
| }); | ||
| } finally { | ||
| fs.rmSync(tmpFile, { force: true }); | ||
| } | ||
| const wsConfig = JSON.parse(json) as Record<string, unknown>; | ||
|
|
||
| // Inject rootDir and displayName. Jest resolves all relative paths inside a | ||
| // project entry relative to that project's rootDir. | ||
| const entry: Record<string, unknown> = { | ||
| ...wsConfig, | ||
| rootDir: `<rootDir>/${ws}`, | ||
| displayName: ws, | ||
| }; | ||
|
|
||
| projects.push({ workspace: ws, config: entry }); | ||
| } | ||
|
|
||
| // Build the projects array source | ||
| const projectLines = projects.map((p) => { | ||
| const body = serialise(p.config, ' '); | ||
| return ` // ${p.workspace}\n ${body}`; | ||
| }); | ||
|
|
||
| const banner = `/** | ||
| * Root Jest config — runs all TypeScript workspace test suites in | ||
| * parallel via Jest's native \`projects\` support. | ||
| * | ||
| * ⚠️ THIS FILE IS AUTO-GENERATED. Do not edit it directly. | ||
| * | ||
| * Generated by scripts/generate-parallel-jest-config.ts | ||
| */ | ||
|
|
||
| /** @type {import('jest').Config} */ | ||
| module.exports = { | ||
| projects: [ | ||
| ${projectLines.join(',\n\n')} | ||
| ], | ||
| }; | ||
| `; | ||
|
|
||
| fs.writeFileSync(outputPath, banner, 'utf8'); | ||
| console.log(`Written: ${path.relative(repoRoot, outputPath)}`); | ||
| console.log(` ${projects.length} project(s) included`); | ||
| for (const p of projects) { | ||
| console.log(` ${p.workspace}`); | ||
| } | ||
| } | ||
|
|
||
| main(); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose once we merge https://github.com/NHSDigital/nhs-notify-digital-letters/pull/254/changes then we need to add also guard-functions