Netflix / pollyjs

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

Not able to intercept request and response with node-http #494

Open sambhav-jain-lilly opened 6 months ago

sambhav-jain-lilly commented 6 months ago

`import { Polly } from '@pollyjs/core'; import { expect } from 'chai'; // Chai assertion library import FetchAdapter from '@pollyjs/adapter-fetch'; // Fetch adapter for Polly import FSPersister from '@pollyjs/persister-fs'; import NodeHttpAdapter from '@pollyjs/adapter-node-http'; import XHRAdapter from '@pollyjs/adapter-xhr';

// Register adapters and persisters with Polly.js

Polly.register(FetchAdapter); // Register the Fetch adapter Polly.register(FSPersister); Polly.register(NodeHttpAdapter); Polly.register(XHRAdapter);

// Describe a test suite using a testing framework (e.g., Mocha or Jasmine) describe('Simple Example', function () {

// Write a test case within the suite using the `it` function
it('fetches a post 1', async function () {

    // Create a new Polly instance named 'Simple Example'
    const polly = new Polly('Simple Example', {
        adapters: ['node-http'],
        persister: 'fs',
        persisterOptions: {
            fs: {
                recordingsDir: 'recordings'
            }
        },
        mode: 'replay', // Set the mode to 'replay' to use existing recordings
        logLevel: 'info', // Log requests to the console with 'info' level
        recordIfMissing: true
    });

    polly.server.get('https://jsonplaceholder.typicode.com/posts/8').intercept((req, res) => {

            console.log('Request intercepted:', req);
            console.log('Response intercepted:', res);
    });

// // Stop the Polly instance, which persists recorded requests and disconnects from adapters await polly.stop(); }); });`

I am using this code print request and responses for the above url. My code runs , and it says it is successful but it dosen't show any logs what I am trying to print. I am using mocha for running test. And chai is for assertion. I am not sure whether it is actually intercepting call or not. Can anyone help me with that. Thanks!

Nick-Minutello commented 2 months ago

Have a look at the documentation for test framework integration. We had a bit of difficulty getting things working because the documentation & behaviour didn't match.

https://netflix.github.io/pollyjs/#/examples?id=jest-node-fetch https://netflix.github.io/pollyjs/#/examples?id=typescript-jest-node-fetch https://netflix.github.io/pollyjs/#/test-frameworks/jest-jasmine