axios Jest已检测到以下1个打开的句柄,可能会阻止Jest退出:TLSWRAP

arknldoa  于 2023-04-06  发布在  iOS
关注(0)|答案(1)|浏览(129)

使用jest和axiom编写一个API集成测试。我发誓当我将bearer令牌硬编码到put请求的头部时,我有一个简单的版本可以工作。但是经过一些futzing,我甚至不能ctr + z到达它“工作”的地方-我无法摆脱这个错误:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TLSWRAP

      13 |
      14 |   it('user can create a cart', async () => {
    > 15 |     const response = await axios.put(

我已经尝试修复这些修复,但没有骰子。

我怎样才能让它成功运行(200响应),而不是因为打开句柄而退出?

import axios from 'axios';

describe('Cart Manipulation', () => {
  let oc_token: string;

  beforeAll(() => {
    oc_token = "eyJblahblahblah"
  });

  it('user can create a cart', async () => {
    const response = await axios.put(
      'https://api.urltogoto',
      { 
        'Accept': 'application/json', 
        // 'Authorization': 'Bearer eyJblahblahblah'
        'Authorization': oc_token
      }
    );
    console.log(response);
    expect(response.status).toBe(200);
  });

});
0tdrvxhp

0tdrvxhp1#

找到了解决方案,我不知道是什么具体的变化造成了差异,但大量的futzing周围产生了这个:

import axios, {AxiosRequestConfig} from 'axios';

describe('Cart Manipulation', () => {
  it('user can create a cart', async () => {
    let config = {
      method: 'put',
      maxBodyLength: Infinity,
      url: 'https://api.blablahurl',
      headers: { 
        'Accept': 'application/json', 
        'Authorization': 'Bearer sillylittletoken'
      }
    } as AxiosRequestConfig;

    const response = await axios.request(config)
    .then((response) => {
      console.log(JSON.stringify(response.data));
      expect(response.status).toEqual(200);
    })
    .catch((error) => {
      console.log(error);
    });
  });
});

相关问题