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

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

Open
sambhav-jain-lilly opened this issue Feb 27, 2024 · 1 comment
Open

Comments

@sambhav-jain-lilly
Copy link

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

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

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

2 participants