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(watch): junit reporter fails to re-generate report #3496

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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