forwardemail / forwardemail/supertest

jest.setTimeout.Error when setting header "accept: application/pdf" in POST request

Open
#626 4 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
14.4k
Forks
782
PR merge metrics
No merged PRs in 30d

Description

By performing a unit test of a controller as proposed in the Nest.js [Documentation](https://docs.nestjs.com/fundamentals/testing#end-to-end-testing) I found a really strange behavior when trying to set the header `{ accept: "application/pdf" }` which triggers a `jest.setTimeout.Error`.

It is curious because I have another test, which is theoretically the same but the difference is `{ accept: "application/xml" }` and this one does not trigger the above mentioned error.

These are the two Test code snippets

```typescript
describe('GaebManager', () => {
let app: INestApplication;
const mockedOfferTextsService = {
getXmlOffer: (): string => 'Retrieved XML Offers',
createXmlOffers: (): string => 'Created XML Offers',
createPdfOffers: (): PDFKit.PDFDocument => {
return new PDFDocument();
},
};
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
controllers: [OfferTextsController],
providers: [OfferTextsService],
})
.overrideProvider(OfferTextsService)
.useValue(mockedOfferTextsService)
.compile();
app = moduleRef.createNestApplication();
await app.init();
});

describe('createXmlOfferTexts', () => {
it('should response with 200 and set application/pdf header', async () => {
await request(app.getHttpServer())
.post('/xml-offers')
.set('accept', 'application/pdf')
.expect(HttpStatus.OK)
.expect('content-type', /pdf/);
});
});

describe('createXmlOfferTexts', () => {
it('should response with 200 and set application/xml header', async () => {
await request(app.getHttpServer())
.post('/xml-offers')
.set('accept', 'application/xml')
.expect(HttpStatus.OK)
.expect('content-type', /xml/);
});
});

afterAll(async () => await app.close());
});
```
`jest.setTimeout.Error` log response

```
: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest
.setTimeout.Timeout - Async callback was not invoked within the 5000ms timeout specified b
y jest.setTimeout.Error:

37 |
38 | describe('createXmlOfferTexts', () => {
> 39 | it('should response with 200 and set application/pdf header', async () => {
| ^
40 | await request(app.getHttpServer())
41 | .post('/xml-offers')
42 | .set('accept', 'application/pdf')

at new Spec (../node_modules/jest-jasmine2/build/jasmine/Spec.js:116:22)
at Suite. (offerTexts/offerTexts.controller.spec.ts:39:3)
at Suite. (offerTexts/offerTexts.controller.spec.ts:38:2)
at Object. (offerTexts/offerTexts.controller.spec.ts:8:1)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.