Skip to content
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

feat: support istanbul coverage provider #1676

Merged
merged 35 commits into from Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bf752f9
refactor: move `coverage.ts` to `coverage/c8.ts`
AriPerkkio Jul 17, 2022
28f0bef
refactor: create interface for coverage logic
AriPerkkio Jul 17, 2022
d98028f
refactor: add `NullCoverageProvider` to handled disabled coverage
AriPerkkio Jul 22, 2022
7e20301
feat: support `istanbul` coverage provider
AriPerkkio Jul 19, 2022
0126cf4
test: `istanbul` coverage provider
AriPerkkio Aug 2, 2022
1b05a3d
docs: `istanbul` coverage provider
AriPerkkio Aug 4, 2022
3519f6d
refactor: provide info on coverage report
antfu Aug 13, 2022
f7f3156
wip: move to packages
antfu Aug 13, 2022
684f7e3
fix: make it working
antfu Aug 15, 2022
93291ad
refactor: use top-level imports for istanbul
antfu Aug 15, 2022
1f662ea
docs: update
antfu Aug 15, 2022
c228115
chore: lint
antfu Aug 15, 2022
d675dab
refactor: update coverage provider interface
antfu Aug 15, 2022
5c0e3aa
refactor: ship ESM only for coverage support
antfu Aug 15, 2022
7ce1e0e
feat: support custom coverage provider
antfu Aug 15, 2022
ba6ac5c
Merge remote-tracking branch 'origin/main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
9d53170
chore: fix lock
antfu Aug 15, 2022
04d4cc4
chore: cli docs
antfu Aug 15, 2022
a4e86b1
Merge remote-tracking branch 'origin/main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
dd38877
chore: fix types
antfu Aug 15, 2022
3055192
Merge remote-tracking branch 'origin/main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
ce30200
chore: fix build
antfu Aug 15, 2022
f8722fa
Merge remote-tracking branch 'origin/main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
cd927b9
chore: update deps
antfu Aug 15, 2022
a5cee1c
Merge remote-tracking branch 'origin/main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
380c210
chore: lint
antfu Aug 15, 2022
246e146
chore: upgrade esno
antfu Aug 15, 2022
4edf2ad
Merge branch 'main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
016030d
chore: update
antfu Aug 15, 2022
43dd177
chore: update
antfu Aug 15, 2022
4fb4dcd
Merge branch 'main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
1b1f059
Merge branch 'main' into pr/AriPerkkio/1676
antfu Aug 15, 2022
bc15516
chore: fix tsconfig
antfu Aug 15, 2022
4b4b0a6
chore: use rollup
antfu Aug 15, 2022
4ff81ae
chore: update
antfu Aug 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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": "tsup",
"dev": "tsup --watch=src"
},
"dependencies": {
"c8": "^7.12.0",
"vitest": "workspace:*"
},
"devDependencies": {
"vite-node": "workspace:*"
}
}
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()
}