Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

2.6 KB
Raw Blame
/** @jest-environment-options {"customExportConditions":["node","node-addons"]} */
// jsdom resolves uuid to an ESM browser build, while jest-junit runs under Node in CI.

import { mkdtempSync, readFileSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'

const JestJUnit = require('jest-junit')

describe('JUnit retry reports', () => {
    test.each([
        ['passed', [], undefined],
        ['passed', ['first attempt <failed>'], 'flakyFailure'],
        ['failed', ['first attempt <failed>'], 'rerunFailure'],
    ])('reports %s with retry evidence %j', (status, retryReasons, retryTag) => {
        const directory = mkdtempSync(join(tmpdir(), 'junit-retries-'))
        const originalEnvironment = process.env
        process.env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('JEST_JUNIT_')))
        try {
            const reporter = new JestJUnit(
                { rootDir: directory },
                { outputDirectory: directory, outputName: 'junit.xml' }
            )
            reporter.onRunComplete(new Set(), {
                startTime: Date.now(),
                testResults: [
                    {
                        testFilePath: join(directory, 'example.test.js'),
                        perfStats: { start: Date.now(), end: Date.now() },
                        numFailingTests: status === 'failed' ? 1 : 0,
                        numPassingTests: status === 'passed' ? 1 : 0,
                        numPendingTests: 0,
                        testResults: [
                            {
                                ancestorTitles: ['example'],
                                title: 'retries',
                                duration: 1,
                                status,
                                retryReasons,
                                failureMessages: status === 'failed' ? ['final failure'] : [],
                            },
                        ],
                    },
                ],
            })
            const report = readFileSync(join(directory, 'junit.xml'), 'utf8')
            expect(report.match(/<testcase\s/g)).toHaveLength(1)
            expect(report.includes('<failure>')).toBe(status === 'failed')
            if (retryTag) {
                expect(report).toContain(`<${retryTag}>first attempt &lt;failed&gt;</${retryTag}>`)
            } else {
                expect(report).not.toMatch(/<(flaky|rerun)Failure>/)
            }
        } finally {
            process.env = originalEnvironment
            rmSync(directory, { recursive: true, force: true })
        }
    })
})