|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, afterEach } from 'vitest' |
| 5 | +import * as mocha from 'mocha' |
| 6 | + |
| 7 | +import $Runner from '../../../src/cypress/runner' |
| 8 | + |
| 9 | +// Match the import shape used by @packages/driver's cypress/mocha.ts |
| 10 | +// so we exercise the same Mocha constructor the driver consumes. |
| 11 | +const Mocha = (mocha as any).Mocha != null ? (mocha as any).Mocha : mocha |
| 12 | +const { Runner, Suite } = Mocha |
| 13 | + |
| 14 | +describe('@packages/driver/src/cypress/runner', () => { |
| 15 | + const createdRealRunners: any[] = [] |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + // Dispose any real Mocha runners created during a test so their `process` |
| 19 | + // listeners don't bleed into subsequent tests. |
| 20 | + while (createdRealRunners.length) { |
| 21 | + try { |
| 22 | + createdRealRunners.pop().dispose() |
| 23 | + } catch { /* noop */ } |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + // Minimal stubs for the arguments $Runner.create() expects. Each helper |
| 28 | + // returns just enough surface area for the factory to construct without |
| 29 | + // throwing; individual tests can override fields as needed. |
| 30 | + const makeCypressStub = () => { |
| 31 | + return { |
| 32 | + testingType: 'component', |
| 33 | + action: vi.fn(), |
| 34 | + emit: vi.fn(), |
| 35 | + emitThen: vi.fn(), |
| 36 | + config: vi.fn(() => false), |
| 37 | + env: vi.fn(() => undefined), |
| 38 | + state: vi.fn(), |
| 39 | + log: vi.fn(), |
| 40 | + isBrowser: vi.fn(() => false), |
| 41 | + browser: { family: 'chromium' }, |
| 42 | + backend: vi.fn(), |
| 43 | + stop: vi.fn(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const makeCyStub = () => { |
| 48 | + return { |
| 49 | + state: vi.fn(), |
| 50 | + onUncaughtException: vi.fn(), |
| 51 | + currentTest: null, |
| 52 | + stop: vi.fn(), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + const makeStateStub = () => vi.fn() |
| 57 | + |
| 58 | + const makeSpecWindow = () => ({ addEventListener: vi.fn() }) as unknown as Window |
| 59 | + |
| 60 | + // Builds the `mocha` wrapper argument $Runner.create() expects, backed by |
| 61 | + // a real Mocha Runner so tests can observe real Mocha behavior. |
| 62 | + const makeMochaWrapper = () => { |
| 63 | + const suite = new Suite('root', {} as any) |
| 64 | + const runner = new Runner(suite) |
| 65 | + |
| 66 | + createdRealRunners.push(runner) |
| 67 | + |
| 68 | + return { |
| 69 | + wrapper: { |
| 70 | + getRunner: () => runner, |
| 71 | + getRootSuite: () => suite, |
| 72 | + }, |
| 73 | + runner, |
| 74 | + suite, |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + it('calls dispose() on the underlying mocha runner when the run completes', () => { |
| 79 | + const { wrapper, runner } = makeMochaWrapper() |
| 80 | + const disposeSpy = vi.spyOn(runner, 'dispose') |
| 81 | + |
| 82 | + const api = $Runner.create( |
| 83 | + makeSpecWindow(), |
| 84 | + wrapper, |
| 85 | + makeCypressStub(), |
| 86 | + makeCyStub(), |
| 87 | + makeStateStub(), |
| 88 | + ) |
| 89 | + |
| 90 | + api.run(() => {}) |
| 91 | + |
| 92 | + // Simulate mocha finishing the run by firing EVENT_RUN_END. |
| 93 | + // The callback registered by $Runner.run is what invokes dispose(). |
| 94 | + runner.emit('end') |
| 95 | + |
| 96 | + expect(disposeSpy).toHaveBeenCalledTimes(1) |
| 97 | + }) |
| 98 | + |
| 99 | + it('removes the uncaughtException listener from `process` after a run completes', () => { |
| 100 | + const { wrapper, runner } = makeMochaWrapper() |
| 101 | + const baseline = process.listenerCount('uncaughtException') |
| 102 | + |
| 103 | + const api = $Runner.create( |
| 104 | + makeSpecWindow(), |
| 105 | + wrapper, |
| 106 | + makeCypressStub(), |
| 107 | + makeCyStub(), |
| 108 | + makeStateStub(), |
| 109 | + ) |
| 110 | + |
| 111 | + // api.run → _runner.run(cb): mocha synchronously adds the |
| 112 | + // `uncaughtException` listener on `process` (mocha's runner.js) and |
| 113 | + // registers the EVENT_RUN_END handler that will invoke cb. |
| 114 | + api.run(() => {}) |
| 115 | + |
| 116 | + expect(process.listenerCount('uncaughtException')).toBe(baseline + 1) |
| 117 | + |
| 118 | + // Simulate run completion: fires EVENT_RUN_END, which invokes our |
| 119 | + // callback, which calls _runner.dispose(), which removes the listener. |
| 120 | + runner.emit('end') |
| 121 | + |
| 122 | + expect(process.listenerCount('uncaughtException')).toBe(baseline) |
| 123 | + }) |
| 124 | + |
| 125 | + it('does not accumulate process listeners across multiple run/end cycles', () => { |
| 126 | + // Simulates the Cypress rerun lifecycle: each "rerun" creates a new |
| 127 | + // $Runner, calls api.run(), then ends. After each cycle, the process |
| 128 | + // listener count should return to baseline. |
| 129 | + const baseline = process.listenerCount('uncaughtException') |
| 130 | + |
| 131 | + for (let i = 0; i < 5; i++) { |
| 132 | + const { wrapper, runner } = makeMochaWrapper() |
| 133 | + |
| 134 | + const api = $Runner.create( |
| 135 | + makeSpecWindow(), |
| 136 | + wrapper, |
| 137 | + makeCypressStub(), |
| 138 | + makeCyStub(), |
| 139 | + makeStateStub(), |
| 140 | + ) |
| 141 | + |
| 142 | + api.run(() => {}) |
| 143 | + runner.emit('end') |
| 144 | + } |
| 145 | + |
| 146 | + expect(process.listenerCount('uncaughtException')).toBe(baseline) |
| 147 | + }) |
| 148 | +}) |
0 commit comments