diff --git a/packages/vitest/src/node/reporters/junit.ts b/packages/vitest/src/node/reporters/junit.ts index 9437bb83331d..9252aac524c6 100644 --- a/packages/vitest/src/node/reporters/junit.ts +++ b/packages/vitest/src/node/reporters/junit.ts @@ -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) @@ -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 } } diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index 9cd8612e38d8..99ab08ece2e9 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -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 = () => { diff --git a/test/watch/.gitignore b/test/watch/.gitignore new file mode 100644 index 000000000000..8639dfc40993 --- /dev/null +++ b/test/watch/.gitignore @@ -0,0 +1 @@ +fixtures/test-results \ No newline at end of file diff --git a/test/watch/test/file-watching.test.ts b/test/watch/test/file-watching.test.ts index e00cf891704f..877e61346b59 100644 --- a/test/watch/test/file-watching.test.ts +++ b/test/watch/test/file-watching.test.ts @@ -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' @@ -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')