代码之家  ›  专栏  ›  技术社区  ›  Édouard Lopez

为什么不运行Chai断言,而运行ava?

  •  1
  • Édouard Lopez  · 技术社区  · 7 年前

    我正处于测试松弛机器人句柄的红色阶段,因此我预计会出现错误。

      generateHandler({ fetch }) {
        return async function(bot, message) {
          let status;
          try {
            let response = await fetch(endpoint, {
              method: 'POST',
              body: {build_parameters: { ALLOW_OVERWRITE: "true" }}
            });
            status = response.ok;
          } catch (error) {
            status = false;
          }
          return status;
        }
      }
    

    我编写了以下测试:

    import test from 'ava';
    import { expect } from 'chai';
    const { generateHandler } = require('../lib/handlers/redeploy_master.js');
    
    
    test('triggers a new build on circleci', async (t) => {
      const fetch = async (endpoint, { method, body }) => {
        const expectedBody = { build_parameters: { ALLOW_OVERWRITE: "true" } };
        expect(method).to.be('POST');
        expect(body).to.deep.equal(JSON.stringify(expectedBody));
        return Promise.resolve({ok: true});
      };
      const handler = generateHandler({fetch});
      const bot = {reply: () => {}};
    
      return await handler(bot, undefined);
    });
    

    如果我更换 expect() ava

    t.true(endpoint.startsWith('http'));
    t.is(method, 'POST');
    t.deepEqual(body, JSON.stringify(expectedBody));
    

       10:     t.is(method, 'POST');
       11:     t.deepEqual(body, JSON.stringify(expectedBody));
       12:     expect(method).to.be('POST');
    
      Difference:
    
      - {
      -   build_parameters: Object { … },
      - }
      + '{"build_parameters":{"ALLOW_OVERWRITE":"true"}}'
    

    实际行为

    我得到了以下错误:

    Test finished without running any assertions
    

    使用 assert

    问题

    假设正确吗 chai 断言在我的 try…catch ava公司 不是吗?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Mark Wubben    7 年前

    AVA的断言实际上并没有抛出,而是直接改变了测试结果。您可以让AVA与Chai一起工作,但您必须确保在测试期间不会捕捉到Chai异常。

    failWithoutAssertions 选项,请参阅 https://github.com/avajs/ava/#custom-assertions