I'm in the red phase of testing a Slack bot handle, so I expect an error to be throne.
I have the following code:
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;
}
}
I wrote the following test:
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);
});
If I replace the expect()
assertions by the native ava
:
t.true(endpoint.startsWith('http'));
t.is(method, 'POST');
t.deepEqual(body, JSON.stringify(expectedBody));
It throws the error I'm expecting:
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"}}'
I got the following error:
Test finished without running any assertions
Using assert
give the same behavior
Is it correct to assume chai
assertions are catched in my try…catch
clause while the one from ava
are not?
AVA's assertions don't actually throw, but directly change the test result. You can make AVA work with Chai, but you have to ensure the Chai exceptions are not caught during your test.
Further, AVA by default fails a test if it did not run any built-in assertions. You can disable this by setting the failWithoutAssertions
option, see https://github.com/avajs/ava/#custom-assertions.