Hi all. Has anyone been able to get provider tests...
# pact-js
c
Hi all. Has anyone been able to get provider tests (as a unit test, with dependencies/upstream services mocked) working with NextJS + typescript + vitest or jest?
m
I don’t know NestJS very well, but I can’t see how vitest/jest would impact the ability to do that. What problem are you having?
c
Hi Matt. Using vitest, mocks or spies not taking effect when we progrmatically start the server for a provider test. Before you say "check vitest": 1. I've already tried / searched, the closest thin I can find is this but the NextJS custom server example doesn't touch testing, and the NextJS vitest example doesn't deal with mocks or custom servers 2. I can get vitest's mocks & spies to work in the context of other unit tests (e.g. mocking fetch when testing a function directly) I've also tried with jest but the result was worse (the custom server refused to start).
m
Is there a way you can use other forms of DI to replace components during testing?
c
I also don't know NextJS very well and I've been told no but if you know of a way I would be keen to try it.
👍 1
m
hmm that doesn’t sound right. The ability to swap in/out dependencies should be something you can do independent of the framework you’re using.
💯 1
i.e. passing in
client
is a way to do “DI” to this function:
Copy code
someFunctionThatNeedsToCallAPI(client: APIClientInterface) => whatever
NextJS shouldn’t prevent this sort of thing. Perhaps the code base isn’t set in a way like this (modular so that things can be tested etc. independently of one another), which is a code smell in its own right IMO.
I’d be keen for other’s views on NextJS strategies here. If you had any way of sharing your code (or perhaps an example project that illuminates the same problem that we can hash on together) that might be a way to help you
c
let me see if I can put something together
thankyou 1