Skip to content

Instantly share code, notes, and snippets.

@jishi
Created August 30, 2019 08:39
Show Gist options
  • Select an option

  • Save jishi/692a0474e248527a6b63239f3a683c22 to your computer and use it in GitHub Desktop.

Select an option

Save jishi/692a0474e248527a6b63239f3a683c22 to your computer and use it in GitHub Desktop.
Cool trick to test delayed execution
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