为什么Nock不使用axios掩码拦截请求?

时间:2019-04-02 23:46:40

标签: nock

我有一个使用axios发出请求的类,当我使用此模块发出请求时,nock不会拦截请求,但是如果我直接通过axios发出请求,则nock会拦截请求

当我使用axios掩码发出请求时,它会返回API的响应,而不是设置为nock的响应

AXIOS掩码(http.js)

const axios = require("axios");

const http = axios.create({
 ....
});

http.interceptors.request.use(request => {
 ...
});

const authenticate = () => {
  .....
};

const get = path =>
  authenticate()
  .then(() => http.get("/v1" + path))
  .then(response => humps.camelizeKeys(response.data))
  .catch(logError);

const post = (path, data) =>
  authenticate()
  .then(() => http.post("/v1" + path, data))
  .then(response => humps.camelizeKeys(response.data))
  .catch(logError);

const put = (path, data) =>
  authenticate()
  .then(() => http.put("/v1" + path, data))
  .then(response => humps.camelizeKeys(response.data))
  .catch(logError);

配置模型

const http = require("../utils/http");

const get = (query) => {
  return http
    .get(`${path}/${query}`)
    .then(result => result)
    .catch(error => {
      logger.error({
        message: "Error getting domain",
        originalMessage: error.message
      });

      throw error;
    });
};

测试

const nock = require('nock');
const axios = require('axios');
///configuration uses MASK AXIOS
import configuration from '../../../models/domain';

describe('configuration', () => {
  test('ok', () => {
    const responseMock = {
      "id": "id",
      "router": "router",
      "handle": "handle",
      "domain_id": "domain_id"
    };

    nock('URL')
    .get('QUERY')
    .reply(200, responseMock);

    configuration.get('QUERY')
    .then((response) => expect(response).toEqual(responseMock))
    .catch((error) => console.log(error));

  });
});

0 个答案:

没有答案