nockBack fails to record any fixtures

Viewed 723

I cannot get nockBack to record any fixtures, although it should do that. My test code looks as follows:

describe("#searchForProjects", function () {
beforeEach(function () {
  nock.back.setMode("record");
  this.con = getTestConnection(ApiType.Production);
});

it("finds a home project", async function () {
  const { nockDone, context } = await nock.back("search_for_project.json");

  await searchForProjects(this.con, "home:dancermak", {
    idOnly: true,
    exactMatch: true
  }).should.eventually.deep.equal([
    {
      name: "home:dancermak",
      apiUrl: normalizeUrl(ApiType.Production)
    }
  ]);

  nockDone();
});

});

Just running this specific test results in a NetConnectNotAllowedError: Nock: Disallowed net connect for $URL.

I have tried including a nock.restore() before the whole test, which results in the request going through, but nock doesn't bother recording anything.

The underlying code is using the https module from nodejs, so that shouldn't be a problem?

Any help would be greatly appreciated.

2 Answers

I have finally managed to crack this and the solution is embarrassingly simple: recording must be activated before creating any http(s).request calls. In my case it was obscured a bit as I have a class that on construction saves either http.request or https.request in a member variable. Activating the recorder beforehands solves the issue.

For me the problem was that I had a failing assertion in my test. This meant that nockDone was not called and so no nocks were written to disk.

Related