Skip to content

Commit

Permalink
fix: load deps reporter (#2249)
Browse files Browse the repository at this point in the history
* test: load deps reporter

* chore: update

* fix: lint

* feat: more timeout

* feat: absolute path will executeId

* chore: update

* chore: update

* test: load report no base on root

* chore: update
  • Loading branch information
poyoho committed Nov 1, 2022
1 parent 7b78931 commit 113d564
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/vitest/src/node/reporters/utils.ts
Expand Up @@ -6,7 +6,7 @@ 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)
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"
}
13 changes: 13 additions & 0 deletions test/reporters/reportTest2/custom-reporter-path.vitest.config.ts
@@ -0,0 +1,13 @@
import { resolve } from 'pathe'
import { defineConfig } from 'vitest/config'

const customReporter = resolve(__dirname, '../src/custom-reporter.ts')

export default defineConfig({
root: __dirname,
test: {
root: __dirname,
include: ['../tests/reporters.spec.ts'],
reporters: [customReporter],
},
})
10 changes: 10 additions & 0 deletions test/reporters/tests/custom-reporter.spec.ts
Expand Up @@ -45,6 +45,16 @@ describe.concurrent('custom reporters', () => {
expect(stdout).includes('hello from custom reporter')
}, 40000)

test('load no base on root custom reporter instances defined in configuration works', async () => {
const stdout = await runWithRetry('--config', './reportTest2/custom-reporter-path.vitest.config.ts')
expect(stdout).includes('hello from custom reporter')
}, 50000)

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')
}, 50000)

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
2 changes: 1 addition & 1 deletion test/reporters/tests/utils.test.ts
Expand Up @@ -10,7 +10,7 @@ import TestReporter from '../src/custom-reporter'

const customReporterPath = resolve(__dirname, '../src/custom-reporter.js')
const fetchModule = {
executeFile: (id: string) => import(id),
executeId: (id: string) => import(id),
} as ViteNodeRunner

describe('Reporter Utils', () => {
Expand Down

0 comments on commit 113d564

Please sign in to comment.