Skip to content

Commit 075cc5d

Browse files
authored
chore(lint): Add jest & node eslint plugins (#1642)
And fixes all reported issues.
1 parent 43592d6 commit 075cc5d

File tree

10 files changed

+164
-56
lines changed

10 files changed

+164
-56
lines changed

.eslintrc.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
"node": true
44
},
55
"plugins": ["jsdoc"],
6-
"extends": ["eslint:recommended", "plugin:jsdoc/recommended", "prettier"],
6+
"extends": [
7+
"eslint:recommended",
8+
"plugin:jsdoc/recommended",
9+
"plugin:jest/recommended",
10+
"plugin:node/recommended",
11+
"prettier"
12+
],
713
"globals": { "Set": true, "Symbol": true },
814
"rules": {
915
"array-callback-return": [
@@ -47,7 +53,10 @@
4753
"jsdoc/require-param-name": 2,
4854
"jsdoc/require-param-type": 2,
4955
"jsdoc/require-param": 2,
50-
"jsdoc/valid-types": 2
56+
"jsdoc/valid-types": 2,
57+
58+
"node/no-unsupported-features/es-builtins": 0, // TODO
59+
"node/shebang": 0
5160
},
5261
"settings": {
5362
"jsdoc": {

benchmark/benchmark.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ var suites = new Suites();
66
var regexIdx = process.argv.indexOf('--regex') + 1;
77
if (regexIdx > 0) {
88
if (regexIdx === process.argv.length) {
9-
console.error('Error: the "--regex" option requires a value');
10-
process.exit(1);
9+
throw new Error('Error: the "--regex" option requires a value');
1110
}
1211
suites.filter(process.argv[regexIdx]);
1312
}

package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
"benchmark": "^2.1.4",
4444
"eslint": "^7.17.0",
4545
"eslint-config-prettier": "^7.1.0",
46+
"eslint-plugin-jest": "^24.1.3",
4647
"eslint-plugin-jsdoc": "^30.7.13",
48+
"eslint-plugin-node": "^11.1.0",
4749
"husky": "^4.3.6",
4850
"jest": "^26.6.3",
4951
"jquery": "^3.5.1",

test/api/attributes.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ describe('$(...)', function () {
3333
expect(attr).toBe('autofocus');
3434
});
3535

36-
it('(key, value) : should set attr', function () {
36+
it('(key, value) : should set one attr', function () {
3737
var $pear = $('.pear').attr('id', 'pear');
3838
expect($('#pear')).toHaveLength(1);
3939
expect($pear).toBeInstanceOf($);
4040
});
4141

42-
it('(key, value) : should set attr', function () {
42+
it('(key, value) : should set multiple attr', function () {
4343
var $el = cheerio('<div></div> <div></div>').attr('class', 'pear');
4444

4545
expect($el[0].attribs['class']).toBe('pear');
@@ -539,26 +539,26 @@ describe('$(...)', function () {
539539
});
540540

541541
describe('.hasClass', function () {
542-
function test(attr) {
542+
function withClass(attr) {
543543
return cheerio('<div class="' + attr + '"></div>');
544544
}
545545

546546
it('(valid class) : should return true', function () {
547547
var cls = $('.apple').hasClass('apple');
548548
expect(cls).toBe(true);
549549

550-
expect(test('foo').hasClass('foo')).toBe(true);
551-
expect(test('foo bar').hasClass('foo')).toBe(true);
552-
expect(test('bar foo').hasClass('foo')).toBe(true);
553-
expect(test('bar foo bar').hasClass('foo')).toBe(true);
550+
expect(withClass('foo').hasClass('foo')).toBe(true);
551+
expect(withClass('foo bar').hasClass('foo')).toBe(true);
552+
expect(withClass('bar foo').hasClass('foo')).toBe(true);
553+
expect(withClass('bar foo bar').hasClass('foo')).toBe(true);
554554
});
555555

556556
it('(invalid class) : should return false', function () {
557557
var cls = $('#fruits').hasClass('fruits');
558558
expect(cls).toBe(false);
559-
expect(test('foo-bar').hasClass('foo')).toBe(false);
560-
expect(test('foo-bar').hasClass('foo')).toBe(false);
561-
expect(test('foo-bar').hasClass('foo-ba')).toBe(false);
559+
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
560+
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
561+
expect(withClass('foo-bar').hasClass('foo-ba')).toBe(false);
562562
});
563563

564564
it('should check multiple classes', function () {
@@ -573,9 +573,9 @@ describe('$(...)', function () {
573573
});
574574

575575
it('(empty string argument) : should return false', function () {
576-
expect(test('foo').hasClass('')).toBe(false);
577-
expect(test('foo bar').hasClass('')).toBe(false);
578-
expect(test('foo bar').removeClass('foo').hasClass('')).toBe(false);
576+
expect(withClass('foo').hasClass('')).toBe(false);
577+
expect(withClass('foo bar').hasClass('')).toBe(false);
578+
expect(withClass('foo bar').removeClass('foo').hasClass('')).toBe(false);
579579
});
580580
});
581581

test/api/manipulation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ describe('$(...)', function () {
369369
expect($container[0].children[0]).toBe($wrap[0]);
370370
});
371371

372-
it('(html) : should wrap elements with it', function () {
372+
it('(html) : should wrap single element with it', function () {
373373
var parent = doc('<p>').wrapAll('<div></div>').parent();
374374
expect(parent).toHaveLength(1);
375375
expect(parent.is('div')).toBe(true);

test/api/traversing.js

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,9 @@ describe('$(...)', function () {
1313

1414
describe('.load', function () {
1515
it('should throw a TypeError if given invalid input', function () {
16-
try {
17-
(function () {
18-
cheerio.load();
19-
})();
20-
21-
throw new Error('Function did not throw');
22-
} catch (err) {
23-
expect(err).toBeInstanceOf(Error);
24-
expect(err.message).toBe('cheerio.load() expects a string');
25-
}
16+
expect(function () {
17+
cheerio.load();
18+
}).toThrow('cheerio.load() expects a string');
2619
});
2720
});
2821

@@ -81,16 +74,9 @@ describe('$(...)', function () {
8174
});
8275

8376
it('should throw an Error if given an invalid selector', function () {
84-
try {
85-
(function () {
86-
$('#fruits').find(':bah');
87-
})();
88-
89-
throw new Error('Function did not throw');
90-
} catch (err) {
91-
expect(err).toBeInstanceOf(Error);
92-
expect(err.message).toContain('unmatched pseudo-class');
93-
}
77+
expect(function () {
78+
$('#fruits').find(':bah');
79+
}).toThrow('unmatched pseudo-class');
9480
});
9581

9682
describe('(cheerio object) :', function () {
@@ -464,16 +450,9 @@ describe('$(...)', function () {
464450
});
465451

466452
it('(selector) : should throw an Error if given an invalid selector', function () {
467-
try {
468-
(function () {
469-
$('.orange').siblings(':bah');
470-
})();
471-
472-
throw new Error('Function did not throw');
473-
} catch (err) {
474-
expect(err).toBeInstanceOf(Error);
475-
expect(err.message).toContain('unmatched pseudo-class');
476-
}
453+
expect(function () {
454+
$('.orange').siblings(':bah');
455+
}).toThrow('unmatched pseudo-class');
477456
});
478457

479458
it('(selector) : does not consider the contents of siblings when filtering (GH-374)', function () {

test/api/utils.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@ describe('cheerio', function () {
110110
expect($.html()).toBe('<body><b>foo</b> <b>bar</b></body>');
111111
});
112112

113-
// TODO:
114-
// it('(html) : should handle xml tag option', function() {
115-
// var $ = $.load('<body><script>oh hai</script></body>', { xml : true });
116-
// console.log($('script')[0].type);
117-
// expect($('script')[0].type).to.be('tag');
118-
// });
113+
it('(html) : should handle xml tag option', function () {
114+
var $ = cheerio.load('<body><script><foo></script></body>', {
115+
xml: true,
116+
});
117+
expect($('script')[0].children[0].type).toBe('tag');
118+
});
119119

120120
it('(buffer) : should accept a buffer', function () {
121121
var html = '<html><head></head><body>foo</body></html>';
122+
// eslint-disable-next-line node/no-unsupported-features/node-builtins
122123
var $html = cheerio.load(Buffer.from(html));
123124
expect($html.html()).toBe(html);
124125
});

test/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ describe('parse', function () {
210210
expect(root.childNodes[0].type).toBe('directive');
211211
});
212212

213-
it('should simply return root ', function () {
213+
it('should simply return root', function () {
214214
var oldroot = parse(basic, defaultOpts, true);
215215
var root = parse(oldroot, defaultOpts, true);
216216
expect(root).toBe(oldroot);

test/xml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('render', function () {
3838
});
3939

4040
describe('(dom)', function () {
41-
it('should keep camelCase for new nodes', function () {
41+
it('should not keep camelCase for new nodes', function () {
4242
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
4343
expect(dom(str, { xml: false })).toBe(
4444
'<someelem someattribute="something">hello</someelem>'

0 commit comments

Comments
 (0)