Skip to content

Commit

Permalink
fix(jest): jestjs/jest#10529
Browse files Browse the repository at this point in the history
  • Loading branch information
ssut committed Sep 29, 2021
1 parent 6f48a0a commit 70ad6f1
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 109 deletions.
110 changes: 61 additions & 49 deletions e2e/module.e2e-spec.ts
Expand Up @@ -37,8 +37,7 @@ const TestQueues: { [key in TestQueue]: SqsConsumerOptions | SqsProducerOptions
describe('SqsModule', () => {
let module: TestingModule;

describe.skip('register', () => {
});
describe.skip('register', () => {});

describe('registerAsync', () => {
let module: TestingModule;
Expand All @@ -53,12 +52,8 @@ describe('SqsModule', () => {
SqsModule.registerAsync({
useFactory: async () => {
return {
consumers: [
TestQueues[TestQueue.Test],
],
producers: [
TestQueues[TestQueue.Test],
],
consumers: [TestQueues[TestQueue.Test]],
producers: [TestQueues[TestQueue.Test]],
};
},
}),
Expand All @@ -79,10 +74,7 @@ describe('SqsModule', () => {

@Injectable()
class A {
public constructor(
public readonly sqsService: SqsService,
) {
}
public constructor(public readonly sqsService: SqsService) {}

@SqsMessageHandler(TestQueue.Test)
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down Expand Up @@ -125,9 +117,7 @@ describe('SqsModule', () => {
],
}),
],
providers: [
A,
],
providers: [A],
}).compile();
await module.init();

Expand Down Expand Up @@ -155,23 +145,30 @@ describe('SqsModule', () => {
expect(sqsService.producers.has(TestQueue.Test)).toBe(true);
});

it('should call message handler when a new message has come', async (done) => {
it('should call message handler when a new message has come', () => {
jest.setTimeout(30000);

const sqsService = module.get(SqsService);
const id = String(Math.floor(Math.random() * 1000000));
fakeProcessor.mockImplementationOnce((message) => {
expect(message).toBeTruthy();
expect(JSON.parse(message.Body)).toStrictEqual({ test: true });
done();
});

await sqsService.send(TestQueue.Test, {
id,
body: { test: true },
delaySeconds: 0,
groupId: 'test',
deduplicationId: id,
return new Promise(async (resolve, reject) => {
try {
fakeProcessor.mockImplementationOnce((message) => {
expect(message).toBeTruthy();
expect(JSON.parse(message.Body)).toStrictEqual({ test: true });
resolve(undefined);
});

await sqsService.send(TestQueue.Test, {
id,
body: { test: true },
delaySeconds: 0,
groupId: 'test',
deduplicationId: id,
});
} catch (e) {
reject(e);
}
});
});

Expand All @@ -192,44 +189,59 @@ describe('SqsModule', () => {
});
}

await waitForExpect(() => {
expect(fakeProcessor.mock.calls).toHaveLength(3);
for (const call of fakeProcessor.mock.calls) {
expect(call).toHaveLength(1);
expect(call[0]).toBeTruthy();
}
}, 5000, 100);
await waitForExpect(
() => {
expect(fakeProcessor.mock.calls).toHaveLength(3);
for (const call of fakeProcessor.mock.calls) {
expect(call).toHaveLength(1);
expect(call[0]).toBeTruthy();
}
},
5000,
100,
);
});

it('should call the registered error handler when an error occurs', async (done) => {
it('should call the registered error handler when an error occurs', () => {
jest.setTimeout(10000);

const sqsService = module.get(SqsService);
const id = String(Math.floor(Math.random() * 1000000));
fakeProcessor.mockImplementationOnce((message) => {
throw new Error('test');
});
fakeErrorEventHandler.mockImplementationOnce((error, message) => {
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain('test');
done();
});

await sqsService.send(TestQueue.Test, {
id,
body: { test: true },
delaySeconds: 0,
groupId: 'test',
deduplicationId: id,
return new Promise(async (resolve, reject) => {
try {
fakeErrorEventHandler.mockImplementationOnce((error, message) => {
expect(error).toBeInstanceOf(Error);
expect(error.message).toContain('test');
resolve(undefined);
});

await sqsService.send(TestQueue.Test, {
id,
body: { test: true },
delaySeconds: 0,
groupId: 'test',
deduplicationId: id,
});
} catch (e) {
reject(e);
}
});
});

it('should consume a dead letter from DLQ', async () => {
jest.setTimeout(10000);

await waitForExpect(() => {
expect(fakeDLQProcessor.mock.calls.length).toBe(1);
}, 9900, 500);
await waitForExpect(
() => {
expect(fakeDLQProcessor.mock.calls.length).toBe(1);
},
9900,
500,
);
});
});
});

0 comments on commit 70ad6f1

Please sign in to comment.