Last active
January 25, 2021 16:04
-
-
Save ctbui/d59346cbe555e6bec25961d4b5f8bb55 to your computer and use it in GitHub Desktop.
some tests on async await and promises all
This file contains 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 timeoutPromise = timeout => | |
new Promise(resolve => setTimeout(resolve, timeout)); | |
describe('some tests on async await and promises all', function() { | |
it('should run async function in map loop', async function(done) { | |
// GIVEN | |
const mockFunction = jest.fn(); | |
const asyncFunctionWithAwait = async timeout => { | |
await timeoutPromise(timeout); | |
mockFunction(timeout); | |
}; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
timeoutArrays.map(timeout => asyncFunctionWithAwait(timeout)); | |
await timeoutPromise(4000); | |
expect(mockFunction.mock.calls.length).toBe(3); | |
done(); | |
}); | |
it('should run async await in map loop', async function(done) { | |
// GIVEN | |
const mockFunction = jest.fn(); | |
const asyncFunctionWithAwait = async timeout => { | |
await timeoutPromise(timeout); | |
mockFunction(timeout); | |
}; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
timeoutArrays.map(async timeout => await asyncFunctionWithAwait(timeout)); // ON A AJOUTE UN AWAIT ICI | |
await timeoutPromise(4000); | |
expect(mockFunction.mock.calls.length).toBe(3); | |
done(); | |
}); | |
it('should run Promise in map loop', async function(done) { | |
// GIVEN | |
const mockFunction = jest.fn(); | |
const createPromise = timeout => | |
new Promise(resolve => { | |
setTimeout(() => { | |
mockFunction(); | |
resolve(); | |
}, timeout); | |
}); | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
timeoutArrays.map(timeout => createPromise(timeout)); | |
await timeoutPromise(4000); | |
expect(mockFunction.mock.calls.length).toBe(3); | |
done(); | |
}); | |
it('should return promise with async', function() { | |
// GIVEN | |
const createNull = timeout => timeout; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
const resultPromises = timeoutArrays.map(async timeout => | |
createNull(timeout) | |
); | |
expect(resultPromises).toEqual(timeoutArrays); | |
}); | |
it('should return timeoutArrays when promise resolve all promises', async () => { | |
// GIVEN | |
const createNull = timeout => timeout; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
const resultPromises = timeoutArrays.map(async timeout => | |
createNull(timeout) | |
); | |
const result = await Promise.all(resultPromises); | |
expect(result).toEqual(timeoutArrays); | |
}); | |
it('should return timeoutArrays when promise resolve all objects', async () => { | |
// GIVEN | |
const createNull = timeout => timeout; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
const resultPromises = timeoutArrays.map(timeout => createNull(timeout)); | |
const result = await Promise.all(resultPromises); | |
expect(result).toEqual(timeoutArrays); | |
}); | |
describe('map async and error', () => { | |
it('should catch error with item.catch', async done => { | |
// GIVEN | |
const throwError = timeout => { | |
throw new Error(timeout); | |
}; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
const resultPromises = timeoutArrays | |
.map(async timeout => throwError(timeout)) | |
.map(item => item.catch(error => console.log('ignore error', error))); | |
const result = await Promise.all(resultPromises); | |
done(); | |
}); | |
it('should throw error outside a promise', async done => { | |
// GIVEN | |
const throwError = timeout => { | |
throw new Error(timeout); | |
}; | |
// WHEN | |
const timeoutArrays = [1001, 1002, 1003]; | |
const resultPromises = timeoutArrays | |
.map(timeout => throwError(timeout)) | |
.map(item => item.catch(error => console.log('ignore error', error))); | |
const result = await Promise.all(resultPromises); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment