我有一个使用axios发出请求的类,当我使用此模块发出请求时,nock不会拦截请求,但是如果我直接通过axios发出请求,则nock会拦截请求
当我使用axios掩码发出请求时,它会返回API的响应,而不是设置为nock的响应
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));
});
});