Skip to content

Commit f2e6312

Browse files
test(helpers): move helper that tests for errors to separate file
1 parent 6d6125e commit f2e6312

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

test/helpers/throws-error.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var values = [
2+
undefined,
3+
null,
4+
0,
5+
1,
6+
true,
7+
false,
8+
{},
9+
[],
10+
function() {},
11+
new Date()
12+
];
13+
14+
/**
15+
* Calls parser with invalid arguments.
16+
*
17+
* @param {Function} parser - The parser.
18+
* @param {Function} [assert] - The assertion module.
19+
*/
20+
function throwsError(parser, assert) {
21+
if (typeof assert !== 'function') {
22+
assert = require('assert');
23+
}
24+
25+
values.forEach(function(value) {
26+
it('throws error for argument: ' + value, function() {
27+
assert.throws(function() {
28+
parser(value);
29+
}, TypeError);
30+
});
31+
});
32+
}
33+
34+
module.exports = throwsError;

test/html-to-dom-server.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { assert } = require('chai');
2-
const cases = require('./cases');
32
const htmlparser = require('htmlparser2');
3+
const cases = require('./cases');
4+
const throwsError = require('./helpers/throws-error');
45
const { CASE_SENSITIVE_TAG_NAMES } = require('../lib/constants');
56

67
/**
@@ -18,22 +19,6 @@ function runTests(parser, cases) {
1819
});
1920
}
2021

21-
/**
22-
* Throws tests (a helper that runs tests and verifies that error is thrown).
23-
*
24-
* @param {Function} parser - The parser.
25-
*/
26-
function throwTests(parser) {
27-
const values = [undefined, null, 1, true, {}, ['Array'], Function, Date];
28-
values.forEach(value => {
29-
it(`throws when argument is ${value}`, () => {
30-
assert.throws(() => {
31-
parser(value);
32-
}, TypeError);
33-
});
34-
});
35-
}
36-
3722
/**
3823
* Tests case-sensitive tags (SVG) to make sure their case is preserved.
3924
*
@@ -48,33 +33,28 @@ function testCaseSensitiveTags(parser) {
4833
});
4934
}
5035

51-
/**
52-
* Tests for parser.
53-
*/
5436
describe('server parser', () => {
37+
// before
5538
const parser = require('../');
5639

57-
// check if invalid parameter type throws error
58-
throwTests(parser);
59-
60-
// should be equivalent to `htmlparser2.parseDOM()`
40+
// tests
41+
throwsError(parser);
6142
runTests(parser, cases.html);
6243
runTests(parser, cases.svg);
6344
});
6445

6546
describe('client parser in jsdom', () => {
47+
// before
6648
const jsdomify = require('jsdomify').default;
6749
jsdomify.create();
6850
const parser = require('../lib/html-to-dom-client');
6951

70-
// check if invalid parameter type throws error
71-
throwTests(parser);
72-
73-
// should return the same output as `htmlparser2.parseDOM()`
52+
// tests
53+
throwsError(parser);
7454
runTests(parser, cases.html);
7555
runTests(parser, cases.svg);
76-
7756
testCaseSensitiveTags(parser);
7857

58+
// after
7959
jsdomify.destroy();
8060
});

0 commit comments

Comments
 (0)