question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to record only external requests?

See original GitHub issue

I’m trying to use pollyjs to test express js endpoint. It works fine until record is persisted to file. After that it just skips actual app code, so that test becomes always green. I assume this is because localhost(127.0.0.1) request/response is being intercepted by polly. Is it possible to record only external requests to prevent this?

app.js:

router.get('/widgets', async (req, res) => {
  //some external remote calls
}

polly_config.js:

module.exports = {
  adapters: ['node-http'],
  persister: 'fs',
  persisterOptions: {
    fs: {
      recordingsDir: `${__dirname}/http_recordings`,
    },
  },
  recordFailedRequests: true,
  recordIfMissing: false,
  matchRequestsBy: {
    headers: false,
  },
};

app.spec.js:

const chai = require('chai');
const { expect } = chai;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const { Polly } = require('@pollyjs/core');
const NodeHttpAdapter = require('@pollyjs/adapter-node-http');
const FSPersister = require('@pollyjs/persister-fs');
const pollyConfig = require('./polly_config');

Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);

describe('app', () => {
  const testPort = 3001;
  before(() => {
    app.listen(testPort);
  });

  describe('GET widgets', () => {
    it('returns widgets requested', async () => {
      const polly = new Polly('get_widgets', pollyConfig);

      const response = await chai.request(`http://127.0.0.1:${testPort}`)                                           
                                 .get(`/widgets`)
      expect(response).to.have.status(200);

      await polly.stop();
    });
  });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
offirgolancommented, Apr 24, 2019

If you’re using mocha, I highly recommend using the setupMocha test helper.

import { setupMocha as setupPolly } from '@pollyjs/core';

describe('app', function() {
  const testPort = 3001;

  before(() => {
    app.listen(testPort);
  });

  setupPolly({ ...pollyConfig });

  beforeEach(function() {
    const { server } = this.polly;

    server.any(`http://127.0.0.1:${testPort}/*`).passthrough();
  });

  describe('GET widgets', () => {
    it('returns widgets requested', async () => {
      const response = await chai.request(`http://127.0.0.1:${testPort}`).get(`/widgets`)
      expect(response).to.have.status(200);
    });
  });
});
0reactions
offirgolancommented, Apr 24, 2019

Correct. I’m gonna go ahead and close this issue. Feel free to reopen or create a new one if you have any more questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Record and Playback an API to Create a Mock
Enter the URL you wish to record from in the target URL field and click the Record button. You can use http://example.mocklab.io to...
Read more >
Enabling external customers to view problem, change, and ...
External customers can view the problem, change, and request records associated with their customer service cases from the Customer and Consumer Service Portals ......
Read more >
How to Stub External Services in Tests
Disable all remote connections. We'll use Webmock, a gem which helps to stub out external HTTP requests. In this example we'll search the...
Read more >
External Research Data Requests
(Please read carefully before requesting an External Research Application.) ... to protect the confidentiality of education records, and the data may only be ......
Read more >
Share Microsoft Teams Meeting Recordings with External ...
After a Microsoft Teams meeting is recorded, you will automatically receive an email from Microsoft Stream alerting you that the video is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found