Migrate babel-cli and babel-generator tests to use jest-expect (#7549)

This commit is contained in:
Deven Bansod 2018-04-11 23:42:56 +05:30 committed by Brian Ng
parent b1c9af3f05
commit 95894397eb
8 changed files with 99 additions and 142 deletions

View File

@ -1,13 +1,11 @@
const includes = require("lodash/includes"); const includes = require("lodash/includes");
const readdir = require("fs-readdir-recursive"); const readdir = require("fs-readdir-recursive");
const helper = require("@babel/helper-fixtures"); const helper = require("@babel/helper-fixtures");
const assert = require("assert");
const rimraf = require("rimraf"); const rimraf = require("rimraf");
const outputFileSync = require("output-file-sync"); const outputFileSync = require("output-file-sync");
const child = require("child_process"); const child = require("child_process");
const merge = require("lodash/merge"); const merge = require("lodash/merge");
const path = require("path"); const path = require("path");
const chai = require("chai");
const fs = require("fs"); const fs = require("fs");
const fixtureLoc = path.join(__dirname, "fixtures"); const fixtureLoc = path.join(__dirname, "fixtures");
@ -53,15 +51,9 @@ const assertTest = function(stdout, stderr, opts) {
if (opts.stderr) { if (opts.stderr) {
if (opts.stderrContains) { if (opts.stderrContains) {
assert.ok( expect(includes(stderr, expectStderr)).toBe(true);
includes(stderr, expectStderr),
"stderr " +
JSON.stringify(stderr) +
" didn't contain " +
JSON.stringify(expectStderr),
);
} else { } else {
chai.expect(stderr).to.equal(expectStderr, "stderr didn't match"); expect(stderr).toBe(expectStderr);
} }
} else if (stderr) { } else if (stderr) {
throw new Error("stderr:\n" + stderr); throw new Error("stderr:\n" + stderr);
@ -73,15 +65,9 @@ const assertTest = function(stdout, stderr, opts) {
if (opts.stdout) { if (opts.stdout) {
if (opts.stdoutContains) { if (opts.stdoutContains) {
assert.ok( expect(includes(stdout, expectStdout)).toBe(true);
includes(stdout, expectStdout),
"stdout " +
JSON.stringify(stdout) +
" didn't contain " +
JSON.stringify(expectStdout),
);
} else { } else {
chai.expect(stdout).to.equal(expectStdout, "stdout didn't match"); expect(stdout).toBe(expectStdout);
} }
} else if (stdout) { } else if (stdout) {
throw new Error("stdout:\n" + stdout); throw new Error("stdout:\n" + stdout);
@ -96,24 +82,19 @@ const assertTest = function(stdout, stderr, opts) {
filename !== ".babelrc" && filename !== ".babelrc" &&
!opts.inFiles.hasOwnProperty(filename) !opts.inFiles.hasOwnProperty(filename)
) { ) {
const expect = opts.outFiles[filename]; const expected = opts.outFiles[filename];
const actual = actualFiles[filename]; const actual = actualFiles[filename];
chai.expect(expect, "Output is missing: " + filename).to.not.be expect(expected).not.toBeUndefined();
.undefined;
if (expect) { if (expected) {
chai expect(actual).toBe(expected);
.expect(actual)
.to.equal(expect, "Compiled output does not match: " + filename);
} }
} }
}); });
Object.keys(opts.outFiles).forEach(function(filename) { Object.keys(opts.outFiles).forEach(function(filename) {
chai expect(actualFiles).toHaveProperty([filename]);
.expect(actualFiles, "Extraneous file in output: " + filename)
.to.contain.key(filename);
}); });
} }
}; };

View File

@ -575,7 +575,7 @@ describe("buildConfigChain", function() {
// fixture("nonexistant-fake"), // fixture("nonexistant-fake"),
// ], // ],
// }); // });
// assert.notEqual(opts2, null); // expect(opts2).not.toBeNull();
// //
// const opts3 = loadOptions({ // const opts3 = loadOptions({
// filename: fixture("nonexistant-fake", "folder", "src.js"), // filename: fixture("nonexistant-fake", "folder", "src.js"),
@ -585,7 +585,7 @@ describe("buildConfigChain", function() {
// fixture("nonexistant-fake"), // fixture("nonexistant-fake"),
// ], // ],
// }); // });
// assert.notEqual(opts3, null); // expect(opts3).not.toBeNull();
}); });
}); });

View File

@ -1,8 +1,6 @@
import Printer from "../lib/printer"; import Printer from "../lib/printer";
import generate, { CodeGenerator } from "../lib"; import generate, { CodeGenerator } from "../lib";
import assert from "assert";
import { parse } from "babylon"; import { parse } from "babylon";
import chai from "chai";
import * as t from "@babel/types"; import * as t from "@babel/types";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
@ -11,12 +9,12 @@ import fixtures from "@babel/helper-fixtures";
describe("generation", function() { describe("generation", function() {
it("completeness", function() { it("completeness", function() {
Object.keys(t.VISITOR_KEYS).forEach(function(type) { Object.keys(t.VISITOR_KEYS).forEach(function(type) {
assert.ok(!!Printer.prototype[type], type + " should exist"); expect(Printer.prototype[type]).toBeTruthy();
}); });
Object.keys(Printer.prototype).forEach(function(type) { Object.keys(Printer.prototype).forEach(function(type) {
if (!/[A-Z]/.test(type[0])) return; if (!/[A-Z]/.test(type[0])) return;
assert.ok(t.VISITOR_KEYS[type], type + " should not exist"); expect(t.VISITOR_KEYS[type]).toBeTruthy();
}); });
}); });
@ -46,7 +44,7 @@ describe("generation", function() {
const generated = generate(combinedAst, { sourceMaps: true }, sources); const generated = generate(combinedAst, { sourceMaps: true }, sources);
chai.expect(generated.map).to.deep.equal( expect(generated.map).toEqual(
{ {
version: 3, version: 3,
sources: ["a.js", "b.js"], sources: ["a.js", "b.js"],
@ -61,7 +59,7 @@ describe("generation", function() {
"sourcemap was incorrectly generated", "sourcemap was incorrectly generated",
); );
chai.expect(generated.rawMappings).to.deep.equal( expect(generated.rawMappings).toEqual(
[ [
{ {
name: undefined, name: undefined,
@ -157,12 +155,9 @@ describe("generation", function() {
"raw mappings were incorrectly generated", "raw mappings were incorrectly generated",
); );
chai expect(generated.code).toBe(
.expect(generated.code) "function hi(msg) {\n console.log(msg);\n}\n\nhi('hello');",
.to.equal( );
"function hi(msg) {\n console.log(msg);\n}\n\nhi('hello');",
"code was incorrectly generated",
);
}); });
it("identifierName", function() { it("identifierName", function() {
@ -189,7 +184,7 @@ describe("generation", function() {
code, code,
); );
chai.expect(generated.map).to.deep.equal( expect(generated.map).toEqual(
{ {
version: 3, version: 3,
sources: ["inline"], sources: ["inline"],
@ -200,7 +195,7 @@ describe("generation", function() {
"sourcemap was incorrectly generated", "sourcemap was incorrectly generated",
); );
chai.expect(generated.rawMappings).to.deep.equal( expect(generated.rawMappings).toEqual(
[ [
{ {
name: undefined, name: undefined,
@ -242,12 +237,7 @@ describe("generation", function() {
"raw mappings were incorrectly generated", "raw mappings were incorrectly generated",
); );
chai expect(generated.code).toBe("function foo2() {\n bar2;\n}");
.expect(generated.code)
.to.equal(
"function foo2() {\n bar2;\n}",
"code was incorrectly generated",
);
}); });
it("lazy source map generation", function() { it("lazy source map generation", function() {
@ -259,14 +249,14 @@ describe("generation", function() {
sourceMaps: true, sourceMaps: true,
}); });
chai.expect(generated.rawMappings).to.be.an("array"); expect(Array.isArray(generated.rawMappings)).toBe(true);
chai expect(
.expect(generated) Object.getOwnPropertyDescriptor(generated, "map"),
.ownPropertyDescriptor("map") ).not.toHaveProperty("value");
.not.to.have.property("value");
chai.expect(generated.map).to.be.an("object"); expect(generated).toHaveProperty("map");
expect(typeof generated.map).toBe("object");
}); });
}); });
@ -294,7 +284,7 @@ describe("programmatic generation", function() {
); );
const ast = parse(generate(ifStatement).code); const ast = parse(generate(ifStatement).code);
assert.equal(ast.program.body[0].consequent.type, "BlockStatement"); expect(ast.program.body[0].consequent.type).toBe("BlockStatement");
}); });
it("prints directives in block with empty body", function() { it("prints directives in block with empty body", function() {
@ -304,12 +294,9 @@ describe("programmatic generation", function() {
); );
const output = generate(blockStatement).code; const output = generate(blockStatement).code;
assert.equal( expect(output).toBe(`{
output,
`{
"use strict"; "use strict";
}`, }`);
);
}); });
it("flow object indentation", function() { it("flow object indentation", function() {
@ -320,12 +307,9 @@ describe("programmatic generation", function() {
); );
const output = generate(objectStatement).code; const output = generate(objectStatement).code;
assert.equal( expect(output).toBe(`{
output,
`{
bar: string bar: string
}`, }`);
);
}); });
it("flow object exact", function() { it("flow object exact", function() {
@ -337,12 +321,9 @@ describe("programmatic generation", function() {
); );
const output = generate(objectStatement).code; const output = generate(objectStatement).code;
assert.equal( expect(output).toBe(`{|
output,
`{|
bar: string bar: string
|}`, |}`);
);
}); });
it("flow object indentation with empty leading ObjectTypeProperty", function() { it("flow object indentation with empty leading ObjectTypeProperty", function() {
@ -359,12 +340,9 @@ describe("programmatic generation", function() {
const output = generate(objectStatement).code; const output = generate(objectStatement).code;
assert.equal( expect(output).toBe(`{
output,
`{
[key: any]: number [key: any]: number
}`, }`);
);
}); });
}); });
@ -372,7 +350,7 @@ describe("CodeGenerator", function() {
it("generate", function() { it("generate", function() {
const codeGen = new CodeGenerator(t.numericLiteral(123)); const codeGen = new CodeGenerator(t.numericLiteral(123));
const code = codeGen.generate().code; const code = codeGen.generate().code;
assert.equal(parse(code).program.body[0].expression.value, 123); expect(parse(code).program.body[0].expression.value).toBe(123);
}); });
}); });
@ -385,7 +363,7 @@ suites.forEach(function(testSuite) {
task.title, task.title,
!task.disabled && !task.disabled &&
function() { function() {
const expect = task.expect; const expected = task.expect;
const actual = task.actual; const actual = task.actual;
const actualCode = actual.code; const actualCode = actual.code;
@ -399,17 +377,15 @@ suites.forEach(function(testSuite) {
const result = generate(actualAst, task.options, actualCode); const result = generate(actualAst, task.options, actualCode);
if ( if (
!expect.code && !expected.code &&
result.code && result.code &&
fs.statSync(path.dirname(expect.loc)).isDirectory() && fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI !process.env.CI
) { ) {
console.log(`New test file created: ${expect.loc}`); console.log(`New test file created: ${expected.loc}`);
fs.writeFileSync(expect.loc, result.code); fs.writeFileSync(expected.loc, result.code);
} else { } else {
chai expect(result.code).toBe(expected.code);
.expect(result.code)
.to.be.equal(expect.code, actual.loc + " !== " + expect.loc);
} }
} }
}, },

View File

@ -7,4 +7,4 @@ class Example {
} }
expect(Example).toHaveProperty("prop"); expect(Example).toHaveProperty("prop");
expect(Example.prop).toBe(undefined); expect(Example.prop).toBeUndefined();

View File

@ -11,10 +11,10 @@ var obj = {
}, },
}; };
assert.equal(obj.x ??= 1, 1); expect(obj.x ??= 1).toBe(1);
assert.equal(sets, 1); expect(sets).toBe(1);
assert.equal(obj.x ??= 2, 1); expect(obj.x ??= 2).toBe(1);
assert.equal(sets, 1); expect(sets).toBe(1);
var gets = 0; var gets = 0;
var deep = { var deep = {
@ -25,25 +25,25 @@ var deep = {
}; };
obj.x = undefined; obj.x = undefined;
assert.equal(deep.obj.x ??= 1, 1); expect(deep.obj.x ??= 1).toBe(1);
assert.equal(gets, 1); expect(gets).toBe(1);
assert.equal(deep.obj.x ??= 2, 1); expect(deep.obj.x ??= 2).toBe(1);
assert.equal(gets, 2); expect(gets).toBe(2);
var key = 0; var key = 0;
obj.x = undefined; obj.x = undefined;
assert.equal(obj[++key] ??= 1, 1); expect(obj[++key] ??= 1).toBe(1);
assert.equal(key, 1); expect(key).toBe(1);
key = 0; key = 0;
assert.equal(obj[++key] ??= 2, 1); expect(obj[++key] ??= 2).toBe(1);
assert.equal(key, 1); expect(key).toBe(1);
obj.x = undefined; obj.x = undefined;
key = 0; key = 0;
assert.equal(deep.obj[++key] ??= 1, 1); expect(deep.obj[++key] ??= 1).toBe(1);
assert.equal(gets, 3); expect(gets).toBe(3);
assert.equal(key, 1); expect(key).toBe(1);
key = 0; key = 0;
assert.equal(deep.obj[++key] ??= 2, 1); expect(deep.obj[++key] ??= 2).toBe(1);
assert.equal(gets, 4); expect(gets).toBe(4);
assert.equal(key, 1); expect(key).toBe(1);

View File

@ -11,10 +11,10 @@ var obj = {
}, },
}; };
assert.equal(obj.x ??= 1, 1); expect(obj.x ??= 1).toBe(1);
assert.equal(sets, 1); expect(sets, 1);
assert.equal(obj.x ??= 2, 1); expect(obj.x ??= 2).toBe(1);
assert.equal(sets, 1); expect(sets, 1);
var gets = 0; var gets = 0;
var deep = { var deep = {
@ -25,25 +25,25 @@ var deep = {
}; };
obj.x = undefined; obj.x = undefined;
assert.equal(deep.obj.x ??= 1, 1); expect(deep.obj.x ??= 1).toBe(1);
assert.equal(gets, 1); expect(gets, 1);
assert.equal(deep.obj.x ??= 2, 1); expect(deep.obj.x ??= 2).toBe(1);
assert.equal(gets, 2); expect(gets, 2);
var key = 0; var key = 0;
obj.x = undefined; obj.x = undefined;
assert.equal(obj[++key] ??= 1, 1); expect(obj[++key] ??= 1).toBe(1);
assert.equal(key, 1); expect(key, 1);
key = 0; key = 0;
assert.equal(obj[++key] ??= 2, 1); expect(obj[++key] ??= 2).toBe(1);
assert.equal(key, 1); expect(key, 1);
obj.x = undefined; obj.x = undefined;
key = 0; key = 0;
assert.equal(deep.obj[++key] ??= 1, 1); expect(deep.obj[++key] ??= 1).toBe(1);
assert.equal(gets, 3); expect(gets, 3);
assert.equal(key, 1); expect(key, 1);
key = 0; key = 0;
assert.equal(deep.obj[++key] ??= 2, 1); expect(deep.obj[++key] ??= 2).toBe(1);
assert.equal(gets, 4); expect(gets, 4);
assert.equal(key, 1); expect(key, 1);

View File

@ -13,10 +13,10 @@ var obj = {
} }
}; };
assert.equal((_obj$x = obj.x) !== null && _obj$x !== void 0 ? _obj$x : obj.x = 1, 1); expect((_obj$x = obj.x) !== null && _obj$x !== void 0 ? _obj$x : obj.x = 1).toBe(1);
assert.equal(sets, 1); expect(sets, 1);
assert.equal((_obj$x2 = obj.x) !== null && _obj$x2 !== void 0 ? _obj$x2 : obj.x = 2, 1); expect((_obj$x2 = obj.x) !== null && _obj$x2 !== void 0 ? _obj$x2 : obj.x = 2).toBe(1);
assert.equal(sets, 1); expect(sets, 1);
var gets = 0; var gets = 0;
var deep = { var deep = {
get obj() { get obj() {
@ -26,23 +26,23 @@ var deep = {
}; };
obj.x = undefined; obj.x = undefined;
assert.equal((_x = (_deep$obj = deep.obj).x) !== null && _x !== void 0 ? _x : _deep$obj.x = 1, 1); expect((_x = (_deep$obj = deep.obj).x) !== null && _x !== void 0 ? _x : _deep$obj.x = 1).toBe(1);
assert.equal(gets, 1); expect(gets, 1);
assert.equal((_x2 = (_deep$obj2 = deep.obj).x) !== null && _x2 !== void 0 ? _x2 : _deep$obj2.x = 2, 1); expect((_x2 = (_deep$obj2 = deep.obj).x) !== null && _x2 !== void 0 ? _x2 : _deep$obj2.x = 2).toBe(1);
assert.equal(gets, 2); expect(gets, 2);
var key = 0; var key = 0;
obj.x = undefined; obj.x = undefined;
assert.equal((_obj = obj[_ref = ++key]) !== null && _obj !== void 0 ? _obj : obj[_ref] = 1, 1); expect((_obj = obj[_ref = ++key]) !== null && _obj !== void 0 ? _obj : obj[_ref] = 1).toBe(1);
assert.equal(key, 1); expect(key, 1);
key = 0; key = 0;
assert.equal((_obj2 = obj[_ref2 = ++key]) !== null && _obj2 !== void 0 ? _obj2 : obj[_ref2] = 2, 1); expect((_obj2 = obj[_ref2 = ++key]) !== null && _obj2 !== void 0 ? _obj2 : obj[_ref2] = 2).toBe(1);
assert.equal(key, 1); expect(key, 1);
obj.x = undefined; obj.x = undefined;
key = 0; key = 0;
assert.equal((_ref4 = (_deep$obj3 = deep.obj)[_ref3 = ++key]) !== null && _ref4 !== void 0 ? _ref4 : _deep$obj3[_ref3] = 1, 1); expect((_ref4 = (_deep$obj3 = deep.obj)[_ref3 = ++key]) !== null && _ref4 !== void 0 ? _ref4 : _deep$obj3[_ref3] = 1).toBe(1);
assert.equal(gets, 3); expect(gets, 3);
assert.equal(key, 1); expect(key, 1);
key = 0; key = 0;
assert.equal((_ref6 = (_deep$obj4 = deep.obj)[_ref5 = ++key]) !== null && _ref6 !== void 0 ? _ref6 : _deep$obj4[_ref5] = 2, 1); expect((_ref6 = (_deep$obj4 = deep.obj)[_ref5 = ++key]) !== null && _ref6 !== void 0 ? _ref6 : _deep$obj4[_ref5] = 2).toBe(1);
assert.equal(gets, 4); expect(gets, 4);
assert.equal(key, 1); expect(key, 1);

View File

@ -8,7 +8,7 @@ expect(c).toBeInstanceOf(Object);
expect(Object.getPrototypeOf(c)).toBe(C.prototype); expect(Object.getPrototypeOf(c)).toBe(C.prototype);
expect(Object.getPrototypeOf(Object.getPrototypeOf(c))).toBeNull(); expect(Object.getPrototypeOf(Object.getPrototypeOf(c))).toBeNull();
expect(c.toString).toBe(undefined); expect(c.toString).toBeUndefined();
class D extends null { class D extends null {
constructor(...args) { constructor(...args) {