Skip to content

Commit

Permalink
fix(coverage): prevent reportsDirectory from removing user's project
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Mar 12, 2024
1 parent 6eda473 commit 0012cec
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,10 @@ Clean coverage report on watch rerun
- **Available for providers:** `'v8' | 'istanbul'`
- **CLI:** `--coverage.reportsDirectory=<path>`

::: warning
Vitest will delete this directory before running tests if `coverage.clean` is enabled (default value).
:::

Directory to write coverage report to.

To preview the coverage report in the output of [HTML reporter](/guide/reporters.html#html-reporter), this option must be set as a sub-directory of the html report directory (for example `./html/coverage`).
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,21 @@ export function resolveConfig(
}
}

// TODO: V2.0.0 remove
// @ts-expect-error -- check for removed API option
if (resolved.coverage.provider === 'c8')
throw new Error('"coverage.provider: c8" is not supported anymore. Use "coverage.provider: v8" instead')

if (resolved.coverage.provider === 'v8' && resolved.coverage.enabled && isBrowserEnabled(resolved))
throw new Error('@vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')

if (resolved.coverage.enabled && resolved.coverage.reportsDirectory) {
const reportsDirectory = resolve(resolved.root, resolved.coverage.reportsDirectory)

if (reportsDirectory === resolved.root || reportsDirectory === process.cwd())
throw new Error(`You cannot set "coverage.reportsDirectory" as ${reportsDirectory}. Vitest needs to be able to remove this directory before test run`)
}

resolved.deps ??= {}
resolved.deps.moduleDirectories ??= []
resolved.deps.moduleDirectories = resolved.deps.moduleDirectories.map((dir) => {
Expand Down
38 changes: 38 additions & 0 deletions test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from 'vitest'
import type { UserConfig } from 'vitest/config'
import { version } from 'vitest/package.json'

import { normalize, resolve } from 'pathe'
import * as testUtils from '../../test-utils'

function runVitest(config: NonNullable<UserConfig['test']> & { shard?: any }) {
Expand Down Expand Up @@ -67,6 +68,43 @@ test('v8 coverage provider cannot be used with browser in workspace', async () =
expect(stderr).toMatch('Error: @vitest/coverage-v8 does not work with --browser. Use @vitest/coverage-istanbul instead')
})

test('coverage reportsDirectory cannot be current working directory', async () => {
const { stderr } = await runVitest({
coverage: {
enabled: true,
reportsDirectory: './',

// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})

const directory = normalize(resolve('./'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})

test('coverage reportsDirectory cannot be root', async () => {
const { stderr } = await runVitest({
root: './fixtures',
coverage: {
enabled: true,
reportsDirectory: './',

// Additional options to make sure this test doesn't accidentally remove whole vitest project
clean: false,
cleanOnRerun: false,
provider: 'custom',
customProviderModule: 'non-existing-provider-so-that-reportsDirectory-is-not-removed',
},
})

const directory = normalize(resolve('./fixtures'))
expect(stderr).toMatch(`Error: You cannot set "coverage.reportsDirectory" as ${directory}. Vitest needs to be able to remove this directory before test run`)
})

test('version number is printed when coverage provider fails to load', async () => {
const { stderr, stdout } = await runVitest({
coverage: {
Expand Down

0 comments on commit 0012cec

Please sign in to comment.