Skip to content

Commit 9065215

Browse files
authored
Merge pull request webpack#3826 from songawee/master
refactor validateSchema and Validation.test to ES2015
2 parents a892c4c + 20968e9 commit 9065215

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

lib/validateSchema.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Gajus Kuizinas @gajus
44
*/
5-
var Ajv = require("ajv");
6-
var ajv = new Ajv({
5+
"use strict";
6+
7+
const Ajv = require("ajv");
8+
const ajv = new Ajv({
79
errorDataPath: "configuration",
810
allErrors: true,
911
verbose: true
@@ -12,16 +14,16 @@ require("ajv-keywords")(ajv, ["instanceof"]);
1214

1315
function validateSchema(schema, options) {
1416
if(Array.isArray(options)) {
15-
var errors = options.map(validateObject.bind(this, schema));
16-
errors.forEach(function(list, idx) {
17+
const errors = options.map((options) => validateObject(schema, options));
18+
errors.forEach((list, idx) => {
1719
list.forEach(function applyPrefix(err) {
18-
err.dataPath = "[" + idx + "]" + err.dataPath;
20+
err.dataPath = `[${idx}]${err.dataPath}`;
1921
if(err.children) {
2022
err.children.forEach(applyPrefix);
2123
}
2224
});
2325
});
24-
return errors.reduce(function(arr, items) {
26+
return errors.reduce((arr, items) => {
2527
return arr.concat(items);
2628
}, []);
2729
} else {
@@ -30,22 +32,20 @@ function validateSchema(schema, options) {
3032
}
3133

3234
function validateObject(schema, options) {
33-
var validate = ajv.compile(schema);
34-
var valid = validate(options);
35+
const validate = ajv.compile(schema);
36+
const valid = validate(options);
3537
return valid ? [] : filterErrors(validate.errors);
3638
}
3739

3840
function filterErrors(errors) {
39-
var newErrors = [];
40-
errors.forEach(function(err) {
41-
var dataPath = err.dataPath;
42-
var children = [];
43-
newErrors = newErrors.filter(function(oldError) {
44-
if(oldError.dataPath.indexOf(dataPath) >= 0) {
41+
let newErrors = [];
42+
errors.forEach((err) => {
43+
const dataPath = err.dataPath;
44+
let children = [];
45+
newErrors = newErrors.filter((oldError) => {
46+
if(oldError.dataPath.includes(dataPath)) {
4547
if(oldError.children) {
46-
oldError.children.forEach(function(child) {
47-
children.push(child);
48-
});
48+
children = children.concat(oldError.children.slice(0));
4949
}
5050
oldError.children = undefined;
5151
children.push(oldError);
@@ -58,7 +58,7 @@ function filterErrors(errors) {
5858
}
5959
newErrors.push(err);
6060
});
61-
//console.log(JSON.stringify(newErrors, 0, 2));
61+
6262
return newErrors;
6363
}
6464

test/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
# Welcome to the webpack test suite!!!!
2-
Every pull request that you submit to webpack (besides README and spelling corrections in comments) requires tests that are created.
2+
Every pull request that you submit to webpack (besides README and spelling corrections in comments) requires tests that are created.
33

44
But don't give up hope!!! Although our tests may appear complex and overwhelming, once you become familiar with the test suite and structure, adding and creating tests will be fun and beneficial as you work inside the codebase! ❤
55

66
## tl;dr
77
* Clone repo
88
* install and link deps
99
* `yarn install && yarn link && yarn link webpack`
10-
* `npm run test` or `npm t`
10+
* `yarn test`
11+
1112
* To run an individual suite: (recommended during development for easier isolated diffs)
12-
13+
1314
Example: `$(npm bin)/mocha --grep ConfigTestCases`
1415

1516
## Test suite overview
1617
We use MochaJS for our tests. For more information on Mocha you can visit their [homepage](https://mochajs.org/)!
1718

1819
### Class Tests
1920
All test files can be found in *.test.js. There are many tests that simply test API's of a specific class/file (such as `Compiler`, `Errors`, Integration, `Parser`, `RuleSet`, Validation).
20-
If the feature you are contributing involves one of those classes, then best to start there to understand the structure.
21+
If the feature you are contributing involves one of those classes, then best to start there to understand the structure.
2122

2223
### xCases
2324
In addition to Class specific tests, there are also directories that end in "Cases". The suites for these cases also have corresponding *.test.js files.
2425

2526
#### cases (`TestCases.test.js`) <sup>1</sup>
26-
Cases are a set of general purpose tests that will run against a variety of permutations of webpack configurations. When you are making a general purpose change that doesn't require you to have a special configuration, you would likely add your tests here. Inside of the `./test/cases` directory you will find tests are broken into thematic sub directories. Take a moment to explore the different options.
27+
Cases are a set of general purpose tests that will run against a variety of permutations of webpack configurations. When you are making a general purpose change that doesn't require you to have a special configuration, you would likely add your tests here. Inside of the `./test/cases` directory you will find tests are broken into thematic sub directories. Take a moment to explore the different options.
2728

28-
To add a new case, create a new directory inside of the top level test groups, and then add an `index.js` file (and any other supporting files).
29+
To add a new case, create a new directory inside of the top level test groups, and then add an `index.js` file (and any other supporting files).
2930

30-
By default this file will be the entry point for the test suite and you can add your `it()`'s there. This will also become bundled so that node env support happens as well.
31+
By default this file will be the entry point for the test suite and you can add your `it()`'s there. This will also become bundled so that node env support happens as well.
3132

3233
#### configCases (`ConfigTestCases.test.js`) <sup>1</sup>
33-
If you are trying to solve a bug which is reproducible when x and y properties are used together in a config, then configCases is the place to be!!!!
34+
If you are trying to solve a bug which is reproducible when x and y properties are used together in a config, then configCases is the place to be!!!!
3435

35-
In addition to an `index.js`, these configCases require a `webpack.config.js` is located inside of your test suite. This will run this specific config through `webpack` just as you were building individually. They will use the same loading/bundling technique of your `it()` tests, however you now have a more specific config use cases that you can write even before you start coding.
36+
In addition to an `index.js`, these configCases require a `webpack.config.js` is located inside of your test suite. This will run this specific config through `webpack` just as you were building individually. They will use the same loading/bundling technique of your `it()` tests, however you now have a more specific config use cases that you can write even before you start coding.
3637

3738
#### statsCases (`Stats.test.js`)
38-
Stats cases are similar to configCases except specifically focusing on the `expected` output of your stats. Instead of writing to the console, however the output of stats will be written to disk.
39+
Stats cases are similar to configCases except specifically focusing on the `expected` output of your stats. Instead of writing to the console, however the output of stats will be written to disk.
3940

4041
By default, the "expected" outcome is a pain to write by hand so instead when statsCases are run the following happens:
4142

42-
* Checks for `expected.txt` file containing expected results.
43+
* Checks for `expected.txt` file containing expected results.
4344
* If the `expected.txt` doesn't match what is output, then an `actual.txt` stats output file will be created and the test will fail. (A typical workflow for stats cases is to fail the test and copy the results from `actual.txt` to `expected.txt`.)
4445
* If the actual output matches `expected.txt`, the tests passes and you are free to submit that PR with pride!!!
4546

46-
## Questions? Comments?
47+
## Questions? Comments?
4748
If you are still nervous or don't quite understand, please submit an issue and tag us in it, and provide a relevant PR while working on!
4849

4950

5051
## Footnotes
5152
<sup>1</sup> webpack's parser supports the use of ES2015 features like arrow functions, harmony exports, etc. However as a library we follow NodeJS's timeline for dropping older versions of node. Because of this we expect your tests on Travis to pass all the way back to NodeJS v0.12; Therefore if you would like specific tests that use these features to be ignored if they are not supported, then you should add a `test.filter.js` file. This allows you to import the syntax needed for that test, meanwhile ignoring it on node versions (during CI) that don't support it. webpack has a variety of helpful exapmles you can refer to if you are just starting out. See the `./helpers` folder to find a list of the versions.
52-

test/Validation.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
var should = require("should");
2-
var webpack = require("../lib/webpack");
3-
var WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
1+
"use strict";
2+
3+
const should = require("should");
4+
const webpack = require("../lib/webpack");
5+
const WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
46

57
describe("Validation", function() {
6-
var testCases = [{
8+
const testCases = [{
79
name: "undefined configuration",
810
config: undefined,
911
message: [

0 commit comments

Comments
 (0)