How to use the nock.recorder function in nock

To help you get started, we’ve selected a few nock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Mozu / mozu-storefront-sdk / test / utils / record.js View on Github external
if (!has_fixtures) try {
        require(path.resolve(fp));
        console.log('using recorded fixtures ' + fp);
        has_fixtures = true;
      } catch (e) {
        nock.recorder.rec({
          dont_print: true
        });
      } else {
        has_fixtures = false;
        fs.readdirSync(path.join(test_folder, fixtures_folder)).filter(function(filename) {
          return filename.indexOf('fixtures-') === 0 && path.extname(filename) === ".js";
        }).map(function(filename) {
          return path.resolve(test_folder, fixtures_folder, filename);
        }).forEach(fs.unlinkSync);
        nock.recorder.rec({
          dont_print: true
        });
      }
    },
    // saves our recording if fixtures didn't already exist
github unlock-protocol / unlock / unlock-js / src / __tests__ / helpers / nockHelper.js View on Github external
constructor(endpoint, debug = false, record = false) {
    this.nockScope = nock(endpoint, { encodedQueryParams: true })

    this.recording = record
    if (record) {
      nock.recorder.rec({
        output_objects: true,
      })
    }

    this.anyRequestSetUp = false
    this.debug = debug
    // ethers hard-codes this value, see https://github.com/ethers-io/ethers.js/issues/489
    this._rpcRequestId = 42
    this._noMatches = []

    nock.emitter.on('no match', (clientRequestObject, options, body) => {
      this._noMatches.push(body)
      if (debug) {
        if (!this.anyRequestSetUp) {
          console.log(
            new Error('No mocks have been set up, but a request was made!')
github tjanczuk / dpush / test / dpush-tests.js View on Github external
function teardownFunc(done) {
    if (process.env.GCM_NOCK_REC) {
      // play nock recording
      var scope = scopeWritten ? ',\n[' : '[';
      var lineWritten;
      nock.recorder.play().forEach(function (line) {
        if (line.indexOf('nock') >= 0) {
          scope += (lineWritten ? ',\n' : '') + 'function (nock) { var result = ' + line + ' return result; }';
          lineWritten = true;
        }
      });

      if (lineWritten) {
          scope += ']';
          scopeWritten = true;
          fs.appendFileSync(__dirname + '/dpush-tests.nock.js', scope);
      }

      nock.recorder.clear();
    }

    unNockHttp();
github nock / nock / types / tests.ts View on Github external
scope = scope.persist(false)
scope = scope.replyContentLength()
scope = scope.replyDate()
scope = scope.replyDate(new Date())

inst = inst.delay(2000)
inst = inst.delay({ head: 1000, body: 1000 })
inst = inst.delayBody(2000)
inst = inst.delayConnection(2000)
inst = inst.socketDelay(2000)

scope.done() // $ExpectType void
scope.isDone() // $ExpectType boolean
scope.restore() // $ExpectType void

nock.recorder.rec()
nock.recorder.rec(true)
nock.recorder.rec({
  dont_print: true,
  output_objects: true,
})
nock.recorder.clear()
strings = nock.recorder.play() as string[]
defs = nock.recorder.play() as nock.Definition[]

// Usage
// $ExpectType Scope
nock('http://example.test')
  .get('/users/1')
  .reply(200, {
    _id: '123ABC',
    _rev: '946B7D1C',
github groovecoder / discord / tests / recorder.js View on Github external
setTimeout(function() {

    var filepath = testUtils.fixturesDir + testNumber + '/';
    var manifest = {
        urls: {},
        description: yargs.description || '',
        posts: 0 // Will increment as we POST comments
    };

    // Calling play() provides an array of recorded URL calls with their payload
    nock.recorder.play().forEach(function(obj) {

        var filename = obj.path.replace(/\//g, '-').substr(1) + '.json';
        var contents;

        // Don't bother recording the initial request to the hook
        if (obj.scope.indexOf(testUtils.appHost) !== -1) return;

        // Add this to the registry
        manifest.urls[obj.path] = {
            file: filename,
            method: obj.method
        };

        // Increment posts if need be
        if (obj.method.toLowerCase() === 'post') manifest.posts++;
github ngageoint / mage-server / functionalTests / record.js View on Github external
after: function (done) {
      if (!has_fixtures && saveToFile) {
        var recordedText = nock.recorder.play();
        console.log("----- Record.js: Saving http request recording -----");
        console.log("Saving file at: " + fp)
        var text = "var nock = require('nock');\n" + recordedText.join('\n');
        // Use "sync" version to force execution to wait for file to be written
        fs.writeFileSync(fp, text, 'utf8', function (err) {
          if (err) return console.log(err);
        });
      } else {
        console.log("*** Not saving http request recording.  It's likely the " +
        "file already exists.");
      }
    }
  }
github s3pweb / loopback-nodejs-client / test / index.js View on Github external
const nockMode = process.env.NOCK;

if (nockMode) {
  var nock = require("nock");

  if (nockMode == "record") {
    const fs = require("fs-extra");

    var recordNameFile = "record.txt";

    fs.writeFile(recordNameFile, "");

    var appendLogToFile = async content => {
      await fs.appendFile(recordNameFile, content);
    };
    nock.recorder.rec({
      use_separator: false,
      logging: appendLogToFile
    });
  }
}

var test = require("ava");

const user = "user@mydomain.com";
const password = "admin";

var LoopbackClient = require("../src");

var loopbackClient, token;

if (nockMode == "play") { nock(apiUrl, {"encodedQueryParams":true})
github apis-is / apis / test / integration / endpoints.js View on Github external
after(() => {
  if (process.env.RECORD_MOCK_DATA) {
    const nockCallObjects = nock.recorder.play()
    fs.writeFileSync(mockDataFilename, JSON.stringify(nockCallObjects, null, 2))
  }
})
github poetic-labs / nock-vcr-recorder-mocha / lib / recorder.js View on Github external
function startRecording(options) {
  nock.restore();
  nock.recorder.clear();
  nock.recorder.rec(options.recorder);
}
github poetic / nock-vcr-recorder / lib / use-cassette.js View on Github external
function nockStartRecording() {
  return nock.recorder.rec({
    output_objects:  true,
    dont_print:      true
  });
}