pollyjs icon indicating copy to clipboard operation
pollyjs copied to clipboard

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

Open theomelo opened this issue 2 years ago • 6 comments

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)

$ ./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

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

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

{
    "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

theomelo avatar Oct 28 '22 00:10 theomelo

this is happening to me as well

codemariner avatar May 10 '23 17:05 codemariner

Try directly passing adapters like this:

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

tabrez96 avatar Jun 20 '23 08:06 tabrez96

This is happening to me as well. I can't seem to record on code sandbox using fs and local-storage isn't persisting in my headers @offirgolan do you have any guidance? My polly config is super simple and it doesn't seem to be working?

import { Polly } from "@pollyjs/core";
import LocalStoragePersister from "@pollyjs/persister-local-storage";
import NodeHttpAdapter from "@pollyjs/adapter-node-http";
import FSPersister from "@pollyjs/persister-fs";
import FetchAdapter from "@pollyjs/adapter-fetch";

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

const config = {
  adapters: ["fetch"],
  persister: "fs",
  logLevel: "info",
  recordFailedRequests: true,
  recordIfMissing: true,
  recordFailedRequests: true,
  mode: "replay",
  persisterOptions: {
    fs: "./recording",
  },
};

export function pollyConfig(name) {
  const polly = new Polly(name, config);
  polly.server
    .any()
    .on("response", (req) =>
      console.log(JSON.stringify({ result: req }, null, 2)),
    );
  return polly;
}

Josiassejod1 avatar Dec 10 '23 23:12 Josiassejod1

@theomelo have you had any luck?

Josiassejod1 avatar Dec 10 '23 23:12 Josiassejod1

@Josiassejod1, unfortunately, no. It's worth noting that I haven't been working with that stack for a while. Since there are other people with this issue, I will keep it open for now.

theomelo avatar Feb 01 '24 00:02 theomelo

Add another instance of the same issue.

Resorted to using:

import NodeHttpAdapter from "@pollyjs/adapter-node-http";
import FSPersister from "@pollyjs/persister-fs";
const config = {
  adapters: [NodeHttpAdapter ],
  persister: FSPersister ,
  ..,restOfOptions 
};

Nick-Minutello avatar Jun 26 '24 09:06 Nick-Minutello