Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lint): Add jest & node eslint plugins #1642

Merged
merged 5 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
"node": true
},
"plugins": ["jsdoc"],
"extends": ["eslint:recommended", "plugin:jsdoc/recommended", "prettier"],
"extends": [
"eslint:recommended",
"plugin:jsdoc/recommended",
"plugin:jest/recommended",
"plugin:node/recommended",
"prettier"
],
"globals": { "Set": true, "Symbol": true },
"rules": {
"array-callback-return": [
Expand Down Expand Up @@ -47,7 +53,10 @@
"jsdoc/require-param-name": 2,
"jsdoc/require-param-type": 2,
"jsdoc/require-param": 2,
"jsdoc/valid-types": 2
"jsdoc/valid-types": 2,

"node/no-unsupported-features/es-builtins": 0, // TODO
"node/shebang": 0
},
"settings": {
"jsdoc": {
Expand Down
3 changes: 1 addition & 2 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ var suites = new Suites();
var regexIdx = process.argv.indexOf('--regex') + 1;
if (regexIdx > 0) {
if (regexIdx === process.argv.length) {
console.error('Error: the "--regex" option requires a value');
process.exit(1);
throw new Error('Error: the "--regex" option requires a value');
}
suites.filter(process.argv[regexIdx]);
}
Expand Down
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"benchmark": "^2.1.4",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-jsdoc": "^30.7.13",
"eslint-plugin-node": "^11.1.0",
"husky": "^4.3.6",
"jest": "^26.6.3",
"jquery": "^3.5.1",
Expand Down
26 changes: 13 additions & 13 deletions test/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('$(...)', function () {
expect(attr).toBe('autofocus');
});

it('(key, value) : should set attr', function () {
it('(key, value) : should set one attr', function () {
var $pear = $('.pear').attr('id', 'pear');
expect($('#pear')).toHaveLength(1);
expect($pear).toBeInstanceOf($);
});

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

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

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

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

expect(test('foo').hasClass('foo')).toBe(true);
expect(test('foo bar').hasClass('foo')).toBe(true);
expect(test('bar foo').hasClass('foo')).toBe(true);
expect(test('bar foo bar').hasClass('foo')).toBe(true);
expect(withClass('foo').hasClass('foo')).toBe(true);
expect(withClass('foo bar').hasClass('foo')).toBe(true);
expect(withClass('bar foo').hasClass('foo')).toBe(true);
expect(withClass('bar foo bar').hasClass('foo')).toBe(true);
});

it('(invalid class) : should return false', function () {
var cls = $('#fruits').hasClass('fruits');
expect(cls).toBe(false);
expect(test('foo-bar').hasClass('foo')).toBe(false);
expect(test('foo-bar').hasClass('foo')).toBe(false);
expect(test('foo-bar').hasClass('foo-ba')).toBe(false);
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
expect(withClass('foo-bar').hasClass('foo-ba')).toBe(false);
});

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

it('(empty string argument) : should return false', function () {
expect(test('foo').hasClass('')).toBe(false);
expect(test('foo bar').hasClass('')).toBe(false);
expect(test('foo bar').removeClass('foo').hasClass('')).toBe(false);
expect(withClass('foo').hasClass('')).toBe(false);
expect(withClass('foo bar').hasClass('')).toBe(false);
expect(withClass('foo bar').removeClass('foo').hasClass('')).toBe(false);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ describe('$(...)', function () {
expect($container[0].children[0]).toBe($wrap[0]);
});

it('(html) : should wrap elements with it', function () {
it('(html) : should wrap single element with it', function () {
var parent = doc('<p>').wrapAll('<div></div>').parent();
expect(parent).toHaveLength(1);
expect(parent.is('div')).toBe(true);
Expand Down
39 changes: 9 additions & 30 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ describe('$(...)', function () {

describe('.load', function () {
it('should throw a TypeError if given invalid input', function () {
try {
(function () {
cheerio.load();
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('cheerio.load() expects a string');
}
expect(function () {
cheerio.load();
}).toThrow('cheerio.load() expects a string');
});
});

Expand Down Expand Up @@ -81,16 +74,9 @@ describe('$(...)', function () {
});

it('should throw an Error if given an invalid selector', function () {
try {
(function () {
$('#fruits').find(':bah');
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
}
expect(function () {
$('#fruits').find(':bah');
}).toThrow('unmatched pseudo-class');
});

describe('(cheerio object) :', function () {
Expand Down Expand Up @@ -464,16 +450,9 @@ describe('$(...)', function () {
});

it('(selector) : should throw an Error if given an invalid selector', function () {
try {
(function () {
$('.orange').siblings(':bah');
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
}
expect(function () {
$('.orange').siblings(':bah');
}).toThrow('unmatched pseudo-class');
});

it('(selector) : does not consider the contents of siblings when filtering (GH-374)', function () {
Expand Down
13 changes: 7 additions & 6 deletions test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ describe('cheerio', function () {
expect($.html()).toBe('<body><b>foo</b> <b>bar</b></body>');
});

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

it('(buffer) : should accept a buffer', function () {
var html = '<html><head></head><body>foo</body></html>';
// eslint-disable-next-line node/no-unsupported-features/node-builtins
var $html = cheerio.load(Buffer.from(html));
expect($html.html()).toBe(html);
});
Expand Down
2 changes: 1 addition & 1 deletion test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('parse', function () {
expect(root.childNodes[0].type).toBe('directive');
});

it('should simply return root ', function () {
it('should simply return root', function () {
var oldroot = parse(basic, defaultOpts, true);
var root = parse(oldroot, defaultOpts, true);
expect(root).toBe(oldroot);
Expand Down
2 changes: 1 addition & 1 deletion test/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('render', function () {
});

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