Skip to content

Commit 129fe64

Browse files
authored
Test invalid json (#7)
* add codecov * test invalid json
1 parent 639f491 commit 129fe64

File tree

6 files changed

+28
-4
lines changed

6 files changed

+28
-4
lines changed

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ const validateOptions = require('schema-utils')
33

44
const schema = require('./options.json')
55

6-
function shouldInline(limit = 10240, size) {
6+
const DEFAULT_OPTIONS = {
7+
limit: 10240,
8+
}
9+
10+
function shouldInline(limit, size) {
711
return size <= parseInt(limit, 10)
812
}
913

1014
// https://v8.dev/blog/cost-of-javascript-2019#json
1115
module.exports = function(source) {
12-
const options = getOptions(this) || {}
16+
const options = Object.assign({}, DEFAULT_OPTIONS, getOptions(this))
1317

1418
validateOptions(schema, options, 'JSON Perf Loader')
1519

test/fixtures/file-invalid.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{

test/fixtures/fixture-invalid.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* eslint-disable */
2+
import json from './file-invalid.json';

test/helpers/compiler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const modules = config => {
1515
type: 'javascript/auto',
1616
use: {
1717
loader: path.resolve(__dirname, '../../src'),
18-
options: config.loader.options || {},
18+
options: config.loader.options,
1919
},
2020
},
2121
]

test/loader-invalid.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const webpack = require('./helpers/compiler')
2+
3+
describe('Loader invaild', () => {
4+
it('should throw error', async () => {
5+
const config = {
6+
loader: {
7+
test: /\.json$/,
8+
type: 'javascript/auto',
9+
},
10+
}
11+
12+
const stats = await webpack('fixture-invalid.js', config)
13+
const { modules, errors, warnings } = stats.toJson()
14+
15+
expect(errors.length).toBeGreaterThan(0)
16+
expect(warnings).toEqual([])
17+
})
18+
})

test/loader.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('Loader', () => {
66
loader: {
77
test: /\.json$/,
88
type: 'javascript/auto',
9-
options: {},
109
},
1110
}
1211

0 commit comments

Comments
 (0)