question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for dynamic mocking frameworks

See original GitHub issue

I’m submitting a…


[ ] Regression 
[X] 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.

Current behavior

When running e2e tests, app.init() throws the following the following exception if I override a provider with a dynamic mock.

    TypeError: instance.onModuleInit is not a function

      at MapIterator.Promise.all.iterare_1.default.map.filter.filter.map [as iteratee] (../node_modules/@nestjs/core/nest-application-context.js:64:47)
      at MapIterator.next (../node_modules/iterare/src/map.ts:11:39)
      at IteratorWithOperators.next (../node_modules/iterare/src/iterate.ts:21:28)
          at Function.all (<anonymous>)
      at NestApplication.callModuleInitHook (../node_modules/@nestjs/core/nest-application-context.js:60:23)
      at NestApplication.callInitHook (../node_modules/@nestjs/core/nest-application-context.js:51:24)
      at NestApplication.init (../node_modules/@nestjs/core/nest-application.js:96:20)

Expected behavior

The ablity to override providers with dynamic mocks.

Minimal reproduction of the problem with instructions

Just to establish some context…

  • the mocking api is ts-mockito
  • the exists method on the PageService is used by the controllers to determine if a page exists
describe('Page API', () => {
  let app: INestApplication;
  const mockPageService: PageService = mock(PageService);

  beforeAll(async () => {
    const mod = await Test.createTestingModule({
      imports: [PagesModule],
    })
      .overrideProvider(PageService)
      .useValue(instance(mockPageService))
      .compile();

    app = mod.createNestApplication();
    await app.init();  // <---- exception is thrown here
  });

  it('[GET] /pages', () => {
    return request(app.getHttpServer())
      .get('/pages')
      .expect(200);
  });

  describe('[GET] /pages/:slug', () => {
    beforeAll(() => {
      when(mockPageService.exists('non-existent-page')).thenReturn(false);
      when(mockPageService.exists('example-page')).thenReturn(true);
    });
    it('should return 4o4 when :slug cannot be found', () => {
      return request(app.getHttpServer())
        .get('/pages/non-existent-page')
        .expect(404);
    });

    it('should return 200 when :slug can be found', () => {
      return request(app.getHttpServer())
        .get('/pages/example-page')
        .expect(200);
    });
  });

  afterAll(async () => {
    await app.close();
  });
});

What is the motivation / use case for changing the behavior?

Dynamic mocks are awesome.

Environment


Nest version: 5.5.0

 
For Tooling issues:
- Node version: v8.11.1  
- Platform: Windows 10  

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
yavivicommented, Jun 9, 2019

The issue still remains. For further clarification, please see this branch of my repository.

Get same issue on nestjs 6.1.1

1reaction
BrunnerLiviocommented, Aug 21, 2019

I looked into it; it is actually a issue by Nest. In the following function we check whether instance.onModuleInit is undefined.

https://github.com/nestjs/nest/blob/baa20004fccca575336d7455a3a3e754400cc0e9/packages/core/hooks/on-module-init.hook.ts#L16-L18

But (probably) due to the mocking library, instance.onModuleInit is null, therefore hasOnModuleInitHook returns true.

Changing it to return !isNil((instance as OnModuleInit).onModuleInit); will fix this issue.

Thanks a lot for your repository @nrhoffmann, it allowed me to get to the root cause of this issue quickly ! Sorry for your inconveniences and closing this issue. We review a lot of issues, so every now and then an issue gets closed, which should not have been 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Isolation (mocking) frameworks - The Art of Unit Testing ...
Defining isolation frameworks and how they help; Two main “flavors” of frameworks; ... These objects are referred to as dynamic stubs and dynamic...
Read more >
API Mocking Tools and Best Practices - Section.io
The responses can be static or dynamic and help mimic the actual data that a real API would return. ... API mocking frameworks...
Read more >
Introduction to .Net Mocking Frameworks - Wright
Net mocking frameworks support three basic types of mock objects: ... Dynamic Mock: With a dynamic mock, any methods/properties which are ...
Read more >
9 helpful tools for creating mock REST servers
9 tools and techniques for mocking HTTP requests - from API design suites to online and local mock servers - to ensure product...
Read more >
Are there any mocking libraries that support the .NET Compact ...
The better mocking frameworks use IL emit, the older ones use ... uses that to create assemblies on the storage card, and then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found