Hi folks, I want to write some pact tests using th...
# pact-js
d
Hi folks, I want to write some pact tests using the pactWith method of the ‘jest-pact’ package. My use case is a bit weird: I have multiple providers with the same API for some reason. So instead of duplicating the whole test file, I’m trying to do something like:
Copy code
it.each(['provider 1', 'provider 2'])('testing %s', provider => pactWith({
            consumer: 'foobar',
            provider: provider,
            port: 45678
        }, () ..... ));
But it fails because I think the Pact mock server is not shut down between each test and then I get:
Port '45678' is already in use by another process.
Any idea ?
👋 1
y
I wouldn't be passing a port in anyway, jest-pact will automatically assign a random port for you. If you must set one up before, you could use it as a param in your it.each setup and pass it as a param
d
I agree. But if I do that, I’ll have to pass the port down to the http client. Now, I run Jest with
testURL: <http://localhost:45678>
This is because I don’t specify the base URL is my HTTP Client. All URLs in my HTTP Client are like:
/foo/bar/rest/api/v1/todos/
y
Hmm. Traditionally you would use the provider.mockService.baseUrl which will contain the port as pass that into your api clients constructor, or as a param if not using classes. Do you run your current suite in band to avoid parallel race conditions by reusing the same port?
d
Yes I run them in band.
y
how about something like this
Copy code
it.each([['provider 1',45678], ['provider 2',45679],)('testing %s', (provider,port) => pactWith({
            consumer: 'foobar',
            provider,
            port
        }, () ..... ));
d
With this solution, I still have to inject anyway the port in the service itself instead of using the testURL property of jest
I think the cleanest way to do it, is to let pact decide the port itself and inject the baseUrl in the service.
👌 2
y
agreed
d
Thanks a lot for the help 🙂
🙌 1
y
my pleasure Dany! let us know how you get on! I'd be up for adding an example into https://github.com/YOU54F/template-jest-pact-typescript 🙂
d
I’m using Angular. Maybe I’ll write something about it later 🙂
👌 2