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

Vitest mock issue when running tests #44

Open
Faboslav opened this issue Nov 4, 2024 · 4 comments
Open

Vitest mock issue when running tests #44

Faboslav opened this issue Nov 4, 2024 · 4 comments
Assignees

Comments

@Faboslav
Copy link

Faboslav commented Nov 4, 2024

Hello there,

i migrated the project from tsyringe, and it works perfectly fine.
When running vitest following occurs when i try to mock a dependency, which is normally injected in the constructor:

TypeError: Cannot read properties of undefined (reading 'provide')
 ❯ Container.bind node_modules/@needle-di/core/src/container.ts:92:73

Screenshot 2024-11-04 at 6 50 19

Mentioned part of the code in test:

  const mockScenarioService = mockDeep<ScenarioService>();
  const mockSessionScenarioProgressProvider = mockDeep<SessionScenarioProgressProvider>();
  let sessionStepGuard: SessionStepGuard;

  beforeEach(() => {
    mockReset(mockScenarioService);
    mockReset(mockSessionScenarioProgressProvider);
    sessionStepGuard = new SessionStepGuard(mockScenarioService, mockSessionScenarioProgressProvider);
  });

Can this be due to some invalid or missing setting, like target of ES2022 or something like that?

@Faboslav
Copy link
Author

Faboslav commented Nov 4, 2024

Okay, so i found out, this happens, when test uses class which uses any kind of InjectionToken inside it in its injects, for example:

export const SCENARIO_REPOSITORY = new InjectionToken<ScenarioRepository>('SCENARIO_REPOSITORY');
  public constructor(
    private readonly scenarioRepository: ScenarioRepository = inject(SCENARIO_REPOSITORY),
  ) {}

which works in the app, but in the test its problematic, but i dont know why.

Trying string instead of token works for me:

private readonly scenarioRepository: ScenarioRepository = inject('ScenarioRepository'),

@dirkluijk
Copy link
Collaborator

dirkluijk commented Nov 4, 2024

Thanks for reporting this issue. Let's see if we can reproduce it.
From your first comment, I'd say it's because SessionStepGuard is undefined somehow, but I am still figuring out why.

Can you clone this StackBlitz and try to reproduce it in there? Alternatively, you can share me a reproduction in a Github repository.

@dirkluijk dirkluijk self-assigned this Nov 4, 2024
@Faboslav
Copy link
Author

Faboslav commented Nov 7, 2024

Sorry, i was little bit busy, i will try to do some more debugging later :).

@dirkluijk
Copy link
Collaborator

Let me know, I am motivated to help you out here. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants