为什么 Chai 断言不是 运行 而 ava 是?
Why Chai assertion are not run while ava are?
我正处于测试 Slack 机器人句柄的红色阶段,所以我预计会出现错误。
我有以下代码:
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);
});
预期行为
如果我用原生 ava
:
替换 expect()
断言
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
的断言不是?
AVA的断言实际上并没有抛出,而是直接改变了测试结果。您可以让 AVA 与 Chai 一起工作,但您必须确保在测试期间不会捕获 Chai 异常。
此外,如果 AVA 没有 运行 任何内置断言,默认情况下它无法通过测试。您可以通过设置 failWithoutAssertions
选项来禁用此功能,请参阅 https://github.com/avajs/ava/#custom-assertions.
我正处于测试 Slack 机器人句柄的红色阶段,所以我预计会出现错误。
我有以下代码:
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);
});
预期行为
如果我用原生 ava
:
expect()
断言
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
的断言不是?
AVA的断言实际上并没有抛出,而是直接改变了测试结果。您可以让 AVA 与 Chai 一起工作,但您必须确保在测试期间不会捕获 Chai 异常。
此外,如果 AVA 没有 运行 任何内置断言,默认情况下它无法通过测试。您可以通过设置 failWithoutAssertions
选项来禁用此功能,请参阅 https://github.com/avajs/ava/#custom-assertions.