Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: load deps reporter #2249

Merged
merged 9 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vitest/src/node/reporters/utils.ts
@@ -1,12 +1,16 @@
import type { ViteNodeRunner } from 'vite-node/client'
import { isAbsolute } from 'pathe'
import type { Reporter } from '../../types'
import { BenchmarkReportsMap, ReportersMap } from './index'
import type { BenchmarkBuiltinReporters, BuiltinReporters } from './index'

async function loadCustomReporterModule<C extends Reporter>(path: string, runner: ViteNodeRunner): Promise<new () => C> {
let customReporterModule: { default: new () => C }
try {
customReporterModule = await runner.executeFile(path)
if (isAbsolute(path))
customReporterModule = await runner.executeFile(path)
else
customReporterModule = await runner.executeId(path)
}
catch (customReporterModuleError) {
throw new Error(`Failed to load custom Reporter from ${path}`, { cause: customReporterModuleError as Error })
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.

8 changes: 8 additions & 0 deletions test/reporters/deps-reporter.vitest.config.ts
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['tests/reporters.spec.ts'],
reporters: ['pkg-reporter'],
},
})
1 change: 1 addition & 0 deletions test/reporters/package.json
Expand Up @@ -6,6 +6,7 @@
},
"devDependencies": {
"execa": "^6.1.0",
"pkg-reporter": "./reportPkg/",
"vitest": "workspace:*"
}
}
9 changes: 9 additions & 0 deletions test/reporters/reportPkg/index.js
@@ -0,0 +1,9 @@
export default class PackageReporter {
onInit(ctx) {
this.ctx = ctx
}

onFinished() {
this.ctx.logger.log('hello from package reporter')
}
}
4 changes: 4 additions & 0 deletions test/reporters/reportPkg/package.json
@@ -0,0 +1,4 @@
{
"name": "pkg-reporter",
"main": "./index.js"
}
5 changes: 5 additions & 0 deletions test/reporters/tests/custom-reporter.spec.ts
Expand Up @@ -45,6 +45,11 @@ describe.concurrent('custom reporters', () => {
expect(stdout).includes('hello from custom reporter')
}, 40000)

test('package.json dependencies reporter instances defined in configuration works', async () => {
const stdout = await runWithRetry('--config', 'deps-reporter.vitest.config.ts')
expect(stdout).includes('hello from package reporter')
}, 40000)

test('a path to a custom reporter defined in configuration works', async () => {
const stdout = await runWithRetry('--config', 'custom-reporter-path.vitest.config.ts', '--reporter', customJSReporterPath)
expect(stdout).includes('hello from custom reporter')
Expand Down