Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestingModuleBuilder is missing overrideModule #4905

Closed
oschlegel opened this issue Jun 14, 2020 · 16 comments
Closed

TestingModuleBuilder is missing overrideModule #4905

oschlegel opened this issue Jun 14, 2020 · 16 comments

Comments

@oschlegel
Copy link

Feature Request

Is your feature request related to a problem? Please describe.

I want to use mock modules to override functionalities of the module instead of overriding internals of the module again and again.

Describe the solution you'd like

A overrideModule method similar to overrideProvider would solve the issue, example:

Test.createTestingModule({
  ...
})
  .overrideModule(LoggerModule)
  .useClass(LoggerTestingModule)
  .compile();

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

For the cases of e2e / integration testing in a project with many encapsulated libraries / modules it would be very helpful to be able to override entire modules instead of overriding internals of the modules every time the module is part of a testing module.

@oschlegel oschlegel added needs triage This issue has not been looked into type: enhancement 🐺 labels Jun 14, 2020
@kamilmysliwiec
Copy link
Member

Sounds interesting! Would you like to try to create a PR for this issue?

@kamilmysliwiec kamilmysliwiec added scope: testing and removed needs triage This issue has not been looked into labels Jun 18, 2020
@moshewe
Copy link

moshewe commented Jun 29, 2020

I'm testing in a hyper-microservice environment and I could really use this feature. I was on the cusp of giving up on nestjs e2e testing entirely and just do unit and integration tests (e.g. Postman) because I was loosing too much time on figuring out why certain mocks work and others don't.

@oschlegel let me know how I can help.

@oschlegel
Copy link
Author

I will build a solution if i can find some time

@anajuliabit
Copy link

anajuliabit commented Sep 22, 2020

This is exactly what I need. I'm working in a application that has one module whose uses several services from another modules and override all internals imports modules is very painful

@kamilmysliwiec kamilmysliwiec added effort3: weeks priority: low (4) Low-priority issue that needs to be resolved labels Feb 2, 2021
@nileger
Copy link

nileger commented Oct 23, 2021

Any news on this one? Would be really helpful! Can anyone give an estimate?

@kamilmysliwiec
Copy link
Member

@NilsLeger feel free to contribute to this issue, PRs are more than welcome!

@joebowbeer
Copy link

joebowbeer commented Dec 12, 2021

See withModule method in TestModuleBuilder from @jbiskur/nestjs-test-utilities

@leonardovillela
Copy link
Contributor

Hi everyone, good evening. I took some time last week to add this functionality on testing-module.builder.ts API.

Maintainers(cc: @kamilmysliwiec) when you have time please, take a look at this PR.

@kamilmysliwiec
Copy link
Member

Thanks @leonardovillela! Let's track this here #8777

@jdelgadofrts
Copy link

Do we have any updates on the PR? #8777

I could really use this feature! :)

@MartianH
Copy link

Do we have any updates on the PR? #8777

I could really use this feature! :)

I concur. Would be very useful.

@navarrojava
Copy link

+1

1 similar comment
@woodinteractive
Copy link

+1

@elovin
Copy link

elovin commented Jun 30, 2023

As far as I can tell this has been added to nestjs 10 ?!

Yet the injected "CACHE_MANAGER" does not use the settings from the overwritten module:

@Inject(CACHE_MANAGER) cacheManager: Cache

class MockCacheModule extends CacheModule {
	static override register<StoreConfig extends Record<any, any> = Record<string, any>>(
		options?: CacheModuleOptions<StoreConfig>,
	): DynamicModule {
		return CacheModule.register({ ...options, ttl: 100 });
	}
}

const module = await Test.createTestingModule({
	imports: [DatabaseModule],
})
	.overrideProvider(ProjectConfigService)
	.useClass(ProjectConfigTestService)
	.overrideModule(CacheModule)
	.useModule(MockCacheModule)
	.compile();

@fmrsabino
Copy link

As far as I can tell this has been added to nestjs 10 ?!

Yet the injected "CACHE_MANAGER" does not use the settings from the overwritten module:

I had a similar issue with a DynamicModule. I got a suggestion on how to tackle it in #11967. It might help with your use-case.

@elovin
Copy link

elovin commented Jul 18, 2023

@fmrsabino thank you, now that I think about it, this makes perfect sense.

Exporting the module config which uses the dynamic cache module and then overwriting that module instance worked as expected.

// [...]
		const module = await Test.createTestingModule({
			imports: [DatabaseModule],
		})
		.overrideModule(dynamicDatabaseCacheModule)
			.useModule(
				CacheModule.register({
					ttl: 100,
				}),
			)
                .compile();

// [...]

@nestjs nestjs locked and limited conversation to collaborators Jul 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests