Last active
October 22, 2019 19:45
-
-
Save setsun/e5e2349788bb141674041e7c69a9bd6f to your computer and use it in GitHub Desktop.
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
const incrementPromise = (value) => { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(value + 1); | |
}, 300); | |
}); | |
}; | |
describe('incrementPromise', () => { | |
beforeEach(() => { | |
jest.useFakeTimers(); | |
}); | |
afterEach(() => { | |
jest.useRealTimers(); | |
}); | |
it('should increment the number after 300 ms', () => { | |
// creates a Promise of "pending" status | |
const promise = incrementPromise(3); | |
// advance the Jest system clock by 300ms | |
jest.advanceTimersByTime(300); | |
// return the promise, and setup the expectation in the .then block | |
return promise.then((value) => { | |
expect(value).toBe(4); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment