assaf / node-replay

When API testing slows you down: record and replay HTTP responses like a boss
http://documentup.com/assaf/node-replay
MIT License
522 stars 107 forks source link

Replay Needs Reset or Cleanup Function #159

Open ggb667 opened 5 years ago

ggb667 commented 5 years ago

Replay needs a cleanup function.

I tried this at the start of index.js (for use in a before block in a mocha test):

const HTTP = require('http'); const HTTPS = require('https'); const DNS = require('dns'); const origHttpRequest = HTTP.request; const origHttpGet = HTTP.get; const origHttpsGet = HTTPS.get; const originalLookup = DNS.lookup;

restore() { HTTP.request = origHttpRequest; HTTP.get = origHttpGet; HTTPS.get = origHttpsGet; DNS.lookup = originalLookup; }

But I missed something because this still has problems when people use nock or sinon, and we would need something for the before block to undo this.

ggb667 commented 5 years ago

What I ended up doing for now in each test:

before(function(){ if(process.env.REPLAY) { Replay.mode = process.env.REPLAY; } });

after(function(){ Replay.mode = "bloody"; });