test: nicer matchers output (#1605)
This patch improves matchers to provide more information once some of them fail.
This commit is contained in:
parent
cb1cb695a5
commit
a3a3774926
@ -43,7 +43,7 @@ class Expect {
|
||||
|
||||
function applyMatcher(matcherName, matcher, inverse, value, ...args) {
|
||||
const result = matcher.call(null, value, ...args);
|
||||
const message = result.message || `expect.${matcherName} failed`;
|
||||
const message = `expect.${matcherName} failed` + (result.message ? `: ${result.message}` : '');
|
||||
console.assert(result.pass !== inverse, message);
|
||||
}
|
||||
}
|
||||
@ -51,42 +51,52 @@ class Expect {
|
||||
|
||||
const DefaultMatchers = {
|
||||
toBe: function(value, other, message) {
|
||||
message = message || `${value} == ${other}`;
|
||||
return { pass: value === other, message };
|
||||
},
|
||||
|
||||
toBeFalsy: function(value, message) {
|
||||
message = message || `${value}`;
|
||||
return { pass: !value, message };
|
||||
},
|
||||
|
||||
toBeTruthy: function(value, message) {
|
||||
message = message || `${value}`;
|
||||
return { pass: !!value, message };
|
||||
},
|
||||
|
||||
toBeGreaterThan: function(value, other, message) {
|
||||
message = message || `${value} > ${other}`;
|
||||
return { pass: value > other, message };
|
||||
},
|
||||
|
||||
toBeGreaterThanOrEqual: function(value, other, message) {
|
||||
message = message || `${value} >= ${other}`;
|
||||
return { pass: value >= other, message };
|
||||
},
|
||||
|
||||
toBeLessThan: function(value, other, message) {
|
||||
message = message || `${value} < ${other}`;
|
||||
return { pass: value < other, message };
|
||||
},
|
||||
|
||||
toBeLessThanOrEqual: function(value, other, message) {
|
||||
message = message || `${value} <= ${other}`;
|
||||
return { pass: value <= other, message };
|
||||
},
|
||||
|
||||
toBeNull: function(value, message) {
|
||||
message = message || `${value} == null`;
|
||||
return { pass: value === null, message };
|
||||
},
|
||||
|
||||
toContain: function(value, other, message) {
|
||||
message = message || `${value} ⊇ ${other}`;
|
||||
return { pass: value.includes(other), message };
|
||||
},
|
||||
|
||||
toEqual: function(value, other, message) {
|
||||
message = message || `${JSON.stringify(value)} ≈ ${JSON.stringify(other)}`;
|
||||
return { pass: JSON.stringify(value) === JSON.stringify(other), message };
|
||||
},
|
||||
|
||||
|
@ -25,9 +25,27 @@ const {it, fit, xit} = runner;
|
||||
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
|
||||
|
||||
describe('testsuite', () => {
|
||||
it('failure', async (state) => {
|
||||
it('toBe', async (state) => {
|
||||
expect(2 + 2).toBe(5);
|
||||
});
|
||||
it('toBeFalsy', async (state) => {
|
||||
expect(true).toBeFalsy();
|
||||
});
|
||||
it('toBeTruthy', async (state) => {
|
||||
expect(false).toBeTruthy();
|
||||
});
|
||||
it('toBeGreaterThan', async (state) => {
|
||||
expect(2).toBeGreaterThan(3);
|
||||
});
|
||||
it('toBeNull', async (state) => {
|
||||
expect(2).toBeNull();
|
||||
});
|
||||
it('toContain', async (state) => {
|
||||
expect('asdf').toContain('e');
|
||||
});
|
||||
it('toEqual', async (state) => {
|
||||
expect([1,2,3]).toEqual([1,2,3,4]);
|
||||
});
|
||||
});
|
||||
|
||||
runner.run();
|
||||
|
Loading…
Reference in New Issue
Block a user