|
| 1 | +/** |
| 2 | + * Jest setup file that automatically wraps describe and it/test functions |
| 3 | + * to register suites and tests with Qase. |
| 4 | + * |
| 5 | + * The Qase Jest reporter reports the directory structure up from the tests directory, |
| 6 | + * making it not consistent with the test suite structure we currently follow in Qase. |
| 7 | + * The solution is to wrap describe and it/test functions to automatically set the suite |
| 8 | + * at the very start of the test to what we really want the reporting structure to be. |
| 9 | + * |
| 10 | + * This file is loaded via setupFilesAfterEnv in jest.config.federation.ts. |
| 11 | + * Qase integration is only enabled when QASE_TESTOPS_JEST_API_TOKEN is set. |
| 12 | + */ |
| 13 | + |
| 14 | +import { qase } from 'jest-qase-reporter/jest'; |
| 15 | + |
| 16 | +const ROOT_SUITE = 'Rocket.Chat Federation Automation'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Stack to track the current suite path hierarchy |
| 20 | + */ |
| 21 | +const suitePathStack: string[] = []; |
| 22 | + |
| 23 | +/** |
| 24 | + * Store the original Jest describe function before we replace it |
| 25 | + */ |
| 26 | +const originalDescribe = global.describe; |
| 27 | + |
| 28 | +/** |
| 29 | + * Gets the full suite path including root |
| 30 | + */ |
| 31 | +function getFullSuitePath(): string { |
| 32 | + return [ROOT_SUITE, ...suitePathStack].join('\t'); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Wraps describe to automatically track suite hierarchy and set suite for tests |
| 37 | + */ |
| 38 | +function describeImpl(name: string, fn: () => void): void { |
| 39 | + suitePathStack.push(name); |
| 40 | + const currentPath = getFullSuitePath(); |
| 41 | + |
| 42 | + originalDescribe(name, () => { |
| 43 | + // Add beforeEach to set suite for all tests in this describe block |
| 44 | + // This must be called before the test runs so the reporter picks it up |
| 45 | + global.beforeEach(() => { |
| 46 | + qase.suite(currentPath); |
| 47 | + }); |
| 48 | + |
| 49 | + // Store current it and test wrappers (they might be wrapped by parent describe) |
| 50 | + const currentIt = global.it; |
| 51 | + const currentTest = global.test; |
| 52 | + |
| 53 | + // Wrap it() to automatically set suite at the very start |
| 54 | + global.it = ((testName: any, fn?: any, timeout?: number) => { |
| 55 | + // Handle qase-wrapped test names (qase returns a string) |
| 56 | + if (typeof testName === 'string' && fn) { |
| 57 | + return currentIt( |
| 58 | + testName, |
| 59 | + async () => { |
| 60 | + // Set suite immediately at the start of the test |
| 61 | + qase.suite(currentPath); |
| 62 | + // Call the original test function and return the result |
| 63 | + return fn(); |
| 64 | + }, |
| 65 | + timeout, |
| 66 | + ); |
| 67 | + } |
| 68 | + // Handle cases where testName might be a number or other type |
| 69 | + return currentIt(testName, fn, timeout); |
| 70 | + }) as typeof global.it; |
| 71 | + |
| 72 | + // Wrap test() to automatically set suite at the very start |
| 73 | + global.test = ((testName: any, fn?: any, timeout?: number) => { |
| 74 | + if (typeof testName === 'string' && fn) { |
| 75 | + return currentTest( |
| 76 | + testName, |
| 77 | + async () => { |
| 78 | + // Set suite immediately at the start of the test |
| 79 | + qase.suite(currentPath); |
| 80 | + // Call the original test function and return the result |
| 81 | + return fn(); |
| 82 | + }, |
| 83 | + timeout, |
| 84 | + ); |
| 85 | + } |
| 86 | + return currentTest(testName, fn, timeout); |
| 87 | + }) as typeof global.test; |
| 88 | + |
| 89 | + // Execute the describe block |
| 90 | + fn(); |
| 91 | + |
| 92 | + // Restore previous wrappers |
| 93 | + global.it = currentIt; |
| 94 | + global.test = currentTest; |
| 95 | + }); |
| 96 | + |
| 97 | + suitePathStack.pop(); |
| 98 | +} |
| 99 | + |
| 100 | +// Only apply qase wrapping if the environment variable is set |
| 101 | +if (process.env.QASE_TESTOPS_JEST_API_TOKEN) { |
| 102 | + // Replace global describe with our wrapper |
| 103 | + (global as any).describe = Object.assign(describeImpl, { |
| 104 | + skip: (name: string, fn: () => void) => originalDescribe.skip(name, fn), |
| 105 | + only: (name: string, fn: () => void) => originalDescribe.only(name, fn), |
| 106 | + }) as typeof global.describe; |
| 107 | +} |
0 commit comments