Netflix / pollyjs

Record, Replay, and Stub HTTP Interactions.
https://netflix.github.io/pollyjs
Apache License 2.0
10.22k stars 352 forks source link

Node's native fetch support #488

Open sparrovv opened 1 year ago

sparrovv commented 1 year ago

Description

This might be a known bug, but I couldn't find any reference.

I want to use node native fetch to make requests, but when configuring polly with fetch-adapter, it doesn't seem to work.

Is there any workaround for that?

Shareable Source

import { Polly } from '@pollyjs/core'
import FetchAdapter from '@pollyjs/adapter-fetch'
import FSPersister from '@pollyjs/persister-fs'

Polly.register(FetchAdapter)
Polly.register(FSPersister)

describe('Native Fetch', () => {
  it('should work', async () => {
    const polly = new Polly('test', {
      adapters: ['fetch'],
      persister: 'fs',
      logLevel: 'debug',
      recordIfMissing: true,
    })

    const response = fetch('https://jsonplaceholder.typicode.com/todos/1')

    expect(response).toBeDefined()
  })
})

Error Message & Stack Trace

    TypeError: Cannot assign to read only property 'Request' of object '[object global]'

       9 | describe('Native Fetch', () => {
      10 |   it('should work', async () => {
    > 11 |     const polly = new Polly('test', {
         |                   ^
      12 |       adapters: ['fetch'],
      13 |       persister: 'fs',
      14 |       logLevel: 'debug',

      at FetchAdapter.onConnect (../../node_modules/@pollyjs/adapter-fetch/src/index.js:53:5)
      at FetchAdapter.onConnect [as connect] (../../node_modules/@pollyjs/adapter/src/index.js:48:12)
      at Polly.connect [as connectTo] (../../node_modules/@pollyjs/core/src/polly.js:297:13)
      at connectTo (../../node_modules/@pollyjs/core/src/polly.js:172:52)
          at Array.forEach (<anonymous>)
      at Polly.forEach [as configure] (../../node_modules/@pollyjs/core/src/polly.js:172:26)
      at new configure (../../node_modules/@pollyjs/core/src/polly.js:47:10)

Dependencies

{
    "@pollyjs/adapter-fetch": "^6.0.6",
    "@pollyjs/core": "^6.0.6",
    "@pollyjs/persister-fs": "^6.0.6",
}

Environment

Node.js v20.3.1
darwin 22.1.0
9.6.7
3.5.1
Josiassejod1 commented 11 months ago

try this example and see if it works?

import { fetchQuote, sendMessage } from "../../javascript/api";
import { pollyConfig } from "../polly-config";

test("fetchQuote should fetch a random quote", async () => {
  const polly = await pollyConfig("FetchQuotes");
  const data = await fetchQuote();
  expect(data).toBeDefined();
  await polly.stop();
});

test("send text message", async () => {
  const polly = await pollyConfig("SendMessage");
  const data = await sendMessage("INSERT-TEST-NUMBER", "Hello!");
  expect(data).toBeDefined();
  await polly.stop();
});
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: "__recordings__",
  },
};

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;
}
Aiosa commented 9 months ago

For me using adapter-fetch and node 20.11.0 works, but I get this message: [Polly] [adapter:fetch] Using the fetch adapter in Node has been deprecated. Please use the node-http adapter instead. Nice. Library deprecation message on a 'new' node feature. Btw node-http adapter does not work at all, obviously. All latest versions.