Skip to content

Commit

Permalink
test: add failing test for vitest-dev#3251
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Oct 20, 2023
1 parent dfac928 commit 3974b83
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

22 changes: 22 additions & 0 deletions test/workspaces/coverage-report-tests/check-coverage.test.ts
Expand Up @@ -19,3 +19,25 @@ test('file coverage summary matches', () => {
expect(branches.total - branches.covered).toBe(1)
expect(functions.total - functions.covered).toBe(1)
})

test('multi environment coverage is merged correctly', async () => {
const coverageJson = JSON.parse(readFileSync('./coverage/coverage-final.json', 'utf-8'))
const coverageMap = libCoverage.createCoverageMap(coverageJson)
const fileCoverage = coverageMap.fileCoverageFor(resolve('./space-multi-transform/src/multi-transform.ts'))
const lineCoverage = fileCoverage.getLineCoverage()

// Condition not covered by any test
expect(lineCoverage[13]).toBe(0)

// Condition covered by Project #1 but not by Project #2
expect(lineCoverage[18]).toBe(1)

// Condition not covered by any test
expect(lineCoverage[22]).toBe(0)

// Condition covered by Project #1 but not by Project #2
expect(lineCoverage[26]).toBe(1)

// Condition covered by both tests
expect(lineCoverage[30]).toBe(2)
})
1 change: 1 addition & 0 deletions test/workspaces/package.json
Expand Up @@ -8,6 +8,7 @@
"test:coverage": "vitest run --root coverage-report-tests"
},
"devDependencies": {
"@ampproject/remapping": "^2.2.1",
"@types/istanbul-lib-coverage": "^2.0.4",
"istanbul-lib-coverage": "^3.2.0",
"jsdom": "latest",
Expand Down
31 changes: 31 additions & 0 deletions test/workspaces/space-multi-transform/src/multi-transform.ts
@@ -0,0 +1,31 @@
/**
* The variable below is modified by custom Vite plugin
*/
export const padding = 'default-padding'

export function run(name: string) {
/*
* These if-branches should show correctly on coverage report.
* Otherwise source maps are off.
*/
if (name === 'not-covered') {
// This is not covered by any test
return 0
}
// Comment
else if (name === 'project-1') {
// This is covered by Project #1
return 1
}
else if (name === 'not-covered-2') {
// This is not covered by any test
return 0
}
else if (name === 'project-2') {
// This is covered by Project #2
return 2
}

// This is covered by both projects, should show 2x hits
return 3
}
9 changes: 9 additions & 0 deletions test/workspaces/space-multi-transform/test/project-1.test.ts
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'

import { run } from '../src/multi-transform'

test('cover some branches', () => {
expect(run('project-1')).toBe(1)

expect(run('last branch')).toBe(3)
})
9 changes: 9 additions & 0 deletions test/workspaces/space-multi-transform/test/project-2.test.ts
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'

import { run } from '../src/multi-transform'

test('cover some branches', () => {
expect(run('project-2')).toBe(2)

expect(run('last branch')).toBe(3)
})
44 changes: 44 additions & 0 deletions test/workspaces/vitest.workspace.ts
@@ -1,4 +1,7 @@
import { defineWorkspace } from 'vitest/config'
import MagicString from 'magic-string'
import remapping from '@ampproject/remapping'
import type { Plugin } from 'vite'

export default defineWorkspace([
'./space_2/*',
Expand All @@ -19,4 +22,45 @@ export default defineWorkspace([
setupFiles: ['./setup.node.ts'],
},
},

// These two projects run on same environment but still transform
// a single file differently due to Vite plugins
{
plugins: [customPlugin(0)],
test: {
name: 'Project with custom plugin #1',
environment: 'node',
include: ['./space-multi-transform/test/project-1.test.ts'],
},
},
{
plugins: [customPlugin(15)],
test: {
name: 'Project with custom plugin #2',
environment: 'node',
include: ['./space-multi-transform/test/project-2.test.ts'],
},
},
])

function customPlugin(offset: number): Plugin {
return {
name: 'vitest-custom-multi-transform',
enforce: 'pre',
transform(code, id) {
if (id.includes('space-multi-transform/src/multi-transform.ts')) {
const padding = '\n*****'.repeat(offset)

const transformed = new MagicString(code)
transformed.replace('\'default-padding\'', `\`${padding}\``)

const map = remapping(
[transformed.generateMap({ hires: true }), this.getCombinedSourcemap() as any],
() => null,
) as any

return { code: transformed.toString(), map }
}
},
}
}

0 comments on commit 3974b83

Please sign in to comment.