nest icon indicating copy to clipboard operation
nest copied to clipboard

feat(testing): UseStub Decorator to simplify stubing of methods

Open oreofeolurin opened this issue 8 years ago • 2 comments

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.

oreofeolurin avatar Mar 15 '18 09:03 oreofeolurin

I'm not sure if we should really mix the application codebase with testing fixtures. They should be treated separately.

kamilmysliwiec avatar Mar 28 '18 16:03 kamilmysliwiec

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.

lock[bot] avatar Sep 25 '19 03:09 lock[bot]