Skip to content

Commit

Permalink
test: move plugins from inline to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Feb 18, 2024
1 parent 19a18ee commit 196e843
Showing 1 changed file with 63 additions and 54 deletions.
117 changes: 63 additions & 54 deletions test/coverage-test/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,15 @@ import { defineConfig } from 'vitest/config'
import vue from '@vitejs/plugin-vue'
import MagicString from 'magic-string'
import remapping from '@ampproject/remapping'
import type { Plugin } from 'vite'

const provider = process.argv[1 + process.argv.indexOf('--provider')]

export default defineConfig(() => ({
export default defineConfig(_ => ({
plugins: [
vue(),
/*
* Transforms `multi-environment.ts` differently based on test environment (JSDOM/Node)
* so that there are multiple different source maps for a single file.
* This causes a case where coverage report is incorrect if sourcemaps are not picked based on transform mode.
*/
{
name: 'vitest-custom-multi-transform',
enforce: 'pre',
transform(code, id, options) {
if (id.includes('src/multi-environment')) {
const ssr = options?.ssr || false
const transforMode = `transformMode is ${ssr ? 'ssr' : 'csr'}`
const padding = '\n*****'.repeat(ssr ? 0 : 15)

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

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

return { code: transformed.toString(), map }
}
},
},
{
// Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
name: 'vitest-custom-virtual-files',
resolveId(id) {
if (id === 'virtual:vitest-custom-virtual-file-1')
return 'src/virtual:vitest-custom-virtual-file-1.ts'

if (id === '\0vitest-custom-virtual-file-2')
return 'src/\0vitest-custom-virtual-file-2.ts'
},
load(id) {
if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #1"
export default virtualFile;
`
}

// Vitest browser resolves this as "\x00", Node as "__x00__"
if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #2"
export default virtualFile;
`
}
},
},
MultiTransformPlugin(),
VirtualFilesPlugin(),
],
define: {
MY_CONSTANT: '"my constant"',
Expand Down Expand Up @@ -105,3 +55,62 @@ export default defineConfig(() => ({
],
},
}))

/*
* Transforms `multi-environment.ts` differently based on test environment (JSDOM/Node)
* so that there are multiple different source maps for a single file.
* This causes a case where coverage report is incorrect if sourcemaps are not picked based on transform mode.
*/
function MultiTransformPlugin(): Plugin {
return {
name: 'vitest-custom-multi-transform',
enforce: 'pre',
transform(code, id, options) {
if (id.includes('src/multi-environment')) {
const ssr = options?.ssr || false
const transforMode = `transformMode is ${ssr ? 'ssr' : 'csr'}`
const padding = '\n*****'.repeat(ssr ? 0 : 15)

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

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

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

// Simulates Vite's virtual files: https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
function VirtualFilesPlugin(): Plugin {
return {
name: 'vitest-custom-virtual-files',
resolveId(id) {
if (id === 'virtual:vitest-custom-virtual-file-1')
return 'src/virtual:vitest-custom-virtual-file-1.ts'

if (id === '\0vitest-custom-virtual-file-2')
return 'src/\0vitest-custom-virtual-file-2.ts'
},
load(id) {
if (id === 'src/virtual:vitest-custom-virtual-file-1.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #1"
export default virtualFile;
`
}

// Vitest browser resolves this as "\x00", Node as "__x00__"
if (id === 'src/__x00__vitest-custom-virtual-file-2.ts' || id === 'src/\x00vitest-custom-virtual-file-2.ts') {
return `
const virtualFile = "This file should be excluded from coverage report #2"
export default virtualFile;
`
}
},
}
}

0 comments on commit 196e843

Please sign in to comment.