mirror of
https://github.com/bendtherules/JSQuestions.git
synced 2026-08-18 13:53:51 +00:00
Fix testcases
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
const TaskScheduler = require('./async-scheduler');
|
||||
|
||||
// Suppress unhandled rejection warnings in tests
|
||||
process.on('unhandledRejection', () => {});
|
||||
|
||||
describe('TaskScheduler', () => {
|
||||
describe('constructor', () => {
|
||||
@@ -34,6 +32,7 @@ describe('TaskScheduler', () => {
|
||||
const scheduler = new TaskScheduler();
|
||||
scheduler.play();
|
||||
const promise = scheduler.addTask(() => Promise.reject(new Error('task failed')));
|
||||
promise.catch(() => {}); // Prevent unhandled rejection
|
||||
await expect(promise).rejects.toThrow('task failed');
|
||||
});
|
||||
|
||||
@@ -43,6 +42,7 @@ describe('TaskScheduler', () => {
|
||||
const promise = scheduler.addTask(() => {
|
||||
throw new Error('sync error');
|
||||
});
|
||||
promise.catch(() => {}); // Prevent unhandled rejection
|
||||
await expect(promise).rejects.toThrow('sync error');
|
||||
});
|
||||
|
||||
@@ -218,24 +218,19 @@ describe('TaskScheduler', () => {
|
||||
expect(mockFn2).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
// it('should pick next task even if current task rejects', async () => {
|
||||
// const scheduler = new TaskScheduler(1);
|
||||
// const mockFn2 = jest.fn(() => Promise.resolve());
|
||||
it('should pick next task even if current task rejects', async () => {
|
||||
const scheduler = new TaskScheduler(1);
|
||||
const mockFn2 = jest.fn(() => Promise.resolve());
|
||||
|
||||
// const p1 = scheduler.addTask(() => Promise.reject(new Error("this should fail")));
|
||||
// p1.catch(() => {}); // suppress unhandled rejection
|
||||
// scheduler.play();
|
||||
scheduler.play();
|
||||
const p1 = scheduler.addTask(() => Promise.reject(new Error("this should fail")));
|
||||
p1.catch(() => {}); // suppress unhandled rejection
|
||||
|
||||
// try {
|
||||
// await p1;
|
||||
// } catch (e) {
|
||||
// // expected
|
||||
// }
|
||||
// const p2 = scheduler.addTask(mockFn2);
|
||||
const p2 = scheduler.addTask(mockFn2);
|
||||
await p2;
|
||||
|
||||
// await p2;
|
||||
// expect(mockFn2).toHaveBeenCalled();
|
||||
// });
|
||||
expect(mockFn2).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('edge cases', () => {
|
||||
|
||||
Reference in New Issue
Block a user