pollyjs
pollyjs copied to clipboard
Not able to intercept request and response with node-http
`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!