Providers mocked via MockModule are persisted across tests #186
Closed
Description
Hello,
Providers that are part of a Module, when mocked via MockModule
, appear to use the same instance across all tests.
For example:
beforeEach(() => {
TestBed.configureTestingModule({
imports: [MockModule(ExampleModule)],
});
exampleProvider = TestBed.inject(ExampleProvider);
});
it('should not be able to pass state between tests (setup phase)', () => {
// Set a property on the class to see if it carries over to the next test.
exampleProvider.a = 11;
});
it('should not be able to pass state between tests (validation phase)', () => {
expect(exampleProvider.a).toBeUndefined(); // Fails
});
I would think the expected behavior is that each test would get a new mocked instance of the provider so that one test could not accidentally impact another test (in this way).
Thank you for your time.
P.S. This also applies to when MockProvider
is used directly, though I understand that to be an internal API.
As a note, this cross-test instance sharing not appear to happen when the the module and provider caches are disabled via ngMocksUniverse.flags
, e.g.:
beforeAll(() => {
ngMocksUniverse.flags.delete('cacheModule');
ngMocksUniverse.flags.delete('cacheProvider');
});
Metadata
Assignees
Labels
No labels