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

How to register adapters with jest-circus #23

Open
stephenh opened this issue Jul 31, 2021 · 2 comments
Open

How to register adapters with jest-circus #23

stephenh opened this issue Jul 31, 2021 · 2 comments

Comments

@stephenh
Copy link

stephenh commented Jul 31, 2021

Hey; we've been using setup-polly-jest for awhile and it's working great, so thanks for the project!

We're trying to upgrade to jest 27 / jest-circus, and for some reason our adapters are no longer being registered, i.e. we have code like:

Polly.register(require("@pollyjs/adapter-node-http"));
Polly.register(require("@pollyjs/persister-fs"));

But our tests are now failing with:

    PollyError: [Polly] Adapter matching the name `node-http` was not registered.

I've put the new jest env at the top of our test files:

/** @jest-environment setup-polly-jest/jest-environment-node */
...
import "src/setupPolly";
...
describe("...", () => {

Where the src/setupPolly file is what has our common Polly.register code in it.

By adding some console.logs inside of polly itself, it looks like there are two versions of Polly's internal event emitter floating around, i.e. from a test run:

CREATED EventEmitter {
  madeAt: 1627748370545,
  [Symbol()]: Map { _c: Map(0) {} },
  [Symbol()]: Set { _c: Set(3) { 'register', 'create', 'stop' } }
}
  console.log
    CREATED EventEmitter {
      madeAt: 1627748375580,
      [Symbol()]: Map { _c: Map(0) {} },
      [Symbol()]: Set { _c: Set(3) { 'register', 'create', 'stop' } }
    }

      at Object.<anonymous> (node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6195:9)

  console.log
    REGISTERING adapters

      at Object.<anonymous> (src/setupPolly.ts:10:9)

  console.log
    REGISTERING ON1 EventEmitter {
      madeAt: 1627748375580,
      [Symbol()]: Map { _c: Map(0) {} },
      [Symbol()]: Set { _c: Set(3) { 'register', 'create', 'stop' } }
    }

      at Function.on (node_modules/@pollyjs/core/src/polly.js:113:12)

  console.log
    REGISTERING ON1 EventEmitter {
      madeAt: 1627748375580,
      [Symbol()]: Map { _c: Map(1) { 'register' => [Set] } },
      [Symbol()]: Set { _c: Set(3) { 'register', 'create', 'stop' } }
    }

      at Function.on (node_modules/@pollyjs/core/src/polly.js:113:12)

EMITTING ON1 EventEmitter {
  madeAt: 1627748370545,
  [Symbol()]: Map { _c: Map(0) {} },
  [Symbol()]: Set { _c: Set(3) { 'register', 'create', 'stop' } }
}
INSIDE emitSync
INSIDE emitSync

  ● Test suite failed to run

    PollyError: [Polly] Adapter matching the name `node-http` was not registered.

      at Object.assert (node_modules/@pollyjs/utils/src/utils/assert.js:5:11)
      at Polly.connectTo (node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6433:11)
      at node_modules/@pollyjs/core/src/polly.js:170:20
          at Array.forEach (<anonymous>)
      at Polly.configure (node_modules/@pollyjs/core/src/polly.js:170:20)
      at new Polly (node_modules/@pollyjs/core/dist/cjs/pollyjs-core.js:6221:10)
      at PollyEnvironmentNode.handleTestEvent (node_modules/setup-polly-jest/lib/jest-environment-polly.js:85:48)

What seems to be happening is:

  • The 1627748370545 event emitter is created immediately when the test starts (I assume from the jest-environment-node)
  • However a second 1627748375580 event emitter is also created (I assume this is due to Jest's sandboxing, like b/c our src/setupPolly.ts is imported from within our test, it is getting it's own separate require/vm context)
  • Our Polly.register(node-adapter) call is putting the adapter into the 2nd / 1627748375580 emitter
    • You can see the 2nd event emitter's map is empty, but then starts to get populated i.e. 'register' => [Set]
  • But when handleTestEvent runs and tries to create a polly instance for the test, the Polly.configure is using the first event emitter, 1627748370545 to look for adapters, and not finding any

So, that is what happening. I really don't know why though. :-)

Is there a different way to Polly.register in the new jest-circus setup?

I've looked through the readme and the commit that added jest-circus support and didn't see anything obvious, but maybe I'm missing something.

Thanks!

@gribnoysup
Copy link
Owner

Hey @stephenh, thanks for reporting the issue! I can confirm that it exists and I think that you probably have the right idea about the root cause. I don't have time to immediately look into it, but will try to see if anything can be done to make global Polly.register calls work with the new jest runtime, I never used this way of registering adapters with polly, so when adding circus support I kinda missed that it's not working anymore. In the meantime as a workaround you can use the approach of directly passing adapters in the config as it is done in this tests in this repo:

const context = setupPolly({
adapters: [require('@pollyjs/adapter-node-http')],
persister: require('@pollyjs/persister-fs'),
persisterOptions: {
fs: {
recordingsDir: path.resolve(__dirname, '__recordings__')
}
}
});

@stephenh
Copy link
Author

stephenh commented Aug 1, 2021

Ah great! We can definitely do that, and I confirmed that works for us as well.

Thanks for the quick reply!

gribnoysup added a commit that referenced this issue Aug 1, 2021
gribnoysup added a commit that referenced this issue Aug 1, 2021
gribnoysup added a commit that referenced this issue Aug 2, 2021
- Do not mix \`async\` and \`done\` usage in jasmine env
- Stop polly instance on \`test_skip\`
- Add a test case for usage of globabl \`Polly.register\`
  (this currently fails on jest-circus)
gribnoysup added a commit that referenced this issue Aug 2, 2021
- Do not mix `async` and `done` usage in jasmine env
- Stop polly instance on `test_skip`
- Add a test case for usage of global `Polly.register`
  (this currently fails on jest-circus)
gribnoysup added a commit that referenced this issue Aug 2, 2021
- Do not mix `async` and `done` usage in jasmine env
- Stop polly instance on `test_skip`
- Add a test case for usage of global `Polly.register`
  (this currently fails on jest-circus)
- Update CI config to split test running in a better way
gribnoysup added a commit that referenced this issue Aug 2, 2021
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

2 participants