使用axios和拦截器的JEST(REACT JS)测试API

时间:2020-04-08 21:03:25

标签: javascript reactjs jestjs axios interceptor

我正在我的项目中使用模拟进行测试。但是由于某种原因,我在运行npm test-api-service时遇到了此错误。

我在开玩笑。

当我尝试运行测试时,我不知道为什么会出现此错误。

有人打电话帮助我吗?

api.service.test.js

import ApiService from './Api-service';

jest.mock('axios');

it('return wellheads', async () => {
  const wellheads = {
    0: {
      UniqueWellboreIdentifier: 'BR 3-OGX-50D-RJS',
      UniqueWellIdentifier: 'BR 3-OGX-50D-RJS',
      CountryIdentifier: 'Brazil',
      BasinIdentifier: 'Santos',
      FieldIdentifier: 'TUBARAO AZUL',
      Latitude: -23.56569,
      Longitude: -41.445133,
    },
    1: {
      UniqueWellboreIdentifier: 'BR 1-BRSA-532-SPS',
      UniqueWellIdentifier: 'BR 1-BRSA-532-SPS',
      CountryIdentifier: 'Brazil',
      BasinIdentifier: 'Santos',
      FieldIdentifier: 'No field related',
      Latitude: -25.624473,
      Longitude: -43.905731,
    },
  };
  const resp = { data: wellheads };
  axios.interceptors.get.mockResolvedValue(resp);

  return ApiService.getWellhead().then((data) =>
    expect(data).toEqual(wellheads)
  );
});

api-interceptors.js

import axios from 'axios';
import { authContext, getAdalApiToken } from '../config/AdalConfig';
import conf, { getResource } from './../settings';

const api = axios.create({
  baseURL: getResource('psa').ApiBaseUrl,
});

api.interceptors.request.use(
  (config) => {
    return new Promise((resolve, reject) => {
      authContext.acquireToken(conf.AzureADClientId, (message, token, msg) => {
        if (!!token) {
          config.headers.Authorization = `Bearer ${token}`;
          resolve(config);
        } else {
          reject(config);
        }
      });
    });
  },
  function (error) {
    console.log(error);
    return Promise.reject(error);
  }
);

api.interceptors.response.use(undefined, (err) => {
  if (
    err.response &&
    err.response.status === 401 &&
    err.response.config &&
    !err.response.config.__isRetryRequest
  ) {
    err.response.config.__isRetryRequest = true;
    return new Promise((resolve, reject) => {
      authContext.acquireToken(conf.AzureADClientId, (message, token, msg) => {
        getAdalApiToken();
        if (!!token) {
          api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
          resolve(err.config);
        } else {
          reject(err.config);
        }
      });

      // authContext.acquireToken(process.env.REACT_APP_CLIENT_ID, (message, token, msg) => {
      //   axios.defaults.headers.common['Authorization'] = `Bearer ${token}`;
      //   err.config.headers.Authorization = `Bearer ${token}`;

      //   axios(err.config).then(resolve, reject);
      // });
    });
  }
  throw err;
});

export default api;

当我跑步时,错误是:

测试套件无法运行runice.test。 TypeError:无法读取未定义的属性“拦截器”

   7 | });
   8 | 
>  9 | api.interceptors.request.use(
     |     ^
  10 |   (config) => {
  10 |   (config) => {
  11 |     return new Promise((resolve, reject) => {
  12 |       authContext.acquireToken(conf.AzureADClientId, (message, token, msg) => {

0 个答案:

没有答案