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

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

Open
theomelo opened this issue Oct 28, 2022 · 5 comments
Open

Comments

@theomelo
Copy link

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

@codemariner
Copy link

this is happening to me as well

@tabrez96
Copy link

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
  }
);

@Josiassejod1
Copy link

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
Copy link

@theomelo have you had any luck?

@theomelo
Copy link
Author

theomelo commented Feb 1, 2024

@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.

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

4 participants