Testing can be frustrating, especially when we do not have a well documented library/framework from the perspective of writing tests. E.g. I was banging my head against brick wall as to why my E2E test was failing and it turns out that NestJS's @Processor
decorator is doing some sort of magic.
Thus my test was failing. I tried different approaches but none worked:
- Tried to mock the whole thing with
ioredis-mock
. Nope it was still failing: - I tried Testcontainers but still I was getting an error.
- I had a real redis instance running and it was still not passing.
So What was My Issue When I Tried to Mock the Whole Thing
First I tried to write a code like this:
import { getQueueToken } from '@nestjs/bullmq';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import { mockBullMqService } from './bullmq.mock';
describe('AppController (e2e)', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(getQueueToken('YOUR_QUEUE_NAME'))
.useValue({
on: jest.fn(),
add: jest.fn(),
process: jest.fn(),
})
.compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('/ (GET)', () => {
return request(app.getHttpServer())
.get('/cart')
.expect(200)
.expect('Hi');
});
// Add more tests here
});
But it was not working because in my src/user/audio.consumer.ts
I had used @Processor
decorator. And this is not documented anywhere. Yes, it sucks.
So to overcome this problem you have to do something like this in your test:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(getQueueToken('YOUR_QUEUE_NAME'))
.useValue({
on: jest.fn(),
add: jest.fn(),
process: jest.fn(),
})
+ .overrideProvider(AudioConsumer)
+ .useValue({})
.compile();
And with this you should be good to go.
My Issue With Testcontainers
Have not solved it yet. And the real redis instance, I have not figured that out either. BTW you can see the whole conversation of me with me for the most parts here in discord.
If you're passionate about developing safe software for deploying on friday without having to fear this kind of consequences:
You need to start thinking about writing tests and probably E2E or unit tests or a combination of different tests. This gives you the confidence you need to deploy and be sure that with it you have not introduces a bug to the existing feature.
But for sure the new features always are prone to bugs, but at least you know what you've covered (scenarios) and if something goes wrong you will add that as a new test case to your existing ones. This potentially help you to reduce the amount of fires you need to put out :).
Make sure to follow me, and also give my repo a star:
Instagram: https://www.instagram.com/node.js.developers.kh/
Facebook: https://www.facebook.com/kasirbarati
X: https://x.com/kasir_barati
YouTube: https://www.youtube.com/@kasir-barati
GitHub: https://github.com/kasir-barati/
Dev.to: https://dev.to/kasir-barati
LinkedIn: https://linkedin.com/in/kasir-barati
Top comments (0)