Skip to content

Commit

Permalink
Throw a useful error on invalid input to cheerio.load() (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke authored and fb55 committed Sep 9, 2020
1 parent 3229723 commit ea7c0bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var serialize = require('dom-serializer'),
*/

exports.load = function(content, options, isDocument) {
if (content === null || content === undefined) {
throw new Error('cheerio.load() expects a string');
}

var Cheerio = require('./cheerio');

options = _.defaults(flattenOptions(options || {}), defaultOptions);
Expand Down
11 changes: 11 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ describe('$(...)', function() {
$ = cheerio.load(fruits);
});

describe('.load', function() {
it('should throw a TypeError if given invalid input', function() {
expect(function() {
cheerio.load();
}).to.throwException(function(err) {
expect(err).to.be.an(Error);
expect(err.message).to.be('cheerio.load() expects a string');
});
});
});

describe('.find', function() {

it('() : should find nothing', function() {
Expand Down

0 comments on commit ea7c0bc

Please sign in to comment.