pollyjs: Mocha Setup is not working in typescript based tests.

Description

I have setup mocha as defined in the setupMocha example. But when I run in replay mode, the recordings are created again thus having duplicate recordings. Please note that I am using ts-mocha to run the tests like this, and the below code is in one of the tests I have.

Shareable Source

this is how we run the tests. "test:integ": "ts-mocha -p ./tests/integ/tsconfig.json --opts ./tests/integ/mocha.opts", mocha.opts

--timeout=60000
--reporter mocha-multi-reporters
--reporter-options configFile=./tests/integ/report-config.json
./tests/integ/config/test-setup.ts
./tests/integ/**/*.ts


const NodeHttpAdapter = require('@pollyjs/adapter-node-http');

const { Polly, setupMocha: setupPolly } = require('@pollyjs/core');
const FSPersister = require('@pollyjs/persister-fs');
const path = require('path');
Polly.register(NodeHttpAdapter);
Polly.register(FSPersister);
setupPolly({
    /* default configuration options */
    recordIfMissing: true,
    mode: 'replay',
    persister: 'fs',
    adapters: ['node-http'],
    persisterOptions: {
      fs: {
        recordingsDir: path.resolve(__dirname, '../recordings')
      }
    }
  });

Error Message & Stack Trace

There is no error but if I stop my app and run my functional tests then the tests should use the already recorded recordings since I set the mode as replay and also it should not record again and populate the recordings more.

Dependencies

Copy the @pollyjs dependencies from package.json:

    "@pollyjs/adapter": "^4.2.1",
    "@pollyjs/adapter-fetch": "^4.2.1",
    "@pollyjs/adapter-node-http": "^4.2.1",
    "@pollyjs/adapter-xhr": "^4.2.1",
    "@pollyjs/core": "^4.2.1",
    "@pollyjs/persister-fs": "^4.2.1",
    "@pollyjs/persister-rest": "^4.2.1",

Environment

Node.js v10.15.0 darwin 17.7.0 npm – 6.11.3

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 33

Most upvoted comments

Sorry @jasonmit , I have fixed my example and the back-end call is now getting recorded. It looks like we have to strictly follow the documentation. https://github.com/raghanag/polly-test/blob/master/recordings/Puppeteer-Suite_952821357/should-be-able-to-navigate-to-all-routes_1130491217/recording.har#L54

I updated my app with replay mode and tested against the https://github.com/esausilva/example-create-react-app-express it has both front-end and back-end services, once I record the test, and replay the test when the back-end is down, it is working. Looks like PollyJS only records fetch, XHR. maybe my app is not using one of the methods. Thanks a lot @jasonmit

Took a look at it, this configuration should resolve your issue:

After now understanding what you’re trying to achieve the comment below isn’t going to work. Your Selenium runtime will never be interfacing with your actual application (once recorded).

setupPolly({
  persister: 'fs',
  adapters: ['node-http'],
  persisterOptions: {
    fs: {
      recordingsDir: path.resolve(__dirname, '../recordings')
    }
  },
  matchRequestsBy: {
    headers(headers:Record<string, string>) {
      // `host` is unstable since the port changes between every run
      // probably configurable with Selenium to make it stable?
      delete headers.host;
      
      return headers;
    },
    url: {
      // same reason as above
      port: false,
    }
  }
});

Thanks for taking the time to put together a reproduction ❤️