#469·pollyjs

[Question] ExpressJS + Typescript + Jest: Not able to record tests.

Author: theomeloCreated Oct 28, 2022Updated Jun 26, 2024

Description

I clearly don't know how to configure test recording.

The problem

I'm not able to record my tests.

What I tried to do

  1. Set up a simple app using ExpressJS + Typescript + Jest.

  • When running the tests, they pass without error but don't create the record anywhere.
  1. Running your Typescript Node example locally.
  • Without changing anything, it fails. It uses setup-polly-jest to configure Polly.js, so I tried replicating the example without success.

  • The error bellow happens when running my app (which you should also see in the sandbox)

bash
$ ./node_modules/.bin/jest --runInBand
 FAIL  src/__tests__/server.test.ts
  ● 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/src/polly.js:288:5)
      at forEach (node_modules/@pollyjs/core/src/polly.js:172:52)
          at Array.forEach (<anonymous>)
      at Polly.configure (node_modules/@pollyjs/core/src/polly.js:172:26)
      at new Polly (node_modules/@pollyjs/core/src/polly.js:47:10)
      at PollyEnvironmentNode.handleTestEvent (node_modules/setup-polly-jest/lib/jest-environment-polly.js:90:48)
      
Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.08 s
Ran all test suites.

The rest of this issue doesn't consider setup-polly-jest since it's unrelated to my problem with Polly.js


Shareable Source

javascript
import fetch from 'node-fetch';
import polly from '../__tests__/__config__/polly';

describe('It works', () => {
  const { server } = polly;

  beforeEach(() => {
    server
      .get('https://www.google.com')
      .intercept((_, res) => void res.json({ hello: 'world' }) );
  });

  it('Returns ', async () => {
    const response = await fetch('https://www.google.com');
    const data = await response.json();
    expect(JSON.stringify(data)).toBe("{\"hello\":\"world\"}");
  });
});

Config

javascript
import { Polly } from "@pollyjs/core";
import NodeHttpAdapter from '@pollyjs/adapter-node-http';
import FSPersister from '@pollyjs/persister-fs';
import path from "path";

Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);

const polly = new Polly(
  'testing',
  {
    adapters: ['node-http'],
    mode: 'replay',
    persister: 'fs',
    persisterOptions: {
      fs: {
        recordingsDir: path.resolve(__dirname, '../__recordings__')
      }
    },
    recordIfMissing: true
  }
);

export default polly;

Dependencies

json
{
    "express": "^4.16.1",
    "@pollyjs/adapter-node-http": "^6.0.5",
    "@pollyjs/core": "^6.0.5",
    "@pollyjs/persister-fs": "^6.0.5",
    "jest": "^29.2.2",
    "node-fetch": "2",
    "ts-jest": "^29.0.3",
    "ts-node": "^10.9.1",
    "typescript": "^4.8.4"
}

Environment

Node.js v16.18.0 linux 5.15.49-linuxkit npm 8.19.2 yarn 1.22.19 jest 29.2.2