Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

feat: add options validation (schema-utils) #65

Merged
merged 1 commit into from Jul 13, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1,284 changes: 785 additions & 499 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Expand Up @@ -10,15 +10,16 @@
"dependencies": {
"convert-source-map": "^1.5.0",
"istanbul-lib-instrument": "^1.7.3",
"loader-utils": "^1.1.0"
"loader-utils": "^1.1.0",
"schema-utils": "^0.3.0"
},
"engines": {
"node": ">= 4.3 < 5.0.0 || >= 5.10"
},
"license": "MIT",
"scripts": {
"start": "npm run build -- -w",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js'",
"build": "cross-env NODE_ENV=production babel src -d dist --ignore 'src/**/*.test.js' --copy-files",
"clean": "del-cli dist",
"clean:dist": "del-cli dist",
"lint": "eslint --cache src test",
Expand All @@ -27,15 +28,14 @@
"prepublish": "npm run build",
"release": "standard-version",
"security": "nsp check",
"serve:dev": "nodemon $2 --exec babel-node",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --collectCoverageFrom='src/**/*.js' --coverage",
"travis:coverage": "npm run test:coverage -- --runInBand",
"travis:lint": "npm run lint && npm run security",
"travis:test": "npm run test -- --runInBand",
"webpack-defaults": "webpack-defaults",
"appveyor:test": "npm run test"
"appveyor:test": "npm run test",
"webpack-defaults": "webpack-defaults"
},
"devDependencies": {
"babel-cli": "^6.24.0",
Expand All @@ -44,13 +44,13 @@
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.6.0",
"cross-env": "^3.2.4",
"del-cli": "^0.2.1",
"eslint": "^3.18.0",
"cross-env": "^5.0.1",
"del-cli": "^1.1.0",
"eslint": "^4.2.0",
"eslint-config-webpack": "^1.2.5",
"eslint-plugin-import": "^2.7.0",
"jest": "^20.0.4",
"lint-staged": "^3.6.1",
"lint-staged": "^4.0.1",
"memory-fs": "^0.4.1",
"nsp": "^2.6.3",
"pre-commit": "^1.2.2",
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
@@ -1,8 +1,15 @@
import { createInstrumenter } from 'istanbul-lib-instrument';
import loaderUtils from 'loader-utils';
import validateOptions from 'schema-utils';
import convert from 'convert-source-map';
/* eslint-disable-line */
const schema = require('./options');

export default function (source, sourceMap) {
const options = Object.assign({ produceSourceMap: true }, loaderUtils.getOptions(this));

validateOptions(schema, options, 'Istanbul Instrumenter Loader');

let srcMap = sourceMap;
// use inline source map, if any
if (!srcMap) {
Expand All @@ -12,7 +19,6 @@ export default function (source, sourceMap) {
}
}

const options = Object.assign({ produceSourceMap: true }, loaderUtils.getOptions(this));
const instrumenter = createInstrumenter(options);

instrumenter.instrument(source, this.resourcePath, (error, instrumentedSource) => {
Expand Down
27 changes: 27 additions & 0 deletions src/options.json
@@ -0,0 +1,27 @@
{
"type": "object",
"properties": {
"debug": {
"type": "boolean"
},
"compact": {
"type": "boolean"
},
"autoWrap": {
"type": "boolean"
},
"esModules": {
"type": "boolean"
},
"coverageVariable": {
"type": "string"
},
"preserveComments": {
"type": "boolean"
},
"produceSourceMap": {
"type": "boolean"
}
},
"additionalProperties": true
}
10 changes: 10 additions & 0 deletions test/Errors.test.js
@@ -0,0 +1,10 @@
import loader from '../src';

describe('Errors', () => {
test('Options Validation Error', () => {
const err = () => loader.call({ query: { debug: 'foo' } });

expect(err).toThrow();
expect(err).toThrowErrorMatchingSnapshot();
});
});
10 changes: 10 additions & 0 deletions test/__snapshots__/Errors.test.js.snap
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Errors Options Validation Error 1`] = `
"Validation Error

Istanbul Instrumenter Loader Invalid Options

options.debug should be boolean
"
`;
4 changes: 2 additions & 2 deletions test/__snapshots__/index.test.js.snap

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions test/fixtures/basic.js
@@ -1,7 +1,5 @@
module.exports = class Foo {

bar() {
return !!this;
}

};
2 changes: 1 addition & 1 deletion test/utils/loader.js
Expand Up @@ -3,7 +3,7 @@ import loader from '../../src/cjs';

const normalize = str => str.split(path.sep).join('/');

module.exports = function (source, map, ...args) {
module.exports = function wrapper(source, map, ...args) {
// hack the resourcePath to be consistent across systems so that tests always work
this.resourcePath = normalize(this.resourcePath.replace(path.resolve(__dirname, '..'), ''));

Expand Down