Skip to content

Commit 83f719a

Browse files
committed
Added workaround for unfixed jsdom bug jsdom/jsdom#1107
1 parent 9ccd31d commit 83f719a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

lib/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = {
139139
name: 'DOMComment',
140140
base: 'DOMNode',
141141
identify: function (obj) {
142-
return obj && obj.nodeType === 8;
142+
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 8;
143143
},
144144
equal: function (a, b) {
145145
return a.nodeValue === b.nodeValue;
@@ -158,7 +158,7 @@ module.exports = {
158158
name: 'DOMTextNode',
159159
base: 'DOMNode',
160160
identify: function (obj) {
161-
return obj && obj.nodeType === 3;
161+
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 3;
162162
},
163163
equal: function (a, b) {
164164
return a.nodeValue.trim() === b.nodeValue.trim();
@@ -200,7 +200,7 @@ module.exports = {
200200
name: 'HTMLDocType',
201201
base: 'DOMNode',
202202
identify: function (obj) {
203-
return obj && obj.nodeType === 10 && 'publicId' in obj;
203+
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 10 && 'publicId' in obj;
204204
},
205205
inspect: function (doctype, depth, output, inspect) {
206206
output.code('<!DOCTYPE ' + doctype.name + '>', 'html');
@@ -219,7 +219,7 @@ module.exports = {
219219
name: 'HTMLDocument',
220220
base: 'DOMNode',
221221
identify: function (obj) {
222-
return obj && obj.nodeType === 9 && obj.documentElement && obj.implementation;
222+
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 9 && obj.documentElement && obj.implementation;
223223
},
224224
inspect: function (document, depth, output, inspect) {
225225
for (var i = 0 ; i < document.childNodes.length ; i += 1) {
@@ -240,7 +240,7 @@ module.exports = {
240240
name: 'HTMLElement',
241241
base: 'DOMNode',
242242
identify: function (obj) {
243-
return obj && obj.nodeType === 1 && obj.nodeName && obj.attributes && obj.outerHTML;
243+
return obj && typeof obj.nodeType === 'number' && obj.nodeType === 1 && obj.nodeName && obj.attributes;
244244
},
245245
equal: function (a, b, equal) {
246246
return a.nodeName.toLowerCase() === b.nodeName.toLowerCase() && equal(getAttributes(a), getAttributes(b)) && equal(a.childNodes, b.childNodes);

test/jsdom-compatibility.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*global describe, it*/
2+
var unexpected = require('unexpected'),
3+
unexpectedDom = require('../lib/index'),
4+
jsdom = require('jsdom');
5+
6+
var expect = unexpected.clone().installPlugin(unexpectedDom);
7+
expect.output.installPlugin(require('magicpen-prism'));
8+
9+
expect.addAssertion('to inspect as [itself]', function (expect, subject, value) {
10+
var originalSubject = subject;
11+
if (typeof subject === 'string') {
12+
subject = jsdom.jsdom('<!DOCTYPE html><html><head></head><body>' + subject + '</body></html>').body.firstChild;
13+
}
14+
if (this.flags.itself) {
15+
if (typeof originalSubject === 'string') {
16+
expect(expect.inspect(subject).toString(), 'to equal', originalSubject);
17+
} else {
18+
throw new Error('subject must be given as a string when expected to inspect as itself');
19+
}
20+
} else {
21+
expect(expect.inspect(subject).toString(), 'to equal', value);
22+
}
23+
});
24+
25+
describe('jsdom bug compatibility', function () {
26+
it('should work without issue #1107 fixed', function() {
27+
// https://github.com/tmpvar/jsdom/issues/1107
28+
expect('<select><option value="foo">bar</option></select>', 'to inspect as itself');
29+
expect('<form><p>foo</p></form>', 'to inspect as itself');
30+
});
31+
});

0 commit comments

Comments
 (0)