|
| 1 | +/// <reference types="mocha" /> |
| 2 | + |
| 3 | +import * as chai from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | + |
| 6 | +import {loggerClient} from '@testring/logger'; |
| 7 | +import {TestEvents} from '@testring/types'; |
| 8 | + |
| 9 | +import {run} from '../src/run'; |
| 10 | +import {TestContext} from '../src/test-context'; |
| 11 | +import {testAPIController} from '../src/test-api-controller'; |
| 12 | + |
| 13 | +const TEST_ID = 'test.js'; |
| 14 | +const LOG_PREFIX = '[logged inside test]'; |
| 15 | + |
| 16 | +type Restorable = { |
| 17 | + restore: () => void; |
| 18 | +}; |
| 19 | + |
| 20 | +type RunEvents = { |
| 21 | + failedErrors: Error[]; |
| 22 | + getFinishedCount: () => number; |
| 23 | + cleanup: () => void; |
| 24 | +}; |
| 25 | + |
| 26 | +function prepareTestAPI(): void { |
| 27 | + testAPIController.setTestID(TEST_ID); |
| 28 | + testAPIController.setTestParameters({runData: {}}); |
| 29 | + testAPIController.setEnvironmentParameters({}); |
| 30 | +} |
| 31 | + |
| 32 | +function observeRunEvents(): RunEvents { |
| 33 | + const bus = testAPIController.getBus(); |
| 34 | + let finishedCount = 0; |
| 35 | + const failedErrors: Error[] = []; |
| 36 | + |
| 37 | + const finishedHandler = () => { |
| 38 | + finishedCount += 1; |
| 39 | + }; |
| 40 | + const failedHandler = (error: Error) => { |
| 41 | + failedErrors.push(error); |
| 42 | + }; |
| 43 | + |
| 44 | + bus.on(TestEvents.finished, finishedHandler); |
| 45 | + bus.on(TestEvents.failed, failedHandler); |
| 46 | + |
| 47 | + return { |
| 48 | + failedErrors, |
| 49 | + getFinishedCount: () => finishedCount, |
| 50 | + cleanup: () => { |
| 51 | + bus.removeListener(TestEvents.finished, finishedHandler); |
| 52 | + bus.removeListener(TestEvents.failed, failedHandler); |
| 53 | + }, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function track<T extends Restorable>( |
| 58 | + restorables: Restorable[], |
| 59 | + restorable: T, |
| 60 | +): T { |
| 61 | + restorables.push(restorable); |
| 62 | + |
| 63 | + return restorable; |
| 64 | +} |
| 65 | + |
| 66 | +function restoreAll(restorables: Restorable[]): void { |
| 67 | + for (const restorable of restorables.reverse()) { |
| 68 | + restorable.restore(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +describe('TestContext', () => { |
| 73 | + let restorables: Restorable[]; |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + restorables = []; |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + restoreAll(restorables); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should log cleanup errors as warnings and rethrow them', async () => { |
| 84 | + const context = new TestContext({}); |
| 85 | + const cleanupError = new Error('cleanup failed'); |
| 86 | + const application = { |
| 87 | + isStopped: sinon.stub().returns(false), |
| 88 | + end: sinon.stub().rejects(cleanupError), |
| 89 | + }; |
| 90 | + const warn = track(restorables, sinon.stub(loggerClient, 'warn')); |
| 91 | + |
| 92 | + Object.defineProperty(context, 'application', { |
| 93 | + value: application, |
| 94 | + configurable: true, |
| 95 | + }); |
| 96 | + |
| 97 | + try { |
| 98 | + await context.end(); |
| 99 | + chai.assert.fail('Expected context.end() to reject.'); |
| 100 | + } catch (error) { |
| 101 | + chai.expect(error).to.equal(cleanupError); |
| 102 | + } |
| 103 | + |
| 104 | + chai.expect(warn.calledOnceWithExactly(LOG_PREFIX, cleanupError)).to.be |
| 105 | + .equal(true); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('run', () => { |
| 110 | + let restorables: Restorable[]; |
| 111 | + let events: RunEvents; |
| 112 | + let endStep: ReturnType<typeof sinon.stub>; |
| 113 | + |
| 114 | + beforeEach(() => { |
| 115 | + restorables = []; |
| 116 | + prepareTestAPI(); |
| 117 | + events = observeRunEvents(); |
| 118 | + track(restorables, sinon.stub(loggerClient, 'startStep')); |
| 119 | + endStep = track(restorables, sinon.stub(loggerClient, 'endStep')); |
| 120 | + }); |
| 121 | + |
| 122 | + afterEach(() => { |
| 123 | + events.cleanup(); |
| 124 | + restoreAll(restorables); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should finish the test when body and cleanup pass', async () => { |
| 128 | + const end = track(restorables, sinon.stub(TestContext.prototype, 'end')); |
| 129 | + end.resolves(); |
| 130 | + |
| 131 | + await run(() => undefined); |
| 132 | + |
| 133 | + chai.expect(end.calledOnce).to.equal(true); |
| 134 | + chai.expect(events.getFinishedCount()).to.equal(1); |
| 135 | + chai.expect(events.failedErrors).to.deep.equal([]); |
| 136 | + chai.expect(endStep.calledOnceWithExactly(TEST_ID, 'Test passed')).to |
| 137 | + .equal(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should fail the test when cleanup fails after a passed body', async () => { |
| 141 | + const cleanupError = new Error('cleanup failed'); |
| 142 | + const end = track(restorables, sinon.stub(TestContext.prototype, 'end')); |
| 143 | + end.rejects(cleanupError); |
| 144 | + |
| 145 | + await run(() => undefined); |
| 146 | + |
| 147 | + chai.expect(end.calledOnce).to.equal(true); |
| 148 | + chai.expect(events.getFinishedCount()).to.equal(0); |
| 149 | + chai.expect(events.failedErrors).to.deep.equal([cleanupError]); |
| 150 | + chai.expect( |
| 151 | + endStep.calledOnceWithExactly( |
| 152 | + TEST_ID, |
| 153 | + 'Test failed', |
| 154 | + cleanupError, |
| 155 | + ), |
| 156 | + ).to.equal(true); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should keep the body error primary when body and cleanup both fail', async () => { |
| 160 | + const bodyError = new Error('body failed'); |
| 161 | + const cleanupError = new Error('cleanup failed'); |
| 162 | + const end = track(restorables, sinon.stub(TestContext.prototype, 'end')); |
| 163 | + end.rejects(cleanupError); |
| 164 | + |
| 165 | + await run(() => { |
| 166 | + throw bodyError; |
| 167 | + }); |
| 168 | + |
| 169 | + chai.expect(end.calledOnce).to.equal(true); |
| 170 | + chai.expect(events.getFinishedCount()).to.equal(0); |
| 171 | + chai.expect(events.failedErrors).to.deep.equal([bodyError]); |
| 172 | + chai.expect( |
| 173 | + endStep.calledOnceWithExactly(TEST_ID, 'Test failed', bodyError), |
| 174 | + ).to.equal(true); |
| 175 | + }); |
| 176 | +}); |
0 commit comments