Skip to content

Commit

Permalink
test: workspace coverage with multiple transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Apr 25, 2023
1 parent 83f4da4 commit f9d095c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/coverage-c8/src/provider.ts
Expand Up @@ -100,8 +100,13 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage
const sourceMapMeta: Record<SourceMapMeta['url'], MapAndSource> = {}
const extensions = Array.isArray(this.options.extension) ? this.options.extension : [this.options.extension]

// TODO: Fixed in #3226
const fetchCache = this.ctx.projects.map(project =>
Array.from(project.vitenode.fetchCache.entries()),
).flat()

const entries = Array
.from(this.ctx.vitenode.fetchCache.entries())
.from(fetchCache)
.filter(entry => report._shouldInstrument(entry[0]))
.map(([file, { result }]) => {
if (!result.map)
Expand Down
6 changes: 6 additions & 0 deletions test/workspaces/space_1/test/multiple-transforms.spec.ts
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import run from '../../src/multiple-transforms'

test('cover space 1 related transforms', () => {
expect(run()).toBe('OK')
})
22 changes: 22 additions & 0 deletions test/workspaces/space_1/vite.config.ts
@@ -1,6 +1,28 @@
import { defineProject } from 'vitest/config'
import MagicString from 'magic-string'

export default defineProject({
plugins: [
{
name: 'custom:workspace-1',
transform(code, id) {
if (id.endsWith('multiple-transforms.ts')) {
const s = new MagicString(code)
const start = code.indexOf('//! WORKSPACE_1_REMOVE_START')
const end = code.indexOf('//! WORKSPACE_1_REMOVE_END')

s.remove(start, end + '//! WORKSPACE_1_REMOVE_END'.length)
return {
code: s.toString(),
map: s.generateMap({
hires: true,
source: id,
}),
}
}
},
},
],
test: {
name: 'space_1',
environment: 'happy-dom',
Expand Down
6 changes: 6 additions & 0 deletions test/workspaces/space_3/multiple-transforms.space-3-test.ts
@@ -0,0 +1,6 @@
import { expect, test } from 'vitest'
import run from '../src/multiple-transforms'

test('cover space 3 related transforms', () => {
expect(run()).toBe('OK')
})
22 changes: 22 additions & 0 deletions test/workspaces/space_3/vitest.config.ts
@@ -1,6 +1,28 @@
import MagicString from 'magic-string'
import { defineProject } from 'vitest/config'

export default defineProject({
plugins: [
{
name: 'custom:workspace-3',
transform(code, id) {
if (id.endsWith('multiple-transforms.ts')) {
const s = new MagicString(code)
const start = code.indexOf('//! WORKSPACE_3_REMOVE_START')
const end = code.indexOf('//! WORKSPACE_3_REMOVE_END')

s.remove(start, end + '//! WORKSPACE_3_REMOVE_END'.length)
return {
code: s.toString(),
map: s.generateMap({
hires: true,
source: id,
}),
}
}
},
},
],
test: {
include: ['**/*.space-3-test.ts'],
name: 'space_3',
Expand Down
54 changes: 54 additions & 0 deletions test/workspaces/src/multiple-transforms.ts
@@ -0,0 +1,54 @@
/* eslint-disable spaced-comment, unused-imports/no-unused-vars */

export default function run(a = 1, b = 2) {
//! WORKSPACE_1_REMOVE_START

// In workspace 1 we remove two functions
function removedFunctionOne() {
return 1
}
function removedFunctionThree() {
return 1
}

//! WORKSPACE_1_REMOVE_END

const unusedFunction1 = () => 1
const unusedFunction2 = () => a ? b : null

// Coverage of this branch breaks when combined coverage of workspaces is not handled correctly
if (a === 1000 && b === 1) {
// This should be uncovered
return 1001
}

//! WORKSPACE_3_REMOVE_START

// In workspace 3 we remove three branches
if (a === 100 && b === 1)
return 101

if (a === 100 && b === 2)
return 102

if (a === 100 && b === 3)
return 103

//! WORKSPACE_3_REMOVE_END

function unusedFunction3() {
return 1
}

// Coverage of this branch breaks when combined coverage of workspaces is not handled correctly
if (a === 1000 && b === 2) {
// This should be uncovered
return 1002
}

function unusedFunction4() {
return 1
}

return 'OK'
}

0 comments on commit f9d095c

Please sign in to comment.