-
Notifications
You must be signed in to change notification settings - Fork 5
Keep test-only source out of the published build #218
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
Merged
wpak-ai
merged 2 commits into
cppalliance:main
from
timon0305:fix/tsconfig-exclude-test-code-217
Jul 15, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| /** | ||
| * Guards against test-only source leaking into the published build (#217). | ||
| * | ||
| * The stale `tsconfig.json` exclude pointed at a path that no longer matched | ||
| * `test-helpers.ts`, so it (and the `.compile-test.ts` brand guards) shipped | ||
| * into `dist/`. `tsconfig.build.json` now excludes test code by pattern; this | ||
| * test asks tsc which files that config would compile and fails if any test | ||
| * module is still in the program. | ||
| */ | ||
|
|
||
| const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..'); | ||
| const tscEntry = path.join(repoRoot, 'node_modules', 'typescript', 'bin', 'tsc'); | ||
|
|
||
| function buildProgramSrcFiles(): string[] { | ||
| const out = execFileSync( | ||
| process.execPath, | ||
| [tscEntry, '-p', 'tsconfig.build.json', '--listFilesOnly'], | ||
| { cwd: repoRoot, encoding: 'utf8', maxBuffer: 32 * 1024 * 1024 } | ||
| ); | ||
| return out | ||
| .split('\n') | ||
| .map((line) => line.trim()) | ||
| .filter(Boolean) | ||
| .map((abs) => path.relative(repoRoot, abs).split(path.sep).join('/')) | ||
| .filter((rel) => rel.startsWith('src/')); | ||
| } | ||
|
|
||
| // Mirrors tsconfig.build.json's exclude — add any new test-only suffix to both. | ||
| const TEST_CODE = [ | ||
| /\.test\.ts$/, | ||
| /\.compile-test\.ts$/, | ||
| /(^|\/)test-helpers\.ts$/, | ||
| /(^|\/)__tests__\//, | ||
| ]; | ||
|
|
||
| describe('build output excludes test code (#217)', () => { | ||
| it('the build tsconfig compiles no test-only modules from src', () => { | ||
| const files = buildProgramSrcFiles(); | ||
| // Guard against a vacuous pass: if the build config ever compiled nothing, | ||
| // `leaked` would be empty while the real build is broken. | ||
| expect(files).toContain('src/index.ts'); | ||
| const leaked = files.filter((f) => TEST_CODE.some((re) => re.test(f))); | ||
| expect(leaked).toEqual([]); | ||
| }, 60_000); | ||
| }); | ||
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,13 @@ | ||
| { | ||
| "extends": "./tsconfig.json", | ||
| // New test-only suffixes must also go in TEST_CODE in | ||
| // src/__tests__/build-excludes-test-code.test.ts, which guards this list. | ||
| "exclude": [ | ||
| "node_modules", | ||
| "dist", | ||
| "**/*.test.ts", | ||
| "**/*.compile-test.ts", | ||
| "**/test-helpers.ts", | ||
| "**/__tests__/**" | ||
| ] | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.