From a1c8f6ee4557d8a48d1703835023c6e55ce04947 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 22:50:48 +0000 Subject: [PATCH 1/2] fix: compile and instrument TypeScript in use-babelrc unit tests `@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 Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP --- .github/workflows/ci.yml | 1 + README.md | 13 +++- lib/use-babelrc.ts | 19 ++++- package-lock.json | 73 +++++++++++++++++++ package.json | 1 + test-apps/unit-tests-ts/.babelrc | 4 + test-apps/unit-tests-ts/README.md | 3 + test-apps/unit-tests-ts/cypress.config.js | 16 ++++ .../unit-tests-ts/cypress/e2e/spec-a.cy.ts | 13 ++++ .../unit-tests-ts/cypress/e2e/spec-b.cy.ts | 9 +++ .../unit-tests-ts/cypress/plugins/index.js | 6 ++ .../unit-tests-ts/cypress/support/e2e.js | 1 + test-apps/unit-tests-ts/package.json | 10 +++ test-apps/unit-tests-ts/src/utils/math.ts | 1 + test-apps/unit-tests-ts/src/utils/misc.ts | 8 ++ test/lib/__fixtures__/use-babelrc-ts/.babelrc | 4 + test/lib/__fixtures__/use-babelrc-ts/math.ts | 1 + test/lib/__fixtures__/use-babelrc-ts/spec.ts | 4 + test/lib/use-babelrc.test.ts | 26 +++++++ 19 files changed, 207 insertions(+), 6 deletions(-) create mode 100644 test-apps/unit-tests-ts/.babelrc create mode 100644 test-apps/unit-tests-ts/README.md create mode 100644 test-apps/unit-tests-ts/cypress.config.js create mode 100644 test-apps/unit-tests-ts/cypress/e2e/spec-a.cy.ts create mode 100644 test-apps/unit-tests-ts/cypress/e2e/spec-b.cy.ts create mode 100644 test-apps/unit-tests-ts/cypress/plugins/index.js create mode 100644 test-apps/unit-tests-ts/cypress/support/e2e.js create mode 100644 test-apps/unit-tests-ts/package.json create mode 100644 test-apps/unit-tests-ts/src/utils/math.ts create mode 100644 test-apps/unit-tests-ts/src/utils/misc.ts create mode 100644 test/lib/__fixtures__/use-babelrc-ts/.babelrc create mode 100644 test/lib/__fixtures__/use-babelrc-ts/math.ts create mode 100644 test/lib/__fixtures__/use-babelrc-ts/spec.ts create mode 100644 test/lib/use-babelrc.test.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 59c5f5c89..376908af6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -93,6 +93,7 @@ jobs: - swc-webpack - ts-example - unit-tests-js + - unit-tests-ts - use-webpack - redirect defaults: diff --git a/README.md b/README.md index 68b01a6d9..11056e364 100644 --- a/README.md +++ b/README.md @@ -350,7 +350,16 @@ module.exports = (on, config) => { The code coverage from spec files will be combined with end-to-end coverage. -Find examples of just the unit tests and JavaScript source files with collected code coverage in [test-apps/unit-tests-js](./test-apps/unit-tests-js). +`use-babelrc` compiles `.js`, `.jsx`, `.ts` and `.tsx` files with your `.babelrc`. For TypeScript, add `@babel/preset-typescript` to it: + +```json +{ + "presets": ["@babel/preset-typescript"], + "plugins": ["istanbul"] +} +``` + +Find examples of just the unit tests with collected code coverage in [test-apps/unit-tests-js](./test-apps/unit-tests-js) (JavaScript) and [test-apps/unit-tests-ts](./test-apps/unit-tests-ts) (TypeScript). ## Instrument backend code @@ -770,7 +779,7 @@ Full examples we use for testing in this repository: - [test-apps/component-vite](test-apps/component-vite) collects coverage from React component tests served by Vite - [test-apps/swc-webpack](test-apps/swc-webpack) instruments a webpack build with SWC instead of Babel, and loads it after the page's `load` event - [test-apps/unit-tests-js](test-apps/unit-tests-js) runs just the unit tests and reports code coverage (JavaScript source code) -- [test-apps/unit-tests-ts](test-apps/ts-example) runs just the unit tests and reports code coverage (TypeScript source code) +- [test-apps/unit-tests-ts](test-apps/unit-tests-ts) runs just the unit tests and reports code coverage (TypeScript source code) ### External examples diff --git a/lib/use-babelrc.ts b/lib/use-babelrc.ts index 271782ebc..829831928 100644 --- a/lib/use-babelrc.ts +++ b/lib/use-babelrc.ts @@ -3,11 +3,22 @@ import webpackPreprocessor from '@cypress/webpack-preprocessor' const defaults = webpackPreprocessor.defaultOptions -// remove presets so the babelrc file will be used -// @ts-ignore - we know that the use property exists -if (defaults.webpackOptions?.module?.rules?.[0]?.use?.[0]?.options?.presets) { +const webpackOptions = defaults.webpackOptions +const babelRule = webpackOptions?.module?.rules?.[0] +if (babelRule && typeof babelRule === 'object') { + // Let the project's .babelrc handle TypeScript too, so unit tests that + // import .ts sources get instrumented like .js ones + babelRule.test = /\.[jt]sx?$/ + // remove presets so the babelrc file will be used // @ts-ignore - we know that the use property exists - delete defaults.webpackOptions.module.rules[0].use[0].options.presets + delete babelRule.use?.[0]?.options?.presets +} +if (webpackOptions) { + webpackOptions.resolve = { + ...webpackOptions.resolve, + // webpack's defaults come first so existing imports resolve as before + extensions: ['.js', '.json', '.wasm', '.jsx', '.ts', '.tsx'] + } } // @ts-ignore - we know that the use property exists const preprocessor = webpackPreprocessor(defaults) diff --git a/package-lock.json b/package-lock.json index 23034df95..d9751d2d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "devDependencies": { "@babel/core": "^7.16.0", "@babel/preset-env": "^7.16.11", + "@babel/preset-typescript": "^7.29.7", "@cypress/code-coverage": "file:.", "@types/debug": "^4.1.12", "@types/express": "^5.0.6", @@ -636,6 +637,38 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-syntax-jsx": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-jsx/-/plugin-syntax-jsx-7.29.7.tgz", + "integrity": "sha512-TSu8+mHCoEaaCDEZ0I3+6mvTBYR4PCxQwf2z9/r5Tbztv6NaLR3B9thGTTxX2WGuGHJqRiAbKPeGTJ5XWXVg6A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-plugin-utils": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, + "node_modules/@babel/plugin-syntax-typescript": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.29.7.tgz", + "integrity": "sha512-ngr+82Sh0xMz25TPCZi+nC2iTzjfCdWS2ONXTp/PtSCHCgaCNBpdMqgvJ2ccdLlClVZ7sisIgB914j/JFe+RZA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-plugin-utils": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/plugin-syntax-unicode-sets-regex": { "version": "7.18.6", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-unicode-sets-regex/-/plugin-syntax-unicode-sets-regex-7.18.6.tgz", @@ -1393,6 +1426,26 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-transform-typescript": { + "version": "7.29.9", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.29.9.tgz", + "integrity": "sha512-FFwIwzU+7SCOuxxV4YtJql6T9981ZVTm+FHO5GhVsRqCTdy0WwrEZh3l42ARpXruJUDGOptdduHW6Zr7pNPLNg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-annotate-as-pure": "^7.29.7", + "@babel/helper-create-class-features-plugin": "^7.29.7", + "@babel/helper-plugin-utils": "^7.29.7", + "@babel/helper-skip-transparent-expression-wrappers": "^7.29.7", + "@babel/plugin-syntax-typescript": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/plugin-transform-unicode-escapes": { "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.29.7.tgz", @@ -1563,6 +1616,26 @@ "@babel/core": "^7.0.0-0 || ^8.0.0-0 <8.0.0" } }, + "node_modules/@babel/preset-typescript": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/preset-typescript/-/preset-typescript-7.29.7.tgz", + "integrity": "sha512-/Foi8vKY2EVbed/1eZx0gJEEwHAIxogrySI7rULcRIvhZzbvoE/b5qG5Ghc0WKAFKOHA9SD1x7RsFlOYdutIiQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@babel/helper-plugin-utils": "^7.29.7", + "@babel/helper-validator-option": "^7.29.7", + "@babel/plugin-syntax-jsx": "^7.29.7", + "@babel/plugin-transform-modules-commonjs": "^7.29.7", + "@babel/plugin-transform-typescript": "^7.29.7" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/template": { "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.29.7.tgz", diff --git a/package.json b/package.json index a57d42541..f1f93b2d2 100644 --- a/package.json +++ b/package.json @@ -78,6 +78,7 @@ "devDependencies": { "@babel/core": "^7.16.0", "@babel/preset-env": "^7.16.11", + "@babel/preset-typescript": "^7.29.7", "@cypress/code-coverage": "file:.", "@types/debug": "^4.1.12", "@types/express": "^5.0.6", diff --git a/test-apps/unit-tests-ts/.babelrc b/test-apps/unit-tests-ts/.babelrc new file mode 100644 index 000000000..18c5a8118 --- /dev/null +++ b/test-apps/unit-tests-ts/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["@babel/preset-typescript"], + "plugins": ["istanbul"] +} diff --git a/test-apps/unit-tests-ts/README.md b/test-apps/unit-tests-ts/README.md new file mode 100644 index 000000000..779a4ec0c --- /dev/null +++ b/test-apps/unit-tests-ts/README.md @@ -0,0 +1,3 @@ +# example: unit-tests-ts + +Examples that only run unit tests written using TypeScript. The `.babelrc` adds `@babel/preset-typescript` so `@cypress/code-coverage/use-babelrc` can compile and instrument the `.ts` files. diff --git a/test-apps/unit-tests-ts/cypress.config.js b/test-apps/unit-tests-ts/cypress.config.js new file mode 100644 index 000000000..f6a9efc01 --- /dev/null +++ b/test-apps/unit-tests-ts/cypress.config.js @@ -0,0 +1,16 @@ +const { defineConfig } = require('cypress') + +module.exports = defineConfig({ + allowCypressEnv: false, + fixturesFolder: false, + e2e: { + setupNodeEvents(on, config) { + return require('./cypress/plugins/index.js')(on, config) + }, + expose: { + codeCoverage: { + exclude: 'cypress/**/*.*' + } + } + }, +}) diff --git a/test-apps/unit-tests-ts/cypress/e2e/spec-a.cy.ts b/test-apps/unit-tests-ts/cypress/e2e/spec-a.cy.ts new file mode 100644 index 000000000..d109869d7 --- /dev/null +++ b/test-apps/unit-tests-ts/cypress/e2e/spec-a.cy.ts @@ -0,0 +1,13 @@ +/// +import { isObject, add } from '../../src/utils/misc' + +describe('unit tests', () => { + it('adds two numbers', () => { + expect(add(2, 3)).to.equal(5) + }) + + it('checks for object', () => { + expect(isObject({}), '{}').to.be.true + expect(isObject([]), '[]').to.be.true + }) +}) diff --git a/test-apps/unit-tests-ts/cypress/e2e/spec-b.cy.ts b/test-apps/unit-tests-ts/cypress/e2e/spec-b.cy.ts new file mode 100644 index 000000000..d74079c10 --- /dev/null +++ b/test-apps/unit-tests-ts/cypress/e2e/spec-b.cy.ts @@ -0,0 +1,9 @@ +/// +import { sub } from '../../src/utils/math' + +describe('Unit tests', () => { + it('subtracts numbers', () => { + const result: number = sub(10, 4) + expect(result).to.equal(6) + }) +}) diff --git a/test-apps/unit-tests-ts/cypress/plugins/index.js b/test-apps/unit-tests-ts/cypress/plugins/index.js new file mode 100644 index 000000000..e78e530e7 --- /dev/null +++ b/test-apps/unit-tests-ts/cypress/plugins/index.js @@ -0,0 +1,6 @@ +module.exports = (on, config) => { + require('@cypress/code-coverage/task')(on, config) + //used to instrument files included as unit tests + on('file:preprocessor', require('@cypress/code-coverage/use-babelrc')) + return config +} diff --git a/test-apps/unit-tests-ts/cypress/support/e2e.js b/test-apps/unit-tests-ts/cypress/support/e2e.js new file mode 100644 index 000000000..cc6040de7 --- /dev/null +++ b/test-apps/unit-tests-ts/cypress/support/e2e.js @@ -0,0 +1 @@ +import '@cypress/code-coverage/support' diff --git a/test-apps/unit-tests-ts/package.json b/test-apps/unit-tests-ts/package.json new file mode 100644 index 000000000..b1fcfa4c2 --- /dev/null +++ b/test-apps/unit-tests-ts/package.json @@ -0,0 +1,10 @@ +{ + "name": "example-unit-tests-ts", + "description": "Run unit tests written using TypeScript", + "scripts": { + "pretest": "rimraf .nyc_output .cache coverage dist", + "test": "cypress run", + "coverage:verify": "npx nyc report --check-coverage true --lines 100", + "coverage:check-files": "check-coverage misc.ts math.ts && only-covered misc.ts math.ts" + } +} diff --git a/test-apps/unit-tests-ts/src/utils/math.ts b/test-apps/unit-tests-ts/src/utils/math.ts new file mode 100644 index 000000000..b07b326a1 --- /dev/null +++ b/test-apps/unit-tests-ts/src/utils/math.ts @@ -0,0 +1 @@ +export const sub = (a: number, b: number): number => a - b diff --git a/test-apps/unit-tests-ts/src/utils/misc.ts b/test-apps/unit-tests-ts/src/utils/misc.ts new file mode 100644 index 000000000..a81e0dbca --- /dev/null +++ b/test-apps/unit-tests-ts/src/utils/misc.ts @@ -0,0 +1,8 @@ +/** + * Very simple check, returns true for arrays as well + */ +export function isObject(object: unknown): object is object { + return object != null && typeof object === 'object' +} + +export const add = (a: number, b: number): number => a + b diff --git a/test/lib/__fixtures__/use-babelrc-ts/.babelrc b/test/lib/__fixtures__/use-babelrc-ts/.babelrc new file mode 100644 index 000000000..18c5a8118 --- /dev/null +++ b/test/lib/__fixtures__/use-babelrc-ts/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["@babel/preset-typescript"], + "plugins": ["istanbul"] +} diff --git a/test/lib/__fixtures__/use-babelrc-ts/math.ts b/test/lib/__fixtures__/use-babelrc-ts/math.ts new file mode 100644 index 000000000..b07b326a1 --- /dev/null +++ b/test/lib/__fixtures__/use-babelrc-ts/math.ts @@ -0,0 +1 @@ +export const sub = (a: number, b: number): number => a - b diff --git a/test/lib/__fixtures__/use-babelrc-ts/spec.ts b/test/lib/__fixtures__/use-babelrc-ts/spec.ts new file mode 100644 index 000000000..39acea4d8 --- /dev/null +++ b/test/lib/__fixtures__/use-babelrc-ts/spec.ts @@ -0,0 +1,4 @@ +import { sub } from './math' + +const result: number = sub(10, 4) +console.log(result) diff --git a/test/lib/use-babelrc.test.ts b/test/lib/use-babelrc.test.ts new file mode 100644 index 000000000..a94e58c9f --- /dev/null +++ b/test/lib/use-babelrc.test.ts @@ -0,0 +1,26 @@ +import { describe, it, expect } from 'vitest' +import { EventEmitter } from 'events' +import { mkdtempSync, readFileSync } from 'fs' +import { tmpdir } from 'os' +import path from 'path' +import preprocessor from '../../lib/use-babelrc' + +const fixtures = path.join(__dirname, '__fixtures__', 'use-babelrc-ts') + +describe('use-babelrc', () => { + it('bundles TypeScript specs and instruments the .ts files they import', async () => { + const file = Object.assign(new EventEmitter(), { + filePath: path.join(fixtures, 'spec.ts'), + outputPath: path.join( + mkdtempSync(path.join(tmpdir(), 'use-babelrc-')), + 'spec.js' + ), + shouldWatch: false + }) as unknown as Cypress.FileObject + + const bundle = readFileSync(await preprocessor(file), 'utf8') + + expect(bundle).toContain('__coverage__') + expect(bundle).toContain(JSON.stringify(path.join(fixtures, 'math.ts'))) + }, 60_000) +}) From cdd8906aa1ba00414981fbe0496fb295707fcfcf Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 2 Nov 2026 00:11:35 +0000 Subject: [PATCH 2/2] chore: tidy use-babelrc types, test cleanup, and .tsx docs - 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 Claude-Session: https://claude.ai/code/session_01WUv8r36Qgb8PyPw9VYGEbP --- README.md | 2 +- lib/use-babelrc.ts | 13 +++++++++---- test/lib/use-babelrc.test.ts | 19 +++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 11056e364..156845019 100644 --- a/README.md +++ b/README.md @@ -350,7 +350,7 @@ module.exports = (on, config) => { The code coverage from spec files will be combined with end-to-end coverage. -`use-babelrc` compiles `.js`, `.jsx`, `.ts` and `.tsx` files with your `.babelrc`. For TypeScript, add `@babel/preset-typescript` to it: +`use-babelrc` compiles `.js`, `.jsx`, `.ts` and `.tsx` files with your `.babelrc`. For TypeScript, add `@babel/preset-typescript` to it (and a JSX preset such as `@babel/preset-react` for `.tsx` files): ```json { diff --git a/lib/use-babelrc.ts b/lib/use-babelrc.ts index 829831928..e2246a470 100644 --- a/lib/use-babelrc.ts +++ b/lib/use-babelrc.ts @@ -9,9 +9,15 @@ if (babelRule && typeof babelRule === 'object') { // Let the project's .babelrc handle TypeScript too, so unit tests that // import .ts sources get instrumented like .js ones babelRule.test = /\.[jt]sx?$/ - // remove presets so the babelrc file will be used - // @ts-ignore - we know that the use property exists - delete babelRule.use?.[0]?.options?.presets + const babelLoader = Array.isArray(babelRule.use) ? babelRule.use[0] : null + if ( + babelLoader && + typeof babelLoader === 'object' && + typeof babelLoader.options === 'object' + ) { + // remove presets so the babelrc file will be used + delete babelLoader.options.presets + } } if (webpackOptions) { webpackOptions.resolve = { @@ -20,7 +26,6 @@ if (webpackOptions) { extensions: ['.js', '.json', '.wasm', '.jsx', '.ts', '.tsx'] } } -// @ts-ignore - we know that the use property exists const preprocessor = webpackPreprocessor(defaults) // Type assertion to avoid exposing private FileEvent type export = preprocessor as (file: Cypress.FileObject) => string | Promise diff --git a/test/lib/use-babelrc.test.ts b/test/lib/use-babelrc.test.ts index a94e58c9f..13f6c1bbf 100644 --- a/test/lib/use-babelrc.test.ts +++ b/test/lib/use-babelrc.test.ts @@ -1,6 +1,6 @@ -import { describe, it, expect } from 'vitest' +import { describe, it, expect, afterEach } from 'vitest' import { EventEmitter } from 'events' -import { mkdtempSync, readFileSync } from 'fs' +import { mkdtempSync, readFileSync, rmSync } from 'fs' import { tmpdir } from 'os' import path from 'path' import preprocessor from '../../lib/use-babelrc' @@ -8,13 +8,20 @@ import preprocessor from '../../lib/use-babelrc' const fixtures = path.join(__dirname, '__fixtures__', 'use-babelrc-ts') describe('use-babelrc', () => { + let outDir: string | undefined + + afterEach(() => { + if (outDir) { + rmSync(outDir, { recursive: true, force: true }) + outDir = undefined + } + }) + it('bundles TypeScript specs and instruments the .ts files they import', async () => { + outDir = mkdtempSync(path.join(tmpdir(), 'use-babelrc-')) const file = Object.assign(new EventEmitter(), { filePath: path.join(fixtures, 'spec.ts'), - outputPath: path.join( - mkdtempSync(path.join(tmpdir(), 'use-babelrc-')), - 'spec.js' - ), + outputPath: path.join(outDir, 'spec.js'), shouldWatch: false }) as unknown as Cypress.FileObject