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

Jest Example errors related to async setup and teardown #385

Closed
yosevu opened this issue Oct 28, 2019 · 4 comments
Closed

Jest Example errors related to async setup and teardown #385

yosevu opened this issue Oct 28, 2019 · 4 comments

Comments

@yosevu
Copy link
Contributor

yosevu commented Oct 28, 2019

Software versions

  • OS: e.g. Mac OSX 10.14.6
  • Library version: e.g. 9.0.6
  • Node Version: 10.4.1

Expected behaviour

There are no errors or warnings in the test output.

Actual behaviour

There are errors and warnings in the test output related to handling async code.

Steps to reproduce

There are errors using the example test setup code from https://github.com/pact-foundation/pact-js/blob/master/examples/jest/pactTestWrapper.js

Snippet 1

beforeAll(done => {
  provider.setup().then(() => done())
})

afterAll(done => {
  provider.finalize().then(() => done())
})

Error

console.error node_modules/jest-jasmine2/build/jasmine/Env.js:289
      Unhandled error
    console.error node_modules/jest-jasmine2/build/jasmine/Env.js:290
      undefined

Fix

beforeAll(done => {
    provider.setup().then(() => done());
  });

  afterAll(done => {
    provider.finalize().then(() => done(), err => done.fail(err));
  });

Snippet 2

beforeAll(done => {
    provider.setup().then(() => done());
  });

  afterAll(done => {
    provider.finalize().then(() => {
      provider.removeInteractions();
      done();
    }, err => done.fail(err));
  });

Warning

(node:33088) UnhandledPromiseRejectionWarning: PopsicleError: Unable to connect to "http://127.0.0.1:3083/interactions"
(node:33088) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)
(node:33088) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Could I update the Jest Examples to include promise examples like this:

beforeAll(() => provider.setup());

  afterAll(() =>
    provider
      .finalize()
      .then(provider.removeInteractions())
      .catch(console.error));
@mefellows
Copy link
Member

mefellows commented Oct 28, 2019

Thanks @yosevukilonzo. I don't see anywhere where we use provider.removeInteractions(); in our Jest examples - could you please point me to them? This is definitely not necessary if finalize is called first.

As for the wrapper code:

beforeAll(done => {
  provider.setup().then(() => done())
})

afterAll(done => {
  provider.finalize().then(() => done())
})

Yes, I believe a PR to replace it with the following should work just fine, thanks for the offer:

beforeAll(() => provider.setup())
afterAll(() => provider.finalize())

@TimothyJones
Copy link
Contributor

Thanks for spotting this, @yosevukilonzo

I'll wait for your PR so that you get proper credit in the commit history.

You might also be interested in https://github.com/YOU54F/jest-pact , which we've been working on to make setting up Pact tests with Jest less fiddly.

@yosevu
Copy link
Contributor Author

yosevu commented Oct 29, 2019

I don't know either now @mefellows, I must have seen it in an example somewhere else 🤷‍♂. Good to know that it is unnecessary with finalize. Is removeInteractions intended to ever be used directly? I don't see it documented in the README. I'll create a PR to update the example!

Cool @TimothyJones, I wasn't aware of jest-pact!

@TimothyJones
Copy link
Contributor

We fixed this by accident when we migrated the jest example to use jest-pact a while back (which is neat, as avoiding this mistake is kind of the point of jest-pact).

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

3 participants