Skip to content

Commit

Permalink
fix: context clonning error when creating custom reporter (fix #614) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Demivan committed Jan 25, 2022
1 parent db1a601 commit 0feb256
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
20 changes: 17 additions & 3 deletions packages/vitest/src/node/core.ts
Expand Up @@ -7,7 +7,7 @@ import c from 'picocolors'
import { ViteNodeServer } from 'vite-node/server'
import type { ArgumentsType, RawSourceMap, Reporter, ResolvedConfig, UserConfig } from '../types'
import { SnapshotManager } from '../integrations/snapshot/manager'
import { clone, deepMerge, hasFailed, noop, slash, toArray } from '../utils'
import { deepMerge, hasFailed, noop, slash, toArray } from '../utils'
import { cleanCoverage, reportCoverage } from '../coverage'
import { DefaultReporter, ReportersMap } from './reporters'
import { createPool } from './pool'
Expand Down Expand Up @@ -92,9 +92,23 @@ export class Vitest {
}

getConfig() {
const hasCustomReporter = toArray(this.config.reporters)
.some(reporter => typeof reporter !== 'string')

if (!hasCustomReporter && !this.configOverride)
return this.config

const config = deepMerge({}, this.config)

if (this.configOverride)
return deepMerge(clone(this.config), this.configOverride) as ResolvedConfig
return this.config
deepMerge(config, this.configOverride)

// Custom reporters cannot be serialized for sending to workers #614
// but workers don't need reporters anyway
if (hasCustomReporter)
config.reporters = []

return config as ResolvedConfig
}

async start(filters?: string[]) {
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

23 changes: 23 additions & 0 deletions test/reporters/custom-reporter.vitest.config.ts
@@ -0,0 +1,23 @@
import type { Reporter } from 'vitest'
import type { Vitest } from 'vitest/src/node'

import { defineConfig } from 'vite'

class TestReporter implements Reporter {
ctx!: Vitest

onInit(ctx) {
this.ctx = ctx
}

onFinished() {
this.ctx.log('hello from custom reporter')
}
}

export default defineConfig({
test: {
include: ['tests/reporters.spec.ts'],
reporters: ['default', new TestReporter()],
},
})
3 changes: 2 additions & 1 deletion test/reporters/package.json
Expand Up @@ -2,9 +2,10 @@
"name": "@vitest/test-reporters",
"private": true,
"scripts": {
"test": "vitest --global"
"test": "vitest"
},
"devDependencies": {
"execa": "^6.0.0",
"vitest": "workspace:*"
}
}
18 changes: 18 additions & 0 deletions test/reporters/tests/custom-reporter.spec.ts
@@ -0,0 +1,18 @@
import { execa } from 'execa'
import { resolve } from 'pathe'
import { expect, test } from 'vitest'

test('custom resolvers work with threads', async() => {
const root = resolve(__dirname, '..')

const { stdout } = await execa('npx', ['vitest', 'run', '--config', 'custom-reporter.vitest.config.ts'], {
cwd: root,
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
})

expect(stdout).toContain('hello from custom reporter')
}, 20000)
2 changes: 1 addition & 1 deletion test/reporters/tests/reporters.spec.ts
@@ -1,4 +1,4 @@
import { expect, test, vi } from 'vitest'
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import { JsonReporter } from '../../../packages/vitest/src/node/reporters/json'
import { JUnitReporter } from '../../../packages/vitest/src/node/reporters/junit'
import { TapReporter } from '../../../packages/vitest/src/node/reporters/tap'
Expand Down
5 changes: 0 additions & 5 deletions test/reporters/tsconfig.json

This file was deleted.

0 comments on commit 0feb256

Please sign in to comment.