Skip to content

Commit 9f92e04

Browse files
committed
Use nested error message in 'queried for' assertions. Fixes #13
1 parent 42be445 commit 9f92e04

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,19 @@ module.exports = {
403403

404404
expect.addAssertion(['HTMLDocument', 'HTMLElement'], 'queried for [first]', function (expect, subject, value) {
405405
var queryResult;
406+
407+
this.errorMode = 'nested';
408+
406409
if (this.flags.first) {
407410
queryResult = subject.querySelector(value);
408411
if (!queryResult) {
409-
this.errorMode = 'nested';
410412
expect.fail(function (output) {
411413
output.error('The selector').sp().jsString(value).sp().error('yielded no results');
412414
});
413415
}
414416
} else {
415417
queryResult = subject.querySelectorAll(value);
416418
if (queryResult.length === 0) {
417-
this.errorMode = 'nested';
418419
expect.fail(function (output) {
419420
output.error('The selector').sp().jsString(value).sp().error('yielded no results');
420421
});

test/unexpected-dom.js

+24
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,30 @@ describe('unexpected-dom', function () {
372372
' The selector .blabla yielded no results'
373373
);
374374
});
375+
376+
it('should return an array-like NodeList', function () {
377+
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div></div><div></div><div></div></body></html>');
378+
379+
expect(document, 'queried for', 'div', 'to be a', 'DOMNodeList');
380+
});
381+
382+
it('should be able to use array semantics', function () {
383+
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div></div><div></div><div></div></body></html>');
384+
385+
expect(document, 'queried for', 'div', 'to have length', 3);
386+
});
387+
388+
it('should fail array checks with useful nested error message', function () {
389+
var document = jsdom.jsdom('<!DOCTYPE html><html><body><div></div><div></div><div></div></body></html>');
390+
391+
expect(function () {
392+
expect(document, 'queried for', 'div', 'to have length', 1);
393+
}, 'to throw',
394+
'expected <!DOCTYPE html><html><body>.........</body></html> queried for \'div\' to have length 1\n' +
395+
' expected NodeList[ <div></div>, <div></div>, <div></div> ] to have length 1\n' +
396+
' expected 3 to be 1'
397+
);
398+
});
375399
});
376400

377401
describe('diffing', function () {

0 commit comments

Comments
 (0)