Skip to content

Instantly share code, notes, and snippets.

@setsun
Last active October 22, 2019 19:45
Show Gist options
  • Save setsun/e5e2349788bb141674041e7c69a9bd6f to your computer and use it in GitHub Desktop.
Save setsun/e5e2349788bb141674041e7c69a9bd6f to your computer and use it in GitHub Desktop.
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