mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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) {
|
function applyMatcher(matcherName, matcher, inverse, value, ...args) {
|
||||||
const result = matcher.call(null, 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);
|
console.assert(result.pass !== inverse, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -51,42 +51,52 @@ class Expect {
|
|||||||
|
|
||||||
const DefaultMatchers = {
|
const DefaultMatchers = {
|
||||||
toBe: function(value, other, message) {
|
toBe: function(value, other, message) {
|
||||||
|
message = message || `${value} == ${other}`;
|
||||||
return { pass: value === other, message };
|
return { pass: value === other, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeFalsy: function(value, message) {
|
toBeFalsy: function(value, message) {
|
||||||
|
message = message || `${value}`;
|
||||||
return { pass: !value, message };
|
return { pass: !value, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeTruthy: function(value, message) {
|
toBeTruthy: function(value, message) {
|
||||||
|
message = message || `${value}`;
|
||||||
return { pass: !!value, message };
|
return { pass: !!value, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeGreaterThan: function(value, other, message) {
|
toBeGreaterThan: function(value, other, message) {
|
||||||
|
message = message || `${value} > ${other}`;
|
||||||
return { pass: value > other, message };
|
return { pass: value > other, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeGreaterThanOrEqual: function(value, other, message) {
|
toBeGreaterThanOrEqual: function(value, other, message) {
|
||||||
|
message = message || `${value} >= ${other}`;
|
||||||
return { pass: value >= other, message };
|
return { pass: value >= other, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeLessThan: function(value, other, message) {
|
toBeLessThan: function(value, other, message) {
|
||||||
|
message = message || `${value} < ${other}`;
|
||||||
return { pass: value < other, message };
|
return { pass: value < other, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeLessThanOrEqual: function(value, other, message) {
|
toBeLessThanOrEqual: function(value, other, message) {
|
||||||
|
message = message || `${value} <= ${other}`;
|
||||||
return { pass: value <= other, message };
|
return { pass: value <= other, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toBeNull: function(value, message) {
|
toBeNull: function(value, message) {
|
||||||
|
message = message || `${value} == null`;
|
||||||
return { pass: value === null, message };
|
return { pass: value === null, message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toContain: function(value, other, message) {
|
toContain: function(value, other, message) {
|
||||||
|
message = message || `${value} ⊇ ${other}`;
|
||||||
return { pass: value.includes(other), message };
|
return { pass: value.includes(other), message };
|
||||||
},
|
},
|
||||||
|
|
||||||
toEqual: function(value, other, message) {
|
toEqual: function(value, other, message) {
|
||||||
|
message = message || `${JSON.stringify(value)} ≈ ${JSON.stringify(other)}`;
|
||||||
return { pass: JSON.stringify(value) === JSON.stringify(other), message };
|
return { pass: JSON.stringify(value) === JSON.stringify(other), message };
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -25,9 +25,27 @@ const {it, fit, xit} = runner;
|
|||||||
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
|
const {beforeAll, beforeEach, afterAll, afterEach} = runner;
|
||||||
|
|
||||||
describe('testsuite', () => {
|
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();
|
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();
|
runner.run();
|
||||||
|
Loading…
Reference in New Issue
Block a user