Repository navigation
fix: compile and instrument TypeScript in use-babelrc unit tests - #1013
Merged
Merged
Conversation
`@cypress/code-coverage/use-babelrc` only ran Babel on `.js`/`.jsx` files
and webpack could not resolve `.ts` imports, so TypeScript unit-test specs
failed to bundle ("Module parse failed") and TypeScript sources imported
by specs were never instrumented.
- Run the `.babelrc`-driven babel-loader rule on `.ts`/`.tsx` as well
- Resolve `.jsx`, `.ts` and `.tsx` after webpack's default extensions
- Add test-apps/unit-tests-ts (and to the CI matrix), and point the
README's "unit-tests-ts" entry at it instead of ts-example
- Document adding `@babel/preset-typescript` to `.babelrc`
Fixes #435
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP
- Narrow the babel-loader rule instead of using @ts-ignore - Remove the test's temp output folder after each run - Note that .tsx files also need a JSX preset in .babelrc Refs #435 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP
|
🎉 This PR is included in version 4.1.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Additional details
@cypress/code-coverage/use-babelrconly ran Babel on.js/.jsxfiles, and webpack couldn't resolve.tsimports. So TypeScript unit-test specs failed to bundle (Module parse failed: Unexpected token), and.tssources imported from JS specs couldn't be resolved, which meant no coverage for TS unit tests. The oldexamples/unit-tests-tsapp the issue refers to no longer exists, and the README'stest-apps/unit-tests-tsentry pointed atts-exampleinstead.What changed:
lib/use-babelrc.ts: the Babel rule now also matches.ts/.tsx, so the project's.babelrccompiles them..jsx,.tsand.tsxare added toresolve.extensionsafter webpack's defaults (.js,.json,.wasm), so existing JS imports resolve exactly as before. The two@ts-ignores are replaced with proper type narrowing.test/lib/use-babelrc.test.tsbundles a TS fixture through the real preprocessor and checks that the imported.tsfile is instrumented. It fails without the fix.test-apps/unit-tests-ts(the TS twin ofunit-tests-js) is added to theverify-test-appsCI matrix. The README entry now links to it.@babel/preset-typescriptto.babelrc(plus a JSX preset for.tsx).package.json:@babel/preset-typescriptis a new root devDependency, since the test apps resolve packages from the repo root.This isn't a breaking change.
.tsfiles with type syntax didn't compile withuse-babelrcbefore, and.tsfiles without type syntax still compile through Babel even without the TypeScript preset.The underlying problem overlaps with #361, which also covers the browserify preprocessor and is left open.
Steps to test
npm ci && npm run build && npm test. The newuse-babelrctest passes.cd test-apps/unit-tests-ts && npm i && npm test && npm run coverage:verify && npm run coverage:check-files. You should see 100% line coverage, with onlymisc.tsandmath.tsin the report.test-apps/unit-tests-jsto confirm JS unit tests are unaffected.I couldn't run
cypress runwhere I prepared this (the Cypress binary download was blocked), so theverify-test-apps (unit-tests-ts)CI job is the first real Cypress run of the new app. Both apps' specs were bundled and instrumented through the preprocessor directly.How has the user experience changed?
Before: with
on('file:preprocessor', require('@cypress/code-coverage/use-babelrc')), TypeScript specs failed to bundle and.tssource files got no coverage.After: TypeScript specs and the
.tsfiles they import are compiled with the project's.babelrcand show up in the coverage report, once@babel/preset-typescriptis in.babelrc. No visual change.Generated by Claude Code
Note
Low Risk
Focused preprocessor/webpack config change with regression tests; JavaScript unit-test behavior is intended to stay the same.
Overview
Fixes TypeScript unit tests that use
@cypress/code-coverage/use-babelrc, where specs and imported.tssources failed to bundle or appear in coverage because webpack only ran Babel on.js/.jsxand could not resolve.tsimports.lib/use-babelrc.tswidens the webpack Babel rule to/\.[jt]sx?$/, appends.jsx,.ts, and.tsxtoresolve.extensions(after webpack’s defaults), and replaces@ts-ignorewith typed checks while still stripping built-in presets so the project.babelrcdrives compilation.Docs and examples: README’s “Instrument unit tests” section now documents
@babel/preset-typescriptin.babelrcand points to a dedicatedtest-apps/unit-tests-tsapp (replacing the incorrect link tots-example). CIverify-test-appsincludesunit-tests-ts.Tests: A Vitest case bundles a TypeScript fixture through the real preprocessor and asserts Istanbul instrumentation on imported
.tsfiles; root@babel/preset-typescriptsupports fixtures and the new app.Reviewed by Cursor Bugbot for commit cdd8906. Bugbot is set up for automated code reviews on this repo. Configure here.