Closed crazycabo closed 1 year ago
Vite has some changes from Jest in its setup and teardown methods. I simply had to follow them, and the process works.
Ah, thanks!
If you're are able to share the lessons for anyone else that might be interested?
I bet it's because vitest expects an Awaitable<void>
. Could you try changing the setup and teardown functions to:
beforeAll(async () => { await provider.setup() } ); // note the braces
If this is indeed the problem, I kind of think it's a bug in vitest because they say they're Jest compatible.
I raised a PR with vitest that will fix this: https://github.com/vitest-dev/vitest/pull/3147
Vite has some changes from Jest in its setup and teardown methods. I simply had to follow them, and the process works.
Can you please share your findings? I tried to change setup and teardown functions to use asynchronous functions but didn't work for me
Does this not work: https://github.com/pact-foundation/pact-js/issues/965#issuecomment-1348185596 ?
Does this not work: #965 (comment) ?
No, not working
Perhaps you can share the error you're having or the code you have written?
Perhaps you can share the error you're having or the code you have written?
To give you more context, I was using Pact with Jest and I'm switching to Vitest. But seems the test's feeling and I believe it's an issue with how Vitest handles teardown hooks.
In Jest I was using it like so:
describe('...', () => {
const provider = new Pact({
...commonPactConfig,
provider: 'provider-name',
});
beforeAll(() => provider.setup());
afterAll(() => provider.finalize());
afterEach(() => provider.verify());
it('should ...', async () => {
const email = 'john.doe@hse.de';
const interaction = new Interaction()
.given('...')
.uponReceiving('...')
.withRequest({
method: 'POST',
path: '/path',
body: {
...
},
headers: {
'Content-Type': 'application/json',
},
})
.willRespondWith({
status: 200,
});
await provider.addInteraction(interaction);
const response = await serviceApi(email);
expect(response.status).toBe(200);
});
});
Console logs:
Pact verification failed!
Test failed for the following reasons:
Mock server failed with the following mismatches:
0) The following request was expected but not received:
Method: POST
Error: Pact verification failed - expected interactions did not match actual.
See the linked issue on Vite above - you will need to change your methods to return a void promise instead of an unknown promise.
I fixed this in a PR for them but it has gone into limbo.
Oh wait, looking at your test, I think the problem is in the part you omitted. Can you share your whole test please
@TimothyJones I updated the previous comment.
I solved my problem by moving from Pact
to PactV3
and using --threads=false
option when running the tests.
Command:
vitest run --threads=false -c vite.pact.config.ts --mode=pact
Pact example:
describe('...', () => {
const provider = new PactV3({
...commonPactConfig,
provider: 'provider-service',
});
describe('...', () => {
it('..., () => {
provider
.given('...')
.uponReceiving('...')
.withRequest({
method: 'POST',
path: `/path`,
body: ...,
})
.willRespondWith({
status: 200,
body: ...,
});
return provider.executeTest(async () => {
const response = await API.createSomethingAwesome();
expect(response).toBeDefined();
});
});
});
});
I've just tested this out with some simple consumer examples using PactV2 and PactV3 interfaces, with vitest and got them passing successfully and generating pact files
I think this was to do with https://github.com/vitest-dev/vitest/issues/3146
Thanks @TimothyJones, I was reading through that thread and noted the PR had been closed, as there is some confusion between striving to be a drop-in replacement and actually being a drop-in replacement.
I'm not sure if anything has changed in vitest to support this working now. I didn't try this at the time, so thought I would whip something quickly together.
That came about as in a separate project, there was a suggestion to switch from jest -> vitest, so I wanted to check if it would work, which reminded me of this topic.
Software versions
Issue Checklist
Please confirm the following:
Expected behaviour
Teardown process should not cause errors.
Actual behaviour
After Pact files are created, an unknown error occurs during teardown process. It's difficult to debug since no output points to a possible cause. I have not found any documentation of Pact using Vite, either.
Tests do execute and Pact files are created but, for some reason a non-zero exit code causes issues.
Steps to reproduce
Execute at least one Pact test with Vite via vitest command.
Relevant log files