Skip to content

Commit

Permalink
test(coverage): option specific test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Aug 16, 2023
1 parent 0ff798a commit 639830a
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 274 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,6 @@

exports[`istanbul json report 1`] = `
{
"<process-cwd>/../test-utils/fixtures/math.ts": {
"b": {},
"branchMap": {},
"f": {
"0": 1,
"1": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 20,
"line": 1,
},
"start": {
"column": 16,
"line": 1,
},
},
"loc": {
"end": {
"column": null,
"line": 3,
},
"start": {
"column": 42,
"line": 1,
},
},
"name": "sum",
},
"1": {
"decl": {
"end": {
"column": 25,
"line": 5,
},
"start": {
"column": 16,
"line": 5,
},
},
"loc": {
"end": {
"column": null,
"line": 7,
},
"start": {
"column": 47,
"line": 5,
},
},
"name": "multiply",
},
},
"path": "<process-cwd>/../test-utils/fixtures/math.ts",
"s": {
"0": 1,
"1": 0,
},
"statementMap": {
"0": {
"end": {
"column": null,
"line": 2,
},
"start": {
"column": 2,
"line": 2,
},
},
"1": {
"end": {
"column": null,
"line": 6,
},
"start": {
"column": 2,
"line": 6,
},
},
},
},
"<process-cwd>/src/Counter/Counter.component.ts": {
"b": {},
"branchMap": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,178 +2,6 @@

exports[`v8 json report 1`] = `
{
"<process-cwd>/../test-utils/fixtures/math.ts": {
"all": false,
"b": {
"0": [
1,
],
},
"branchMap": {
"0": {
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"locations": [
{
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
],
"type": "branch",
},
},
"f": {
"0": 1,
"1": 0,
},
"fnMap": {
"0": {
"decl": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"line": 1,
"loc": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 7,
"line": 1,
},
},
"name": "sum",
},
"1": {
"decl": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 7,
"line": 5,
},
},
"line": 5,
"loc": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 7,
"line": 5,
},
},
"name": "multiply",
},
},
"path": "<process-cwd>/../test-utils/fixtures/math.ts",
"s": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1,
"5": 0,
"6": 0,
},
"statementMap": {
"0": {
"end": {
"column": 43,
"line": 1,
},
"start": {
"column": 0,
"line": 1,
},
},
"1": {
"end": {
"column": 14,
"line": 2,
},
"start": {
"column": 0,
"line": 2,
},
},
"2": {
"end": {
"column": 1,
"line": 3,
},
"start": {
"column": 0,
"line": 3,
},
},
"3": {
"end": {
"column": 0,
"line": 4,
},
"start": {
"column": 0,
"line": 4,
},
},
"4": {
"end": {
"column": 48,
"line": 5,
},
"start": {
"column": 0,
"line": 5,
},
},
"5": {
"end": {
"column": 14,
"line": 6,
},
"start": {
"column": 0,
"line": 6,
},
},
"6": {
"end": {
"column": 1,
"line": 7,
},
"start": {
"column": 0,
"line": 7,
},
},
},
},
"<process-cwd>/src/Counter/Counter.component.ts": {
"all": false,
"b": {
Expand Down
20 changes: 20 additions & 0 deletions test/coverage-test/coverage-report-tests/allow-external.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'node:fs'
import { expect, test } from 'vitest'

const allowExternal = import.meta.env.VITE_COVERAGE_ALLOW_EXTERNAL

test.skipIf(!allowExternal)('{ allowExternal: true } includes files outside project root', async () => {
expect(fs.existsSync('./coverage/test-utils/fixtures/math.ts.html')).toBe(true)

// Files inside project root should always be included
expect(fs.existsSync('./coverage/coverage-test/src/utils.ts.html')).toBe(true)
})

test.skipIf(allowExternal)('{ allowExternal: false } excludes files outside project root', async () => {
expect(fs.existsSync('./coverage/test-utils/fixtures/math.ts.html')).toBe(false)
expect(fs.existsSync('./test-utils/fixtures/math.ts.html')).toBe(false)
expect(fs.existsSync('./fixtures/math.ts.html')).toBe(false)

// Files inside project root should always be included
expect(fs.existsSync('./coverage/utils.ts.html')).toBe(true)
})
17 changes: 9 additions & 8 deletions test/coverage-test/coverage-report-tests/generic.report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import libCoverage from 'istanbul-lib-coverage'
import { readCoverageJson } from './utils'

test('html report', async () => {
const coveragePath = resolve('./coverage/coverage-test/src')
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('index.html')
Expand All @@ -31,14 +31,14 @@ test('lcov report', async () => {
})

test('all includes untested files', () => {
const coveragePath = resolve('./coverage/coverage-test/src')
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('untested-file.ts.html')
})

test('files should not contain query parameters', () => {
const coveragePath = resolve('./coverage/coverage-test/src/Counter')
const coveragePath = resolve('./coverage/src/Counter')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('index.html')
Expand All @@ -48,19 +48,20 @@ test('files should not contain query parameters', () => {
})

test('file using import.meta.env is included in report', async () => {
const coveragePath = resolve('./coverage/coverage-test/src')
const coveragePath = resolve('./coverage/src')
const files = fs.readdirSync(coveragePath)

expect(files).toContain('importEnv.ts.html')
})

test('files should not contain a setup file', () => {
const coveragePath = resolve('./coverage/coverage-test')
const coveragePath = resolve('./coverage')
const files = fs.readdirSync(coveragePath)

expect(files).not.toContain('coverage-test')
expect(files).not.toContain('setup.ts.html')

const coverageSrcPath = resolve('./coverage/coverage-test/src')
const coverageSrcPath = resolve('./coverage/src')
const srcFiles = fs.readdirSync(coverageSrcPath)

expect(srcFiles).not.toContain('another-setup.ts.html')
Expand Down Expand Up @@ -102,8 +103,8 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile',
})

test('virtual files should be excluded', () => {
const files = fs.readdirSync(resolve('./coverage/coverage-test'))
const srcFiles = fs.readdirSync(resolve('./coverage/coverage-test/src'))
const files = fs.readdirSync(resolve('./coverage'))
const srcFiles = fs.readdirSync(resolve('./coverage/src'))

for (const file of [...files, ...srcFiles]) {
expect(file).not.toContain('virtual:')
Expand Down
4 changes: 1 addition & 3 deletions test/coverage-test/coverage-report-tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,5 @@ export async function readCoverageJson() {
}

export function normalizeFilename(filename: string) {
return normalize(filename)
.replace(normalize(process.cwd()), '<process-cwd>')
.replace(normalize(process.cwd().replace('coverage-test', '')), '<process-cwd>/../')
return normalize(filename).replace(normalize(process.cwd()), '<process-cwd>')
}
12 changes: 12 additions & 0 deletions test/coverage-test/option-tests/allow-external.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { expect, test } from 'vitest'

import { multiply } from '../src/utils'
import * as ExternalMath from '../../test-utils/fixtures/math'

test('calling files outside project root', () => {
expect(ExternalMath.sum(2, 3)).toBe(5)
})

test('multiply - add some files to report', () => {
expect(multiply(2, 3)).toBe(6)
})
1 change: 1 addition & 0 deletions test/coverage-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test:custom": "node ./testing.mjs --provider custom",
"test:istanbul": "node ./testing.mjs --provider istanbul",
"test:browser": "node ./testing.mjs --browser --provider istanbul",
"test:options": "node ./testing-options.mjs",
"test:types": "vitest typecheck --run --reporter verbose"
},
"devDependencies": {
Expand Down

0 comments on commit 639830a

Please sign in to comment.