Skip to content

Providers mocked via MockModule are persisted across tests #186

Closed
@JoshuaEN

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
});

Full Example

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions