Skip to content

Commit

Permalink
feat: support istanbul coverage provider (#1676)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
AriPerkkio and antfu committed Aug 15, 2022
1 parent 1d15efa commit 265fdbe
Show file tree
Hide file tree
Showing 47 changed files with 1,228 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Expand Up @@ -5,4 +5,4 @@ node_modules
*.d.ts
coverage
!.vitepress
test/core/src/self
test/core/src/self
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@ yarn-debug.log*
yarn-error.log*
lib-cov
coverage
!**/integrations/coverage
node_modules
.env
.cache
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -35,7 +35,7 @@ A blazing fast unit test framework powered by Vite.
- [Jest Snapshot](https://jestjs.io/docs/snapshot-testing)
- [Chai](https://www.chaijs.com/) built-in for assertions, with [Jest expect](https://jestjs.io/docs/expect) compatible APIs.
- [Smart & instant watch mode](https://vitest.dev/guide/features.html#watch-mode), like HMR for tests!
- [Native code coverage](https://vitest.dev/guide/features.html#coverage) via [c8](https://github.com/bcoe/c8)
- [Native code coverage](https://vitest.dev/guide/features.html#coverage) via [c8](https://github.com/bcoe/c8) or [`istanbul`](https://istanbul.js.org/).
- [Tinyspy](https://github.com/Aslemammad/tinyspy) built-in for mocking, stubbing, and spies.
- [JSDOM](https://github.com/jsdom/jsdom) and [happy-dom](https://github.com/capricorn86/happy-dom) for DOM and browser API mocking
- Components testing ([Vue](./examples/vue), [React](./examples/react), [Svelte](./examples/svelte), [Lit](./examples/lit), [Vitesse](./examples/vitesse))
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/components/FeaturesList.vue
Expand Up @@ -20,7 +20,7 @@
<ListItem><a target="_blank" href="https://www.chaijs.com/" rel="noopener noreferrer">Chai</a> built-in for assertions + <a target="_blank" href="https://jestjs.io/docs/expect" rel="noopener noreferrer">Jest expect</a> compatible APIs</ListItem>
<ListItem><a target="_blank" href="https://github.com/Aslemammad/tinyspy" rel="noopener noreferrer">Tinyspy</a> built-in for mocking</ListItem>
<ListItem><a target="_blank" href="https://github.com/capricorn86/happy-dom" rel="noopener noreferrer">happy-dom</a> or <a target="_blank" href="https://github.com/jsdom/jsdom" rel="noopener noreferrer">jsdom</a> for DOM mocking</ListItem>
<ListItem>Native code coverage via <a target="_blank" href="https://github.com/bcoe/c8" rel="noopener noreferrer">c8</a></ListItem>
<ListItem>Code coverage via <a target="_blank" href="https://github.com/bcoe/c8" rel="noopener noreferrer">c8</a> or <a target="_blank" href="https://istanbul.js.org/" rel="noopener noreferrer">istanbul</a></ListItem>
<ListItem>Rust-like <a href="/guide/in-source">in-source testing</a></ListItem>
</ul>
</template>
Expand Down
97 changes: 95 additions & 2 deletions docs/config/index.md
Expand Up @@ -420,10 +420,103 @@ Isolate environment for each test file. Does not work if you disable [`--threads

### coverage

- **Type:** `C8Options`
- **Type:** `CoverageC8Options | CoverageIstanbulOptions`
- **Default:** `undefined`

Coverage options passed to [C8](https://github.com/bcoe/c8).
You can use [`c8`](https://github.com/bcoe/c8) or [`istanbul`](https://istanbul.js.org/) for coverage collection.

#### provider

- **Type:** `'c8' | 'istanbul'`
- **Default:** `'c8'`

Use `provider` to select the tool for coverage collection.

#### CoverageC8Options

Used when `provider: 'c8'` is set. Coverage options are passed to [`c8`](https://github.com/bcoe/c8).

#### CoverageIstanbulOptions

Used when `provider: 'istanbul'` is set.

##### exclude

- **Type:** `string[]`
- **Default:** `[]`

List of files excluded from coverage as glob patterns.

##### skipFull

- **Type:** `boolean`
- **Default:** `false`

Do not show files with 100% statement, branch, and function coverage.

##### perFile

- **Type:** `boolean`
- **Default:** `false`

Check thresholds per file.

##### lines

- **Type:** `number`

Threshold for lines.

##### functions

- **Type:** `number`

Threshold for functions.

##### branches

- **Type:** `number`

Threshold for branches.

##### statements

- **Type:** `number`

Threshold for statements.

##### ignoreClassMethods

- **Type:** `string[]`
- **Default:** []

Set to array of class method names to ignore for coverage.

##### watermarks

- **Type:**
<!-- eslint-skip -->
```ts
{
statements?: [number, number],
functions?: [number, number],
branches?: [number, number],
lines?: [number, number]
}
```

- **Default:**
<!-- eslint-skip -->
```ts
{
statements: [50, 80],
functions: [50, 80],
branches: [50, 80],
lines: [50, 80]
}
```

Watermarks for statements, lines, branches and functions.

### testNamePattern

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/cli.md
Expand Up @@ -61,7 +61,7 @@ vitest related /src/index.ts /src/hello-world.js
| `--outputTruncateLength <length>` | Truncate output diff lines up to `<length>` number of characters. |
| `--outputDiffLines <lines>` | Limit number of output diff lines up to `<lines>`. |
| `--outputFile <filename/-s>` | Write test results to a file when the `--reporter=json` or `--reporter=junit` option is also specified <br /> Via [cac's dot notation] you can specify individual outputs for multiple reporters |
| `--coverage` | Use c8 for coverage |
| `--coverage` | Enable coverage report |
| `--run` | Do not watch |
| `--mode` | Override Vite mode (default: `test`) |
| `--mode <name>` | Override Vite mode (default: `test`) |
Expand Down
59 changes: 56 additions & 3 deletions docs/guide/coverage.md
Expand Up @@ -4,13 +4,46 @@ title: Coverage | Guide

# Coverage

Vitest supports Native code coverage via [`c8`](https://github.com/bcoe/c8). `c8` is an optional peer dependency, to use the coverage feature you will need to install it first by:
Vitest supports Native code coverage via [`c8`](https://github.com/bcoe/c8) and instrumented code coverage via [`istanbul`](https://istanbul.js.org/).

## Coverage Providers

:::tip
Since Vitest v0.22.0
:::

Both `c8` and `istanbul` support are optional. By default, `c8` will be used.

You can select the coverage tool by setting `test.coverage.provider` to either `c8` or `istanbul`:

```ts
// vite.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
coverage: {
provider: 'istanbul' // or 'c8'
},
},
})
```

When you start the Vitest process, it will prompt you to install the corresponding support package automatically.

Or if you prefer to install them manually:

```bash
npm i -D c8
# For c8
npm i -D @vitest/coverage-c8

# For istanbul
npm i -D @vitest/coverage-istanbul
```

Then you could get the coverage by passing the `--coverage` flag in CLI.
## Coverage Setup

To test with coverage enabled, you can pass the `--coverage` flag in CLI.

```json
{
Expand All @@ -35,3 +68,23 @@ export default defineConfig({
},
})
```

## Custom Coverage Provider

It's also possible to provide your custom coverage provider by passing an object to the `test.coverage.provider`:

```ts
// vite.config.ts
import { defineConfig } from 'vitest/config'
import CustomCoverageProvider from 'my-custom-coverage-provider'

export default defineConfig({
test: {
coverage: {
provider: CustomCoverageProvider()
},
},
})
```

Please refer to the type definition for more details.
17 changes: 2 additions & 15 deletions docs/guide/features.md
Expand Up @@ -136,7 +136,7 @@ Learn more at [Mocking](/guide/mocking).

## Coverage

Vitest supports Native code coverage via [c8](https://github.com/bcoe/c8).
Vitest supports Native code coverage via [`c8`](https://github.com/bcoe/c8) and instrumented code coverage via [`istanbul`](https://istanbul.js.org/).

```json
{
Expand All @@ -147,20 +147,7 @@ Vitest supports Native code coverage via [c8](https://github.com/bcoe/c8).
}
```

To configure it, set `test.coverage` options in your config file:

```ts
// vite.config.ts
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
coverage: {
reporter: ['text', 'json', 'html'],
},
},
})
```
Learn more at [Coverage](/guide/coverage).

## In-source testing

Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -42,6 +42,8 @@
"@types/node": "^18.7.3",
"@types/ws": "^8.5.3",
"@vitest/browser": "workspace:*",
"@vitest/coverage-c8": "workspace:*",
"@vitest/coverage-istanbul": "workspace:*",
"@vitest/ui": "workspace:*",
"bumpp": "^8.2.1",
"c8": "^7.12.0",
Expand All @@ -64,6 +66,7 @@
"rollup-plugin-esbuild": "^4.9.3",
"rollup-plugin-license": "^2.8.1",
"simple-git-hooks": "^2.8.0",
"tsup": "^6.2.2",
"typescript": "^4.7.4",
"vite": "^3.0.7",
"vitest": "workspace:*",
Expand Down
50 changes: 50 additions & 0 deletions packages/coverage-c8/package.json
@@ -0,0 +1,50 @@
{
"name": "@vitest/coverage-c8",
"type": "module",
"version": "0.0.0",
"description": "C8 coverage provider for Vitest",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/vitest-dev/vitest#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/vitest-dev/vitest.git",
"directory": "packages/coverage-c8"
},
"bugs": {
"url": "https://github.com/vitest-dev/vitest/issues"
},
"keywords": [
"vite",
"vitest",
"test",
"coverage",
"c8"
],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./*": "./*"
},
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "rimraf dist && rollup -c",
"dev": "rollup -c --watch --watch.include=src/**"
},
"dependencies": {
"c8": "^7.12.0",
"vitest": "workspace:*"
},
"devDependencies": {
"vite-node": "workspace:*"
}
}
62 changes: 62 additions & 0 deletions packages/coverage-c8/rollup.config.js
@@ -0,0 +1,62 @@
import { builtinModules } from 'module'
import esbuild from 'rollup-plugin-esbuild'
import dts from 'rollup-plugin-dts'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import alias from '@rollup/plugin-alias'
import nodeResolve from '@rollup/plugin-node-resolve'
import { join } from 'pathe'
import pkg from './package.json'

const entries = {
index: 'src/index.ts',
}

const external = [
...builtinModules,
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
'c8/lib/report.js',
'c8/lib/commands/check-coverage.js',
'vitest',
'vitest/node',
'vitest/config',
]

const plugins = [
alias({
entries: [
{ find: /^node:(.+)$/, replacement: '$1' },
],
}),
nodeResolve(),
json(),
commonjs(),
esbuild({
target: 'node14',
}),
]

export default () => [
{
input: entries,
output: {
dir: 'dist',
format: 'esm',
},
external,
plugins,
},
{
input: entries,
output: {
dir: join(process.cwd(), 'dist'),
entryFileNames: '[name].d.ts',
format: 'esm',
},
external,
plugins: [
dts({ respectExternal: true }),
],
},
]
6 changes: 6 additions & 0 deletions packages/coverage-c8/src/index.ts
@@ -0,0 +1,6 @@
export * from './takeCoverage'

export async function getProvider() {
const { C8CoverageProvider } = await import('./provider')
return new C8CoverageProvider()
}

0 comments on commit 265fdbe

Please sign in to comment.