Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
- swc-webpack
- ts-example
- unit-tests-js
- unit-tests-ts
- use-webpack
- redirect
defaults:
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (and a JSX preset such as `@babel/preset-react` for `.tsx` files):

```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

Expand Down Expand Up @@ -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

Expand Down
28 changes: 22 additions & 6 deletions lib/use-babelrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
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) {
// @ts-ignore - we know that the use property exists
delete 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?$/
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 = {
...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)
// Type assertion to avoid exposing private FileEvent type
export = preprocessor as (file: Cypress.FileObject) => string | Promise<string>
73 changes: 73 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions test-apps/unit-tests-ts/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-typescript"],
"plugins": ["istanbul"]
}
3 changes: 3 additions & 0 deletions test-apps/unit-tests-ts/README.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions test-apps/unit-tests-ts/cypress.config.js
Original file line number Diff line number Diff line change
@@ -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/**/*.*'
}
}
},
})
13 changes: 13 additions & 0 deletions test-apps/unit-tests-ts/cypress/e2e/spec-a.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference types="cypress" />
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
})
})
9 changes: 9 additions & 0 deletions test-apps/unit-tests-ts/cypress/e2e/spec-b.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="cypress" />
import { sub } from '../../src/utils/math'

describe('Unit tests', () => {
it('subtracts numbers', () => {
const result: number = sub(10, 4)
expect(result).to.equal(6)
})
})
6 changes: 6 additions & 0 deletions test-apps/unit-tests-ts/cypress/plugins/index.js
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions test-apps/unit-tests-ts/cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@cypress/code-coverage/support'
10 changes: 10 additions & 0 deletions test-apps/unit-tests-ts/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
1 change: 1 addition & 0 deletions test-apps/unit-tests-ts/src/utils/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sub = (a: number, b: number): number => a - b
8 changes: 8 additions & 0 deletions test-apps/unit-tests-ts/src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/lib/__fixtures__/use-babelrc-ts/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-typescript"],
"plugins": ["istanbul"]
}
1 change: 1 addition & 0 deletions test/lib/__fixtures__/use-babelrc-ts/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const sub = (a: number, b: number): number => a - b
4 changes: 4 additions & 0 deletions test/lib/__fixtures__/use-babelrc-ts/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { sub } from './math'

const result: number = sub(10, 4)
console.log(result)
33 changes: 33 additions & 0 deletions test/lib/use-babelrc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, it, expect, afterEach } from 'vitest'
import { EventEmitter } from 'events'
import { mkdtempSync, readFileSync, rmSync } 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', () => {
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(outDir, '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)
})
Loading