Skip to content

Instantly share code, notes, and snippets.

@ctbui
Last active January 25, 2021 16:04
Show Gist options
  • Save ctbui/d59346cbe555e6bec25961d4b5f8bb55 to your computer and use it in GitHub Desktop.
Save ctbui/d59346cbe555e6bec25961d4b5f8bb55 to your computer and use it in GitHub Desktop.
some tests on async await and promises all
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