feat(testing): UseStub Decorator to simplify stubing of methods
I'm submitting a...
[ ] Regression
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Hello guys, I would like to suggest a feature to provide stubs for a class using decorators and use it alongside overrideComponent . This would enhance nest testing experience.
Now consider this following piece of code that stubs catsService class.
describe('Cats', () => {
const catsService = { findAll: () => ['test'] };
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [CatsModule],
})
.overrideComponent(CatsService).useValue(catsService)
.compile();
});
});
Now that might seem easier, but it gets more complicated as your project grows, you probably need to write tons of stubs for a class. So I'am suggesting this instead:
class CatsService {
@UseStub(['test'])
findAll() {
// Logic goes here
}
}
describe('Cats', () => {
beforeAll(async () => {
const module = await Test.createTestingModule({
imports: [CatsModule],
})
.overrideComponent(CatsService).useStub()
.compile();
});
});
In case you feel its necessary and deserves a PR, I could send one right away.
I'm not sure if we should really mix the application codebase with testing fixtures. They should be treated separately.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.