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

Update pact javascript examples #117

Open
TimothyJones opened this issue Sep 27, 2021 · 0 comments
Open

Update pact javascript examples #117

TimothyJones opened this issue Sep 27, 2021 · 0 comments

Comments

@TimothyJones
Copy link

Hi there,

I'm one of the pact maintainers, and we occasionally get people asking questions about the examples you have here, which (while instructive) have one or two bugs in the way that jest is used. Is it possible to update the examples to address these issues?

For instance, in the react example, this code doesn't correctly fail if the heroService throws an error, because the catch with done.fail isn't done at the end of the promise chain. This means the test doesn't trigger the appropriate failure in every error case. To address this, you could either move the done.fail part to the end of the promise chain, or return the promise to Jest instead of using done.

        it('sends a request according to contract', (done) => {
            heroService.createHero(new Hero('Superman', 'flying', 'DC'))
                .then(hero => {
                    expect(hero.id).toEqual(42);
                })
                .then(() => {
                    global.provider.verify()
                        .then(() => done(), error => {
                            done.fail(error)
                        })
                });
        });

Another issue is that the pact server is started on every test in jest-wrapper.js. This isn't a bug, but means that the log is quite noisy, which makes it harder to debug. For example, if the pact server starts correctly in a test that doesn't need it, but fails in a test that does, then this is not obvious from the current log. It's probably clearer to only start the server in pact tests.

You can get some of these improvements for free with jest-pact, which was introduced since this example was written, and substantially reduces the need for boilerplate in a jest test. See https://github.com/pact-foundation/jest-pact

You can also entirely avoid the need for publish.js by using the pact-broker CLI directly in your npm script - see: https://github.com/pact-foundation/pact-js#publish-in-npm-scripts

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

1 participant