Add tests for debug output (#156)

This commit is contained in:
Brian Ng 2017-02-24 07:27:43 -06:00 committed by Henry Zhu
parent bc02c95ef0
commit 1092dde11c
12 changed files with 458 additions and 4 deletions

View File

@ -1,5 +1,6 @@
node_modules
lib
test/tmp
.DS_Store
*.log
.vscode
.vscode

View File

@ -34,3 +34,22 @@ Until `compat-table` is a standalone npm module for data we are using the git ur
`"compat-table": "github:kangax/compat-table#gh-pages",`
So we update and then run `npm run build-data`. If there are no changes, then `plugins.json` will be the same.
### Writing Tests
#### General
All the tests for `babel-preset-env` exist in the `test/fixtures` folder. The
test setup and conventions are exactly the same as testing a Babel plugin, so
please read our [documentation on writing tests](https://github.com/babel/babel/blob/master/CONTRIBUTING.md#babel-plugin-x).
#### Testing the `debug` option
Testing debug output to `stdout` is similar. Under the `test/debug-fixtures`,
create a folder with a descriptive name of your test, and add the following:
* Add a `options.json` file (just as the other tests, this is essentially a
`.babelrc`) with the desired test configuration (required)
* Add a `stdout.txt` file with the expected debug output. For added
convenience, if there is no `stdout.txt` present, the test runner will
generate one for you.

View File

@ -52,14 +52,17 @@
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-eslint": "^7.1.1",
"babel-helper-fixtures": "^6.22.0",
"babel-helper-plugin-test-runner": "^6.18.0",
"babel-plugin-transform-flow-strip-types": "^6.8.0",
"babel-preset-es2015": "^6.14.0",
"babel-register": "^6.14.0",
"chai": "^3.0.0",
"compat-table": "kangax/compat-table#b0cec63ea21f3a7788a8eececcb918de903b7fc5",
"eslint": "^3.13.1",
"eslint-config-babel": "^5.0.0",
"eslint-plugin-flowtype": "^2.29.1",
"fs-extra": "^2.0.0",
"lodash": "^4.15.0",
"mocha": "^3.0.2",
"rimraf": "^2.5.4"

View File

@ -21,7 +21,12 @@ export const validateIncludesAndExcludes = (opts = [], type) => {
`Invalid Option: The '${type}' option must be an Array<String> of plugins/built-ins`
);
const unknownOpts = opts.filter((opt) => !validIncludesAndExcludes.includes(opt));
const unknownOpts = [];
opts.forEach((opt) => {
if (validIncludesAndExcludes.indexOf(opt) === -1) {
unknownOpts.push(opt);
}
});
invariant(
unknownOpts.length === 0,
@ -56,7 +61,7 @@ export const validateLooseOption = (looseOpt = false) => {
export const validateModulesOption = (modulesOpt = "commonjs") => {
invariant(
modulesOpt === false || Object.keys(moduleTransformations).includes(modulesOpt),
modulesOpt === false || Object.keys(moduleTransformations).indexOf(modulesOpt) > -1,
`Invalid Option: The 'modules' option must be either 'false' to indicate no modules, or a
module type which can be be one of: 'commonjs' (default), 'amd', 'umd', 'systemjs'.`
);

View File

@ -0,0 +1,104 @@
const chai = require("chai");
const child = require("child_process");
const fs = require("fs-extra");
const helper = require("babel-helper-fixtures");
const path = require("path");
const fixtureLoc = path.join(__dirname, "debug-fixtures");
const tmpLoc = path.join(__dirname, "tmp");
const clear = () => {
process.chdir(__dirname);
if (fs.existsSync(tmpLoc)) fs.removeSync(tmpLoc);
fs.mkdirSync(tmpLoc);
process.chdir(tmpLoc);
};
const saveInFiles = (files) => {
Object.keys(files).forEach((filename) => {
const content = files[filename];
fs.outputFileSync(filename, content);
});
};
const assertTest = (stdout, stderr, opts) => {
stderr = stderr.trim();
if (stderr) {
throw new Error("stderr:\n" + stderr);
}
stdout = stdout.trim();
stdout = stdout.replace(/\\/g, "/");
if (opts.stdout) {
const expectStdout = opts.stdout.trim();
chai.expect(stdout).to.equal(expectStdout, "stdout didn't match");
} else {
const file = path.join(opts.testLoc, "stdout.txt");
console.log(`New test file created: ${file}`);
fs.outputFileSync(file, stdout);
}
};
const buildTest = (opts) => {
const binLoc = path.join(process.cwd(), "node_modules/.bin/babel");
return (callback) => {
clear();
saveInFiles(opts.inFiles);
let args = [binLoc];
args = args.concat(opts.args);
const spawn = child.spawn(process.execPath, args);
let stdout = "";
let stderr = "";
spawn.stdout.on("data", (chunk) => stdout += chunk);
spawn.stderr.on("data", (chunk) => stderr += chunk);
spawn.on("close", () => {
let err;
try {
assertTest(stdout, stderr, opts);
} catch (e) {
err = e;
}
callback(err);
});
};
};
describe("debug output", () => {
fs.readdirSync(fixtureLoc).forEach((testName) => {
const testLoc = path.join(fixtureLoc, testName);
const opts = {
args: ["src", "--out-dir", "lib"],
testLoc: testLoc,
};
const stdoutLoc = path.join(testLoc, "stdout.txt");
if (fs.existsSync(stdoutLoc)) {
opts.stdout = helper.readFile(stdoutLoc);
}
const optionsLoc = path.join(testLoc, "options.json");
if (!fs.existsSync(optionsLoc)) {
throw new Error(`Debug test '${testName}' is missing an options.json file`);
}
opts.inFiles = {
"src/in.js": "",
".babelrc": helper.readFile(optionsLoc),
};
it(testName, buildTest(opts));
});
});

View File

@ -0,0 +1,12 @@
{
"presets": [
["../../lib", {
"debug": true,
"targets": {
"browsers": "chrome >= 54, ie 10",
"node": 6
},
"useBuiltIns": true
}]
]
}

View File

@ -0,0 +1,113 @@
babel-preset-env: `DEBUG` option
Using targets:
{
"chrome": 54,
"ie": 10,
"node": 6
}
Modules transform: commonjs
Using plugins:
transform-es2015-arrow-functions {"ie":10}
transform-es2015-block-scoped-functions {"ie":10}
transform-es2015-block-scoping {"ie":10}
transform-es2015-classes {"ie":10}
transform-es2015-computed-properties {"ie":10}
check-es2015-constants {"ie":10}
transform-es2015-destructuring {"ie":10,"node":6}
transform-es2015-for-of {"ie":10,"node":6}
transform-es2015-function-name {"ie":10,"node":6}
transform-es2015-literals {"ie":10}
transform-es2015-object-super {"ie":10}
transform-es2015-parameters {"ie":10}
transform-es2015-shorthand-properties {"ie":10}
transform-es2015-spread {"ie":10}
transform-es2015-sticky-regex {"ie":10}
transform-es2015-template-literals {"ie":10}
transform-es2015-typeof-symbol {"ie":10}
transform-es2015-unicode-regex {"ie":10}
transform-regenerator {"ie":10}
transform-exponentiation-operator {"ie":10,"node":6}
transform-async-to-generator {"chrome":54,"ie":10,"node":6}
syntax-trailing-function-commas {"chrome":54,"ie":10,"node":6}
Using polyfills:
es6.typed.uint8-clamped-array {"ie":10}
es6.map {"ie":10,"node":6}
es6.set {"ie":10,"node":6}
es6.weak-map {"ie":10,"node":6}
es6.weak-set {"ie":10,"node":6}
es6.reflect.apply {"ie":10,"node":6}
es6.reflect.construct {"ie":10,"node":6}
es6.reflect.define-property {"ie":10,"node":6}
es6.reflect.delete-property {"ie":10,"node":6}
es6.reflect.get {"ie":10,"node":6}
es6.reflect.get-own-property-descriptor {"ie":10,"node":6}
es6.reflect.get-prototype-of {"ie":10,"node":6}
es6.reflect.has {"ie":10,"node":6}
es6.reflect.is-extensible {"ie":10,"node":6}
es6.reflect.own-keys {"ie":10,"node":6}
es6.reflect.prevent-extensions {"ie":10,"node":6}
es6.reflect.set {"ie":10,"node":6}
es6.reflect.set-prototype-of {"ie":10,"node":6}
es6.promise {"ie":10,"node":6}
es6.symbol {"ie":10,"node":6}
es6.object.assign {"ie":10}
es6.object.is {"ie":10}
es6.object.set-prototype-of {"ie":10}
es6.function.name {"ie":10,"node":6}
es6.string.raw {"ie":10}
es6.string.from-code-point {"ie":10}
es6.string.code-point-at {"ie":10}
es6.string.repeat {"ie":10}
es6.string.starts-with {"ie":10}
es6.string.ends-with {"ie":10}
es6.string.includes {"ie":10}
es6.regexp.flags {"ie":10,"node":6}
es6.regexp.match {"ie":10}
es6.regexp.replace {"ie":10}
es6.regexp.split {"ie":10}
es6.regexp.search {"ie":10}
es6.array.from {"ie":10,"node":6}
es6.array.of {"ie":10}
es6.array.copy-within {"ie":10}
es6.array.find {"ie":10}
es6.array.find-index {"ie":10}
es6.array.fill {"ie":10}
es6.array.iterator {"ie":10}
es6.number.is-finite {"ie":10}
es6.number.is-integer {"ie":10}
es6.number.is-safe-integer {"ie":10}
es6.number.is-nan {"ie":10}
es6.number.epsilon {"ie":10}
es6.number.min-safe-integer {"ie":10}
es6.number.max-safe-integer {"ie":10}
es6.math.acosh {"ie":10}
es6.math.asinh {"ie":10}
es6.math.atanh {"ie":10}
es6.math.cbrt {"ie":10}
es6.math.clz32 {"ie":10}
es6.math.cosh {"ie":10}
es6.math.expm1 {"ie":10}
es6.math.fround {"ie":10}
es6.math.hypot {"ie":10}
es6.math.imul {"ie":10}
es6.math.log1p {"ie":10}
es6.math.log10 {"ie":10}
es6.math.log2 {"ie":10}
es6.math.sign {"ie":10}
es6.math.sinh {"ie":10}
es6.math.tanh {"ie":10}
es6.math.trunc {"ie":10}
es7.array.includes {"ie":10,"node":6}
es7.object.values {"ie":10,"node":6}
es7.object.entries {"ie":10,"node":6}
es7.object.get-own-property-descriptors {"ie":10,"node":6}
es7.string.pad-start {"chrome":54,"ie":10,"node":6}
es7.string.pad-end {"chrome":54,"ie":10,"node":6}
web.timers {"chrome":54,"ie":10,"node":6}
web.immediate {"chrome":54,"ie":10,"node":6}
web.dom.iterable {"chrome":54,"ie":10,"node":6}
src/in.js -> lib/in.js

View File

@ -0,0 +1,15 @@
{
"presets": [
["../../lib", {
"debug": true,
"exclude": [
"transform-async-to-generator",
"transform-regenerator", "transform-es2015-parameters"
],
"targets": {
"firefox": 52,
"node": 7.4
}
}]
]
}

View File

@ -0,0 +1,18 @@
babel-preset-env: `DEBUG` option
Using targets:
{
"firefox": 52,
"node": 7.4
}
Modules transform: commonjs
Using plugins:
transform-es2015-destructuring {"firefox":52}
transform-es2015-for-of {"firefox":52}
transform-es2015-function-name {"firefox":52}
transform-es2015-literals {"firefox":52}
transform-exponentiation-operator {"node":7.4}
syntax-trailing-function-commas {"node":7.4}
src/in.js -> lib/in.js

View File

@ -0,0 +1,11 @@
{
"presets": [
["../../lib", {
"debug": true,
"targets": {
"browsers": "ie 10, ios 9, safari 7, edge 13, chrome 54, firefox 49"
},
"useBuiltIns": true
}]
]
}

View File

@ -0,0 +1,125 @@
babel-preset-env: `DEBUG` option
Using targets:
{
"chrome": 54,
"edge": 13,
"firefox": 49,
"ie": 10,
"ios": 9,
"safari": 7
}
Modules transform: commonjs
Using plugins:
transform-es2015-arrow-functions {"ie":10,"ios":9,"safari":7}
transform-es2015-block-scoped-functions {"edge":13,"ie":10,"ios":9,"safari":7}
transform-es2015-block-scoping {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-classes {"ie":10,"ios":9,"safari":7}
transform-es2015-computed-properties {"ie":10,"safari":7}
check-es2015-constants {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-destructuring {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-for-of {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-function-name {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-literals {"firefox":49,"ie":10,"safari":7}
transform-es2015-object-super {"ie":10,"ios":9,"safari":7}
transform-es2015-parameters {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-es2015-shorthand-properties {"ie":10,"safari":7}
transform-es2015-spread {"ie":10,"ios":9,"safari":7}
transform-es2015-sticky-regex {"ie":10,"ios":9,"safari":7}
transform-es2015-template-literals {"ie":10,"safari":7}
transform-es2015-typeof-symbol {"ie":10,"safari":7}
transform-es2015-unicode-regex {"ie":10,"ios":9,"safari":7}
transform-regenerator {"firefox":49,"ie":10,"ios":9,"safari":7}
transform-exponentiation-operator {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
transform-async-to-generator {"chrome":54,"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
syntax-trailing-function-commas {"chrome":54,"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
Using polyfills:
es6.typed.data-view {"edge":13}
es6.typed.int8-array {"edge":13}
es6.typed.uint8-array {"edge":13}
es6.typed.uint8-clamped-array {"ie":10}
es6.typed.int16-array {"edge":13}
es6.typed.uint16-array {"edge":13}
es6.typed.int32-array {"edge":13}
es6.typed.uint32-array {"edge":13}
es6.typed.float32-array {"edge":13}
es6.typed.float64-array {"edge":13}
es6.map {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
es6.set {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
es6.weak-map {"edge":13,"firefox":49,"ie":10,"safari":7}
es6.weak-set {"edge":13,"firefox":49,"ie":10,"safari":7}
es6.reflect.apply {"ie":10,"ios":9,"safari":7}
es6.reflect.construct {"ie":10,"ios":9,"safari":7}
es6.reflect.define-property {"ie":10,"ios":9,"safari":7}
es6.reflect.delete-property {"ie":10,"ios":9,"safari":7}
es6.reflect.get {"ie":10,"ios":9,"safari":7}
es6.reflect.get-own-property-descriptor {"ie":10,"ios":9,"safari":7}
es6.reflect.get-prototype-of {"ie":10,"ios":9,"safari":7}
es6.reflect.has {"ie":10,"ios":9,"safari":7}
es6.reflect.is-extensible {"ie":10,"ios":9,"safari":7}
es6.reflect.own-keys {"ie":10,"ios":9,"safari":7}
es6.reflect.prevent-extensions {"ie":10,"ios":9,"safari":7}
es6.reflect.set {"ie":10,"ios":9,"safari":7}
es6.reflect.set-prototype-of {"ie":10,"ios":9,"safari":7}
es6.promise {"ie":10,"ios":9,"safari":7}
es6.symbol {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
es6.object.assign {"ie":10,"safari":7}
es6.object.is {"ie":10,"safari":7}
es6.object.set-prototype-of {"edge":13,"ie":10,"safari":7}
es6.function.name {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
es6.string.raw {"ie":10,"safari":7}
es6.string.from-code-point {"ie":10,"safari":7}
es6.string.code-point-at {"ie":10,"safari":7}
es6.string.repeat {"ie":10,"safari":7}
es6.string.starts-with {"ie":10,"safari":7}
es6.string.ends-with {"ie":10,"safari":7}
es6.string.includes {"ie":10,"safari":7}
es6.regexp.flags {"edge":13,"ie":10,"safari":7}
es6.regexp.match {"edge":13,"ie":10,"ios":9,"safari":7}
es6.regexp.replace {"edge":13,"ie":10,"ios":9,"safari":7}
es6.regexp.split {"edge":13,"ie":10,"ios":9,"safari":7}
es6.regexp.search {"edge":13,"ie":10,"ios":9,"safari":7}
es6.array.from {"edge":13,"ie":10,"ios":9,"safari":7}
es6.array.of {"ie":10,"safari":7}
es6.array.copy-within {"ie":10,"safari":7}
es6.array.find {"ie":10,"safari":7}
es6.array.find-index {"ie":10,"safari":7}
es6.array.fill {"ie":10,"safari":7}
es6.array.iterator {"ie":10,"safari":7}
es6.number.is-finite {"ie":10,"safari":7}
es6.number.is-integer {"ie":10,"safari":7}
es6.number.is-safe-integer {"ie":10,"safari":7}
es6.number.is-nan {"ie":10,"safari":7}
es6.number.epsilon {"ie":10,"safari":7}
es6.number.min-safe-integer {"ie":10,"safari":7}
es6.number.max-safe-integer {"ie":10,"safari":7}
es6.math.acosh {"ie":10,"safari":7}
es6.math.asinh {"ie":10,"safari":7}
es6.math.atanh {"ie":10,"safari":7}
es6.math.cbrt {"ie":10,"safari":7}
es6.math.clz32 {"ie":10,"safari":7}
es6.math.cosh {"ie":10,"safari":7}
es6.math.expm1 {"ie":10,"safari":7}
es6.math.fround {"ie":10,"safari":7}
es6.math.hypot {"ie":10,"safari":7}
es6.math.imul {"ie":10}
es6.math.log1p {"ie":10,"safari":7}
es6.math.log10 {"ie":10,"safari":7}
es6.math.log2 {"ie":10,"safari":7}
es6.math.sign {"ie":10,"safari":7}
es6.math.sinh {"ie":10,"safari":7}
es6.math.tanh {"ie":10,"safari":7}
es6.math.trunc {"ie":10,"safari":7}
es7.array.includes {"edge":13,"ie":10,"ios":9,"safari":7}
es7.object.values {"edge":13,"ie":10,"ios":9,"safari":7}
es7.object.entries {"edge":13,"ie":10,"ios":9,"safari":7}
es7.object.get-own-property-descriptors {"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
es7.string.pad-start {"chrome":54,"edge":13,"ie":10,"ios":9,"safari":7}
es7.string.pad-end {"chrome":54,"edge":13,"ie":10,"ios":9,"safari":7}
web.timers {"chrome":54,"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
web.immediate {"chrome":54,"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
web.dom.iterable {"chrome":54,"edge":13,"firefox":49,"ie":10,"ios":9,"safari":7}
src/in.js -> lib/in.js

View File

@ -300,6 +300,14 @@ babel-helper-fixtures@^6.20.0:
lodash "^4.2.0"
try-resolve "^1.0.0"
babel-helper-fixtures@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-helper-fixtures/-/babel-helper-fixtures-6.22.0.tgz#3cb9eaf5feae29f001d2754ab43b14a9dfa304ff"
dependencies:
babel-runtime "^6.22.0"
lodash "^4.2.0"
try-resolve "^1.0.0"
babel-helper-function-name@^6.18.0, babel-helper-function-name@^6.8.0:
version "6.18.0"
resolved "https://registry.yarnpkg.com/babel-helper-function-name/-/babel-helper-function-name-6.18.0.tgz#68ec71aeba1f3e28b2a6f0730190b754a9bf30e6"
@ -824,6 +832,13 @@ babel-runtime@^6.0.0, babel-runtime@^6.11.6, babel-runtime@^6.18.0, babel-runtim
core-js "^2.4.0"
regenerator-runtime "^0.10.0"
babel-runtime@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.22.0.tgz#1cf8b4ac67c77a4ddb0db2ae1f74de52ac4ca611"
dependencies:
core-js "^2.4.0"
regenerator-runtime "^0.10.0"
babel-template@^6.14.0, babel-template@^6.15.0, babel-template@^6.16.0, babel-template@^6.8.0:
version "6.16.0"
resolved "https://registry.yarnpkg.com/babel-template/-/babel-template-6.16.0.tgz#e149dd1a9f03a35f817ddbc4d0481988e7ebc8ca"
@ -1626,6 +1641,13 @@ form-data@~2.1.1:
combined-stream "^1.0.5"
mime-types "^2.1.12"
fs-extra@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-2.0.0.tgz#337352bded4a0b714f3eb84de8cea765e9d37600"
dependencies:
graceful-fs "^4.1.2"
jsonfile "^2.1.0"
fs-readdir-recursive@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.0.0.tgz#8cd1745c8b4f8a29c8caec392476921ba195f560"
@ -1752,7 +1774,7 @@ globby@^5.0.0:
pify "^2.0.0"
pinkie-promise "^2.0.0"
graceful-fs@^4.1.2, graceful-fs@^4.1.4:
graceful-fs@^4.1.2, graceful-fs@^4.1.4, graceful-fs@^4.1.6:
version "4.1.11"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.11.tgz#0e8bdfe4d1ddb8854d64e04ea7c00e2a026e5658"
@ -2111,6 +2133,12 @@ json5@^0.5.0:
version "0.5.1"
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
jsonfile@^2.1.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-2.4.0.tgz#3736a2b428b87bbda0cc83b53fa3d633a35c2ae8"
optionalDependencies:
graceful-fs "^4.1.6"
jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"