Skip to content

Commit

Permalink
fix(watch): junit reporter fails to re-generate report
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Jun 2, 2023
1 parent 2deb70a commit 04f6e17
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
8 changes: 7 additions & 1 deletion packages/vitest/src/node/reporters/junit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ export class JUnitReporter implements Reporter {
const fileFd = await fs.open(this.reportFile, 'w+')
this.fileFd = fileFd

this.baseLog = async (text: string) => await fs.writeFile(fileFd, `${text}\n`)
this.baseLog = async (text: string) => {
if (!this.fileFd)
this.fileFd = await fs.open(this.reportFile!, 'w+')

await fs.writeFile(this.fileFd, `${text}\n`)
}
}
else {
this.baseLog = async (text: string) => this.ctx.logger.log(text)
Expand Down Expand Up @@ -248,5 +253,6 @@ export class JUnitReporter implements Reporter {
this.ctx.logger.log(`JUNIT report written to ${this.reportFile}`)

await this.fileFd?.close()
this.fileFd = undefined
}
}
2 changes: 1 addition & 1 deletion test/test-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export async function runVitestCli(_options?: Options | string, ...args: string[
return resolve()

const timeout = setTimeout(() => {
reject(new Error(`Timeout when waiting for output "${expected}".\nReceived:\n${this.stdout}`))
reject(new Error(`Timeout when waiting for output "${expected}".\nReceived:\n${this.stdout}. \nStderr:\n${this.stderr}`))
}, process.env.CI ? 20_000 : 4_000)

const listener = () => {
Expand Down
1 change: 1 addition & 0 deletions test/watch/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures/test-results
33 changes: 31 additions & 2 deletions test/watch/test/file-watching.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readFileSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, test } from 'vitest'
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { afterEach, describe, expect, test } from 'vitest'

import { runVitestCli } from '../../test-utils'

Expand Down Expand Up @@ -88,6 +88,35 @@ test("dynamic test case", () => {
await vitest.waitForStdout('1 passed')
})

test('editing source file generates new test report to file system', async () => {
const report = 'fixtures/test-results/junit.xml'
if (existsSync(report))
rmSync(report)

// Test report should not be present before test run
expect(existsSync(report)).toBe(false)

const vitest = await runVitestCli(
...cliArgs,
'--reporter', 'verbose',
'--reporter', 'junit',
'--output-file', 'test-results/junit.xml',
)

// Test report should be generated on initial test run
expect(existsSync(report)).toBe(true)

// Test report should be re-generated on second test run
rmSync(report)
expect(existsSync(report)).toBe(false)

writeFileSync(sourceFile, editFile(sourceFileContent), 'utf8')

await vitest.waitForStdout('JUNIT report written')
await vitest.waitForStdout(report)
expect(existsSync(report)).toBe(true)
})

describe('browser', () => {
test.runIf((process.platform !== 'win32'))('editing source file triggers re-run', async () => {
const vitest = await runVitestCli(...cliArgs, '--browser.enabled', '--browser.headless', '--browser.name=chrome')
Expand Down

0 comments on commit 04f6e17

Please sign in to comment.