Реагировать на тест API с ошибкой Nock с сообщением «Ошибка: Nock: нет соответствия для запроса» - PullRequest
0 голосов
/ 26 февраля 2019

Вот код Express Route, который отлично работает в спине и во внешнем интерфейсе.

// Редактирование / обновление by vessel_type by_id - Работает

router.put("/:id", (req, res, next) => {
  Vessel_Type.findByIdAndUpdate(
    req.params.id,
    req.body,

    { new: true },
    (err, updatedRecord) => {
      if (err) {
        console.error(err);
        return next(err);
      } else {
        res.status(200).send(updatedRecord);
      }
    }
  );
});

А вот мой тестовый код API в Reactвнешний интерфейс с nock

  it("API test-3 - PUT (/api/vesseltype/id)", done => {
    nock(host)
      .defaultReplyHeaders({
        "access-control-allow-origin": "*",
        "Content-Type": "application/json"
      })
      .persist()
      .log(console.log)
      .put("/api/vesseltype/5c62cc8f1774b626cd7fdbe6", {
        vesseltype: "Super Large Cargo Ship-45"
      })
      .delayBody(1000)
      .reply(200, "PUT/EDIT data with reqheaders");

    axios
      .put("http://localhost:3000/api/vesseltype/5c62cc8f1774b626cd7fdbe6", {
        vesseltype: "Super Large Cargo Ship-45"
      })
      .then(response => {
        expect(response.data).toBe("PUT/EDIT data with reqheaders");
        expect(typeof response.data).toBe("string");
        done();
      });
  });

Консольное протоколирование ошибки из теста дает следующее в терминале

    console.log node_modules/nock/lib/interceptor.js:332
    matching http://localhost:3000/api/vesseltype/5c62cc8f1774b626cd7fdbe6 to PUT http://localhost:3000/api/vesseltype/5c62cc8f1774b626cd7fdbe6: false

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Error: Nock: No match for request {
      "method": "OPTIONS",
      "url": "http://localhost:3000/api/vesseltype/5c62cc8f1774b626cd7fdbe6",
      "headers": {
        "origin": "http://localhost",
        "access-control-request-method": "PUT",
        "user-agent": "Mozilla/5.0 (linux) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/11.12.0",
        "host": "localhost:3000",
        "content-length": 0
      }
    }
        at Object.dispatchError (/home/paul/codes-Lap/React/Volteo/IES/IES-Rohan-WIP/client/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at EventEmitter.client.on.err (/home/paul/codes-Lap/React/Volteo/IES/IES-Rohan-WIP/client/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
        at EventEmitter.emit (events.js:202:15)

Однако мои маршруты PUT(http://localhost:3000/api/vesseltype/5c62cc8f1774b626cd7fdbe6) идеально подходит между nock и axios.

Я прошел через эти проблемы с github и SO вопросы здесь , здесь здесь но эти решения мне не помогли.

...