在Jest中测试承诺的最佳方法

时间:2017-03-27 03:36:28

标签: javascript jestjs

除非我误解了某些内容,否则在vNext之前,解析和拒绝(https://facebook.github.io/jest/docs/expect.html#resolves)将无法使用。现在/同时用Jest测试承诺的推荐方法是什么?它只是把期望放在天堂和渔获物中吗?

例如:

describe('Fetching', () => {
    const filters = {
        startDate: '2015-09-01'
    };
    const api = new TestApiTransport();

    it('should reject if no startdate is given', () => {
        MyService.fetch().catch(e => expect(e).toBeTruthy()); // see rejects/resolves in v20+
    });            

    it('should return expected data', () => {
        MyService.fetch(filters, null, api).then(serviceObjects => {
            expect(serviceObjects).toHaveLength(2);
        }).catch(e => console.log(e));
    });            
});

3 个答案:

答案 0 :(得分:14)

现在你也可以用这种方式写它:docs

describe('Fetching', () => {
    const filters = {
        startDate: '2015-09-01'
    };
    const api = new TestApiTransport(); 

 it('should reject if no startdate is given', () => {
   expect.assertions(1);
   return expect(MyService.fetch()).rejects.toEqual({
     error: 'Your code message',
   });
 });          


 it('should return expected data', () => {
   expect.assertions(1);
   return expect(MyService.fetch(filters, null, api)).resolves.toEqual(extectedObjectFromApi);
 });            
});

更新(06.01.2019)

同意接受的答案不能正确排列 expect.assertions(1);完成了所有的魔力。 Link to docs

  

expect.assertions(number)验证一定数量的断言   在测试期间调用。这在测试时通常很有用   异步代码,以确保回调中的断言   实际上被召唤了。

因此,将此行置于顶部将控制断言的特定数量是在测试运行时生成的。

答案 1 :(得分:12)

resolvecatch

中返回承诺和期望
describe('Fetching', () = > {
  const filters = {
    startDate: '2015-09-01'
  };
  const api = new TestApiTransport();
  it('should reject if no startdate is given', () = > {
    return MyService.fetch()
      .catch (e => expect(e).toBeTruthy()); // see rejects/resolves in v20+
  });
  it('should return expected data', () = > {
    return MyService.fetch(filters, null, api)
      .then(serviceObjects => {
        expect(serviceObjects).toHaveLength(2);
      })
  });
});

或使用async/await

describe('Fetching', () = > {
  const filters = {
    startDate: '2015-09-01'
  };
  const api = new TestApiTransport();
  it('should reject if no startdate is given', async() = > {
    try {
      const r = await MyService.fetch()
    } catch (e) {
      expect(e).toBeTruthy()
    }
  });
  it('should return expected data', async() = > {
    const serviceObjects = await MyService.fetch(filters, null, api)
    expect(serviceObjects).toHaveLength(2);
  });
});

答案 2 :(得分:0)

我能够使用AXIOS来测试JEST的HTTP REST调用。

it('has an API worth testing', async () => {
  let httpResult = null;
  await callThefunctionThatReturnsPromiseToMakeTheAxiosApiCall()
    .then(function(result) {httpResult=result;})
    .catch(function(err) {httpResult=err;});
  expect(httpResult.data.myData).toBe("myExpectedValue");
});

it('has an API worth testing', async () => {
  let httpResult = await callThefunctionThatReturnsPromiseToMakeTheAxiosApiCall();
  expect(httpResult.data.myData).toBe("myExpectedValue");
});