pact-foundation / pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems.
https://pact.io
Other
1.61k stars 344 forks source link

ReferenceError: setImmediate is not defined #1007

Closed qmg-akara closed 1 year ago

qmg-akara commented 1 year ago

Hi guys, thanks for the hard work.

We recently upgraded to v10.3.1 from 10.2.2 in a React project and started getting this error: ReferenceError: setImmediate is not defined

Software versions

Please provide at least OS and version of pact-js

Issue Checklist

Please confirm the following:

Expected behaviour

No error

Actual behaviour

Getting the following error:

Steps to reproduce

Our code is similar to the following code:

describe('provider tests', () => {
  const aMessage = {};

  const pact = new MessageProviderPact({
    messageProviders: {
      'a message': async () => aMessage,
    },
    provider: 'provider-messaging',
    providerVersion: '1.0.0',
    pactBrokerUrl: 'https://pact-broker/',
    publishVerificationResult: true,
    enablePending: true,
    consumerVersionSelectors: [{ latest: true }],
  });

  // eslint-disable-next-line jest/expect-expect
  it('provides all messages expected by consumers', async () => {
    await pact.verify();
  });
});
mefellows commented 1 year ago

Thanks for the kind words!

I don't believe we use that function anywhere in the code, so my guess is:

  1. It's related to a dependency (or transitive dependency)
  2. It relates to upgrading to the latest @types/node library (seems unlikely)

That example given above clearly won't cut it as a repro, because we run provider verification for numerous scenarios across multiple node versions, and I've never seen the error before. It's possible it's some combination of libraries, configuration within your React project (they are notorious for needing other transpilers etc.) or even just the type definitions that's causing this issue.

Could you please provide a minimal repo we can pull down to test with?

TimothyJones commented 1 year ago

This isn't related to your issue, but I noticed the eslint-ignore block - I think you can improve this by putting the following rule in your lint settings:

"jest/expect-expect": ["error", { "assertFunctionNames": ["expect","pact.verify"]}

@mefellows there probably should be a Pact set of eslint rules. Maybe I'll take a look at that over the xmas break.

TimothyJones commented 1 year ago

Google suggests this might be due to a problem in some versions of Jest. Is it possible that your jest version was also changed when you updated Pact? This can happen if you need to regenerate the package lockfile (or aren't committing it).

Other questions: What testEnvironment setting are you using? Where is the error thrown? Can you provide the full log?

qmg-akara commented 1 year ago

Hey, thanks for the quick reply!

It was a renovate dependency upgrade PR. I reverted upgrades one by one to find the issue but it didn't work. So I had to revert all yarn.lock changes too. And it worked! I started adding back the upgrades for that packages from top to the bottom. All went well until I upgraded pact-js, then started getting the same error. So I reverted that commit and upgraded the rest of the list and all passed successfully.

List of dependencies in renovate PR:

image
ReferenceError: setImmediate is not defined

      80 |   // eslint-disable-next-line jest/expect-expect
      81 |   it('provides all messages expected by consumers', async () => {
    > 82 |     await pact.verify();
         |                ^
      83 |   });
      84 | });
      85 |
TimothyJones commented 1 year ago

Thanks! Do you have a stack trace too? It should be below that bit in the log.

I suspect that will point to the error, which probably isn't inside Pact, but might be in Jest or node.

Are there any other errors, or peer dependency warnings on a clean install?

qmg-akara commented 1 year ago

This was logged during yarn install: ➤ YN0060: │ project@workspace:. provides jest (<hash>) with version 29.3.1, which doesn't satisfy what jest-pact requests

yarn explain peer-requirements <hash> output:

➤ YN0000: project@workspace:. provides jest@npm:29.3.1 [34090] with version 29.3.1, which doesn't satisfy the following requirements:

➤ YN0000: jest-pact@npm:0.10.1 [34090] → ^24.0.0 || ^25.0.0 || ^26.0.0 || ^27.0.0 || ^28.0.0 ✘

Looks like jest-pact doesn't support jest version 29.

Closing the issue it's not related to this repo. Thanks for the replies!

TimothyJones commented 1 year ago

Hmm. I don't think that's related, though (but jest-pact should be updated to support 29 - there's nothing in the changelog that would be a problem for it)

qmg-akara commented 1 year ago

Yeah, you're right, it may not be related. Here is the full error message if that helps:

ReferenceError: setImmediate is not defined

      80 |   // eslint-disable-next-line jest/expect-expect
      81 |   it('provides all messages expected by consumers', async () => {
    > 82 |     await pact.verify();
         |                ^
      83 |   });
      84 | });
      85 |

      at ThreadStream.write (node_modules/thread-stream/index.js:261:7)
      at Pino.write (node_modules/pino/lib/proto.js:210:10)
      at Pino.LOG [as info] (node_modules/pino/lib/tools.js:56:21)
      at Object.info (node_modules/@pact-foundation/pact-core/src/logger/index.ts:35:12)
      at Object.info (node_modules/@pact-foundation/src/common/logger.ts:13:43)
      at MessageProviderPact.Object.<anonymous>.MessageProviderPact.verify (node_modules/@pact-foundation/src/messageProviderPact.ts:78:12)
      at Object.verify (test/Contracts/Provider/ContractVerification.test.ts:82:16)

We have pino-pretty version 6.0.0. Latest version doesn't work with pact-js v10.

Other than that I can't see any other related logs.

TimothyJones commented 1 year ago

What is the setting for testEnvironment? It must be node for Pact tests

What version of pact-core do you have? Could you try updating to at least 13.12.1?

YOU54F commented 1 year ago

Hey @qmg-akara , I saw this today and someone had dropped a PR to update jest-pact to support a peer dep of Jest v29, I merged and released it this afternoon

mefellows commented 1 year ago

Thanks all for the feedback/comments. It sounds like there is still a little more investigation to be done though?

If you do find more evidence of changes required in the Pact ecosystem, please report here and we can re-open to track.

qmg-akara commented 1 year ago

It turns out we need to change the testEnvironment to node for pact tests. I added this to pact files and it worked:

/**
 * @jest-environment node
 */

Thanks for the help!

TimothyJones commented 1 year ago

Yes, indeed. This is in the troubleshooting part of the documentation - but perhaps it would be more discoverable if the "usage with X" sections were extracted to their own pages?