Created
August 30, 2019 08:39
-
-
Save jishi/692a0474e248527a6b63239f3a683c22 to your computer and use it in GitHub Desktop.
Cool trick to test delayed execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| describe('when you send debug=timeout', () => { | |
| let fakeTimer; | |
| beforeEach(() => { | |
| lambdaParameter.queryStringParameters.debug = 'timeout'; | |
| }); | |
| beforeEach(() => { | |
| fakeTimer = sinon.useFakeTimers({ | |
| now: Date.now(), | |
| shouldAdvanceTime: true, | |
| toFake: ['setTimeout'] | |
| }); | |
| }); | |
| it('should not resolve until later', async () => { | |
| let executed = false; | |
| handler(lambdaParameter) | |
| .then(() => { | |
| executed = true; | |
| }); | |
| fakeTimer.tick(59000); | |
| await new Promise((resolve) => setImmediate(resolve)); // allow handler to execute | |
| expect(executed).to.be.false; | |
| fakeTimer.tick(2000); | |
| await new Promise((resolve) => setImmediate(resolve)); // allow handler to execute | |
| expect(executed).to.be.true; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment