Skip to content
This repository was archived by the owner on May 11, 2018. It is now read-only.

Commit d1f301e

Browse files
existentialismhzoo
authored andcommitted
Add tests for debug output (#156)
1 parent 9667dae commit d1f301e

File tree

12 files changed

+458
-4
lines changed

12 files changed

+458
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
lib
3+
test/tmp
34
.DS_Store
45
*.log
5-
.vscode
6+
.vscode

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,22 @@ Until `compat-table` is a standalone npm module for data we are using the git ur
3434
`"compat-table": "github:kangax/compat-table#gh-pages",`
3535

3636
So we update and then run `npm run build-data`. If there are no changes, then `plugins.json` will be the same.
37+
38+
### Writing Tests
39+
40+
#### General
41+
42+
All the tests for `babel-preset-env` exist in the `test/fixtures` folder. The
43+
test setup and conventions are exactly the same as testing a Babel plugin, so
44+
please read our [documentation on writing tests](https://github.com/babel/babel/blob/master/CONTRIBUTING.md#babel-plugin-x).
45+
46+
#### Testing the `debug` option
47+
48+
Testing debug output to `stdout` is similar. Under the `test/debug-fixtures`,
49+
create a folder with a descriptive name of your test, and add the following:
50+
51+
* Add a `options.json` file (just as the other tests, this is essentially a
52+
`.babelrc`) with the desired test configuration (required)
53+
* Add a `stdout.txt` file with the expected debug output. For added
54+
convenience, if there is no `stdout.txt` present, the test runner will
55+
generate one for you.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,17 @@
5252
"devDependencies": {
5353
"babel-cli": "^6.11.4",
5454
"babel-eslint": "^7.1.1",
55+
"babel-helper-fixtures": "^6.22.0",
5556
"babel-helper-plugin-test-runner": "^6.18.0",
5657
"babel-plugin-transform-flow-strip-types": "^6.8.0",
5758
"babel-preset-es2015": "^6.14.0",
5859
"babel-register": "^6.14.0",
60+
"chai": "^3.0.0",
5961
"compat-table": "kangax/compat-table#b0cec63ea21f3a7788a8eececcb918de903b7fc5",
6062
"eslint": "^3.13.1",
6163
"eslint-config-babel": "^5.0.0",
6264
"eslint-plugin-flowtype": "^2.29.1",
65+
"fs-extra": "^2.0.0",
6366
"lodash": "^4.15.0",
6467
"mocha": "^3.0.2",
6568
"rimraf": "^2.5.4"

src/normalize-options.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ export const validateIncludesAndExcludes = (opts = [], type) => {
2121
`Invalid Option: The '${type}' option must be an Array<String> of plugins/built-ins`
2222
);
2323

24-
const unknownOpts = opts.filter((opt) => !validIncludesAndExcludes.includes(opt));
24+
const unknownOpts = [];
25+
opts.forEach((opt) => {
26+
if (validIncludesAndExcludes.indexOf(opt) === -1) {
27+
unknownOpts.push(opt);
28+
}
29+
});
2530

2631
invariant(
2732
unknownOpts.length === 0,
@@ -56,7 +61,7 @@ export const validateLooseOption = (looseOpt = false) => {
5661

5762
export const validateModulesOption = (modulesOpt = "commonjs") => {
5863
invariant(
59-
modulesOpt === false || Object.keys(moduleTransformations).includes(modulesOpt),
64+
modulesOpt === false || Object.keys(moduleTransformations).indexOf(modulesOpt) > -1,
6065
`Invalid Option: The 'modules' option must be either 'false' to indicate no modules, or a
6166
module type which can be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'.`
6267
);

test/debug-fixtures.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const chai = require("chai");
2+
const child = require("child_process");
3+
const fs = require("fs-extra");
4+
const helper = require("babel-helper-fixtures");
5+
const path = require("path");
6+
7+
const fixtureLoc = path.join(__dirname, "debug-fixtures");
8+
const tmpLoc = path.join(__dirname, "tmp");
9+
10+
const clear = () => {
11+
process.chdir(__dirname);
12+
if (fs.existsSync(tmpLoc)) fs.removeSync(tmpLoc);
13+
fs.mkdirSync(tmpLoc);
14+
process.chdir(tmpLoc);
15+
};
16+
17+
const saveInFiles = (files) => {
18+
Object.keys(files).forEach((filename) => {
19+
const content = files[filename];
20+
fs.outputFileSync(filename, content);
21+
});
22+
};
23+
24+
const assertTest = (stdout, stderr, opts) => {
25+
stderr = stderr.trim();
26+
27+
if (stderr) {
28+
throw new Error("stderr:\n" + stderr);
29+
}
30+
31+
stdout = stdout.trim();
32+
stdout = stdout.replace(/\\/g, "/");
33+
34+
if (opts.stdout) {
35+
const expectStdout = opts.stdout.trim();
36+
chai.expect(stdout).to.equal(expectStdout, "stdout didn't match");
37+
} else {
38+
const file = path.join(opts.testLoc, "stdout.txt");
39+
console.log(`New test file created: ${file}`);
40+
fs.outputFileSync(file, stdout);
41+
}
42+
};
43+
44+
const buildTest = (opts) => {
45+
const binLoc = path.join(process.cwd(), "node_modules/.bin/babel");
46+
47+
return (callback) => {
48+
clear();
49+
saveInFiles(opts.inFiles);
50+
51+
let args = [binLoc];
52+
args = args.concat(opts.args);
53+
54+
const spawn = child.spawn(process.execPath, args);
55+
56+
let stdout = "";
57+
let stderr = "";
58+
59+
spawn.stdout.on("data", (chunk) => stdout += chunk);
60+
spawn.stderr.on("data", (chunk) => stderr += chunk);
61+
62+
spawn.on("close", () => {
63+
let err;
64+
65+
try {
66+
assertTest(stdout, stderr, opts);
67+
} catch (e) {
68+
err = e;
69+
}
70+
71+
callback(err);
72+
});
73+
};
74+
};
75+
76+
describe("debug output", () => {
77+
fs.readdirSync(fixtureLoc).forEach((testName) => {
78+
const testLoc = path.join(fixtureLoc, testName);
79+
80+
const opts = {
81+
args: ["src", "--out-dir", "lib"],
82+
testLoc: testLoc,
83+
};
84+
85+
const stdoutLoc = path.join(testLoc, "stdout.txt");
86+
87+
if (fs.existsSync(stdoutLoc)) {
88+
opts.stdout = helper.readFile(stdoutLoc);
89+
}
90+
91+
const optionsLoc = path.join(testLoc, "options.json");
92+
93+
if (!fs.existsSync(optionsLoc)) {
94+
throw new Error(`Debug test '${testName}' is missing an options.json file`);
95+
}
96+
97+
opts.inFiles = {
98+
"src/in.js": "",
99+
".babelrc": helper.readFile(optionsLoc),
100+
};
101+
102+
it(testName, buildTest(opts));
103+
});
104+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
["../../lib", {
4+
"debug": true,
5+
"targets": {
6+
"browsers": "chrome >= 54, ie 10",
7+
"node": 6
8+
},
9+
"useBuiltIns": true
10+
}]
11+
]
12+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
babel-preset-env: `DEBUG` option
2+
3+
Using targets:
4+
{
5+
"chrome": 54,
6+
"ie": 10,
7+
"node": 6
8+
}
9+
10+
Modules transform: commonjs
11+
12+
Using plugins:
13+
transform-es2015-arrow-functions {"ie":10}
14+
transform-es2015-block-scoped-functions {"ie":10}
15+
transform-es2015-block-scoping {"ie":10}
16+
transform-es2015-classes {"ie":10}
17+
transform-es2015-computed-properties {"ie":10}
18+
check-es2015-constants {"ie":10}
19+
transform-es2015-destructuring {"ie":10,"node":6}
20+
transform-es2015-for-of {"ie":10,"node":6}
21+
transform-es2015-function-name {"ie":10,"node":6}
22+
transform-es2015-literals {"ie":10}
23+
transform-es2015-object-super {"ie":10}
24+
transform-es2015-parameters {"ie":10}
25+
transform-es2015-shorthand-properties {"ie":10}
26+
transform-es2015-spread {"ie":10}
27+
transform-es2015-sticky-regex {"ie":10}
28+
transform-es2015-template-literals {"ie":10}
29+
transform-es2015-typeof-symbol {"ie":10}
30+
transform-es2015-unicode-regex {"ie":10}
31+
transform-regenerator {"ie":10}
32+
transform-exponentiation-operator {"ie":10,"node":6}
33+
transform-async-to-generator {"chrome":54,"ie":10,"node":6}
34+
syntax-trailing-function-commas {"chrome":54,"ie":10,"node":6}
35+
36+
Using polyfills:
37+
es6.typed.uint8-clamped-array {"ie":10}
38+
es6.map {"ie":10,"node":6}
39+
es6.set {"ie":10,"node":6}
40+
es6.weak-map {"ie":10,"node":6}
41+
es6.weak-set {"ie":10,"node":6}
42+
es6.reflect.apply {"ie":10,"node":6}
43+
es6.reflect.construct {"ie":10,"node":6}
44+
es6.reflect.define-property {"ie":10,"node":6}
45+
es6.reflect.delete-property {"ie":10,"node":6}
46+
es6.reflect.get {"ie":10,"node":6}
47+
es6.reflect.get-own-property-descriptor {"ie":10,"node":6}
48+
es6.reflect.get-prototype-of {"ie":10,"node":6}
49+
es6.reflect.has {"ie":10,"node":6}
50+
es6.reflect.is-extensible {"ie":10,"node":6}
51+
es6.reflect.own-keys {"ie":10,"node":6}
52+
es6.reflect.prevent-extensions {"ie":10,"node":6}
53+
es6.reflect.set {"ie":10,"node":6}
54+
es6.reflect.set-prototype-of {"ie":10,"node":6}
55+
es6.promise {"ie":10,"node":6}
56+
es6.symbol {"ie":10,"node":6}
57+
es6.object.assign {"ie":10}
58+
es6.object.is {"ie":10}
59+
es6.object.set-prototype-of {"ie":10}
60+
es6.function.name {"ie":10,"node":6}
61+
es6.string.raw {"ie":10}
62+
es6.string.from-code-point {"ie":10}
63+
es6.string.code-point-at {"ie":10}
64+
es6.string.repeat {"ie":10}
65+
es6.string.starts-with {"ie":10}
66+
es6.string.ends-with {"ie":10}
67+
es6.string.includes {"ie":10}
68+
es6.regexp.flags {"ie":10,"node":6}
69+
es6.regexp.match {"ie":10}
70+
es6.regexp.replace {"ie":10}
71+
es6.regexp.split {"ie":10}
72+
es6.regexp.search {"ie":10}
73+
es6.array.from {"ie":10,"node":6}
74+
es6.array.of {"ie":10}
75+
es6.array.copy-within {"ie":10}
76+
es6.array.find {"ie":10}
77+
es6.array.find-index {"ie":10}
78+
es6.array.fill {"ie":10}
79+
es6.array.iterator {"ie":10}
80+
es6.number.is-finite {"ie":10}
81+
es6.number.is-integer {"ie":10}
82+
es6.number.is-safe-integer {"ie":10}
83+
es6.number.is-nan {"ie":10}
84+
es6.number.epsilon {"ie":10}
85+
es6.number.min-safe-integer {"ie":10}
86+
es6.number.max-safe-integer {"ie":10}
87+
es6.math.acosh {"ie":10}
88+
es6.math.asinh {"ie":10}
89+
es6.math.atanh {"ie":10}
90+
es6.math.cbrt {"ie":10}
91+
es6.math.clz32 {"ie":10}
92+
es6.math.cosh {"ie":10}
93+
es6.math.expm1 {"ie":10}
94+
es6.math.fround {"ie":10}
95+
es6.math.hypot {"ie":10}
96+
es6.math.imul {"ie":10}
97+
es6.math.log1p {"ie":10}
98+
es6.math.log10 {"ie":10}
99+
es6.math.log2 {"ie":10}
100+
es6.math.sign {"ie":10}
101+
es6.math.sinh {"ie":10}
102+
es6.math.tanh {"ie":10}
103+
es6.math.trunc {"ie":10}
104+
es7.array.includes {"ie":10,"node":6}
105+
es7.object.values {"ie":10,"node":6}
106+
es7.object.entries {"ie":10,"node":6}
107+
es7.object.get-own-property-descriptors {"ie":10,"node":6}
108+
es7.string.pad-start {"chrome":54,"ie":10,"node":6}
109+
es7.string.pad-end {"chrome":54,"ie":10,"node":6}
110+
web.timers {"chrome":54,"ie":10,"node":6}
111+
web.immediate {"chrome":54,"ie":10,"node":6}
112+
web.dom.iterable {"chrome":54,"ie":10,"node":6}
113+
src/in.js -> lib/in.js
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"presets": [
3+
["../../lib", {
4+
"debug": true,
5+
"exclude": [
6+
"transform-async-to-generator",
7+
"transform-regenerator", "transform-es2015-parameters"
8+
],
9+
"targets": {
10+
"firefox": 52,
11+
"node": 7.4
12+
}
13+
}]
14+
]
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
babel-preset-env: `DEBUG` option
2+
3+
Using targets:
4+
{
5+
"firefox": 52,
6+
"node": 7.4
7+
}
8+
9+
Modules transform: commonjs
10+
11+
Using plugins:
12+
transform-es2015-destructuring {"firefox":52}
13+
transform-es2015-for-of {"firefox":52}
14+
transform-es2015-function-name {"firefox":52}
15+
transform-es2015-literals {"firefox":52}
16+
transform-exponentiation-operator {"node":7.4}
17+
syntax-trailing-function-commas {"node":7.4}
18+
src/in.js -> lib/in.js
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
["../../lib", {
4+
"debug": true,
5+
"targets": {
6+
"browsers": "ie 10, ios 9, safari 7, edge 13, chrome 54, firefox 49"
7+
},
8+
"useBuiltIns": true
9+
}]
10+
]
11+
}

0 commit comments

Comments
 (0)