Skip to content

Commit

Permalink
Migrate a few packages' tests to use Jest Expect (see below)
Browse files Browse the repository at this point in the history
* Migrate the following packages' tests:
    * babel-helper-annotate-as-pure
    * babel-helper-module-imports
    * babel-helper-transform-fixture-test-runner
    * babel-highlight
    * babel-node
    * babel-plugin-transform-modules-commonjs
    * babel-preset-env-standalone
    * babel-preset-env
    * babel-preset-es2015
    * babel-preset-react
    * babel-standalone
    * babel-template
    * babel-traverse
    * babel-types
  • Loading branch information
devenbansod committed Mar 16, 2018
1 parent 82994ce commit e3cab74
Show file tree
Hide file tree
Showing 33 changed files with 597 additions and 850 deletions.
5 changes: 2 additions & 3 deletions packages/babel-helper-annotate-as-pure/test/index.js
@@ -1,12 +1,11 @@
import annotateAsPure from "../";
import assert from "assert";

describe("@babel/helper-annotate-as-pure", () => {
it("will add leading comment", () => {
const node = {};
annotateAsPure(node);

assert.deepEqual(node.leadingComments, [
expect(node.leadingComments).toEqual([
{
type: "CommentBlock",
value: "#__PURE__",
Expand All @@ -26,7 +25,7 @@ describe("@babel/helper-annotate-as-pure", () => {

annotateAsPure(node);

assert.deepEqual(node.leadingComments, [
expect(node.leadingComments).toEqual([
{
type: "CommentBlock",
value: "#__PURE__",
Expand Down
53 changes: 21 additions & 32 deletions packages/babel-helper-module-imports/test/index.js
@@ -1,4 +1,3 @@
import chai from "chai";
import * as babel from "@babel/core";

import { ImportInjector } from "../";
Expand Down Expand Up @@ -33,9 +32,9 @@ function test(sourceType, opts, initializer, expectedCode) {
],
});

chai
.expect(result.code.replace(/\s+/g, " ").trim())
.to.equal((expectedCode || "").replace(/\s+/g, " ").trim());
expect(result.code.replace(/\s+/g, " ").trim()).toBe(
(expectedCode || "").replace(/\s+/g, " ").trim(),
);
}
const testScript = test.bind(undefined, "script");
const testModule = test.bind(undefined, "module");
Expand Down Expand Up @@ -90,11 +89,9 @@ describe("@babel/helper-module-imports", () => {

describe("using a CommonJS loader", () => {
it("should import", () => {
chai
.expect(() => {
testScript({ importedType }, addNamespace());
})
.to.throw(Error, "Cannot import an ES6 module from CommonJS");
expect(() => {
testScript({ importedType }, addNamespace());
}).toThrow("Cannot import an ES6 module from CommonJS");
});
});
});
Expand Down Expand Up @@ -302,11 +299,9 @@ describe("@babel/helper-module-imports", () => {

describe("using a CommonJS loader", () => {
it("should import", () => {
chai
.expect(() => {
testScript({ importedType }, addDefault());
})
.to.throw(Error, "Cannot import an ES6 module from CommonJS");
expect(() => {
testScript({ importedType }, addDefault());
}).toThrow("Cannot import an ES6 module from CommonJS");
});
});
});
Expand Down Expand Up @@ -390,14 +385,12 @@ describe("@babel/helper-module-imports", () => {
});

it("should fail to import with force-enabled liveness", () => {
chai
.expect(() => {
testScript(
{ importedInterop, ensureLiveReference: true },
addDefault(),
);
})
.to.throw(Error, "No live reference for commonjs default");
expect(() => {
testScript(
{ importedInterop, ensureLiveReference: true },
addDefault(),
);
}).toThrow("No live reference for commonjs default");
});
});
});
Expand Down Expand Up @@ -659,11 +652,9 @@ describe("@babel/helper-module-imports", () => {

describe("using a CommonJS loader", () => {
it("should import", () => {
chai
.expect(() => {
testScript({ importedType }, addNamed());
})
.to.throw(Error, "Cannot import an ES6 module from CommonJS");
expect(() => {
testScript({ importedType }, addNamed());
}).toThrow("Cannot import an ES6 module from CommonJS");
});
});
});
Expand Down Expand Up @@ -967,11 +958,9 @@ describe("@babel/helper-module-imports", () => {

describe("using a CommonJS loader", () => {
it("should import", () => {
chai
.expect(() => {
testScript({ importedType }, addSideEffect());
})
.to.throw(Error, "Cannot import an ES6 module from CommonJS");
expect(() => {
testScript({ importedType }, addSideEffect());
}).toThrow("Cannot import an ES6 module from CommonJS");
});
});
});
Expand Down
@@ -1,18 +1,17 @@
import assert from "assert";
import { runCodeInTestContext } from "..";

describe("helper-transform-fixture-test-runner", function() {
it("should not execute code in Node's global context", function() {
try {
global.foo = "outer";
runCodeInTestContext(`
assert.equal(global.foo, undefined);
expect(global.foo).toBeUndefined();
global.foo = "inner";
`);

assert.equal(global.foo, "outer");
expect(global.foo).toBe("outer");
runCodeInTestContext(`
assert.equal(global.foo, "inner");
expect(global.foo).toBe("inner");
`);
} finally {
delete global.foo;
Expand Down
29 changes: 13 additions & 16 deletions packages/babel-highlight/test/index.js
@@ -1,4 +1,3 @@
import assert from "assert";
import chalk from "chalk";
import stripAnsi from "strip-ansi";
import highlight, { shouldHighlight, getChalk } from "..";
Expand All @@ -24,8 +23,8 @@ describe("@babel/highlight", function() {
const code = "console.log('hi')";
const result = highlight(code);
const stripped = stripAnsi(result);
assert.ok(result.length > stripped.length);
assert.equal(stripped, code);
expect(result.length).toBeGreaterThan(stripped.length);
expect(stripped).toBe(code);
});
});

Expand All @@ -36,17 +35,17 @@ describe("@babel/highlight", function() {
const code = "console.log('hi')";
const result = highlight(code);
const stripped = stripAnsi(result);
assert.ok(result.length === stripped.length);
assert.equal(result, code);
expect(result.length).toBe(stripped.length);
expect(result).toBe(code);
});

describe("and the forceColor option is passed", function() {
it("highlights the code anyway", function() {
const code = "console.log('hi')";
const result = highlight(code, { forceColor: true });
const stripped = stripAnsi(result);
assert.ok(result.length > stripped.length);
assert.equal(stripped, code);
expect(result.length).toBeGreaterThan(stripped.length);
expect(stripped).toBe(code);
});
});
});
Expand All @@ -57,20 +56,20 @@ describe("@babel/highlight", function() {
stubColorSupport(true);

it("returns true", function() {
assert.ok(shouldHighlight({}));
expect(shouldHighlight({})).toBeTruthy();
});
});

describe("when colors are not supported", function() {
stubColorSupport(false);

it("returns false", function() {
assert.ok(!shouldHighlight({}));
expect(shouldHighlight({})).toBeFalsy();
});

describe("and the forceColor option is passed", function() {
it("returns true", function() {
assert.ok(shouldHighlight({ forceColor: true }));
expect(shouldHighlight({ forceColor: true })).toBeTruthy();
});
});
});
Expand All @@ -82,14 +81,13 @@ describe("@babel/highlight", function() {

describe("when forceColor is not passed", function() {
it("returns a Chalk instance", function() {
assert.equal(getChalk({}).constructor, chalk.constructor);
expect(getChalk({}).constructor).toBe(chalk.constructor);
});
});

describe("when forceColor is passed", function() {
it("returns a Chalk instance", function() {
assert.equal(
getChalk({ forceColor: true }).constructor,
expect(getChalk({ forceColor: true }).constructor).toBe(
chalk.constructor,
);
});
Expand All @@ -101,14 +99,13 @@ describe("@babel/highlight", function() {

describe("when forceColor is not passed", function() {
it("returns a Chalk instance", function() {
assert.equal(getChalk({}).constructor, chalk.constructor);
expect(getChalk({}).constructor).toBe(chalk.constructor);
});
});

describe("when forceColor is passed", function() {
it("returns a Chalk instance", function() {
assert.equal(
getChalk({ forceColor: true }).constructor,
expect(getChalk({ forceColor: true }).constructor).toBe(
chalk.constructor,
);
});
Expand Down
37 changes: 9 additions & 28 deletions packages/babel-node/test/index.js
@@ -1,13 +1,11 @@
const includes = require("lodash/includes");
const readdir = require("fs-readdir-recursive");
const helper = require("@babel/helper-fixtures");
const assert = require("assert");
const rimraf = require("rimraf");
const outputFileSync = require("output-file-sync");
const child = require("child_process");
const merge = require("lodash/merge");
const path = require("path");
const chai = require("chai");
const fs = require("fs");

const fixtureLoc = path.join(__dirname, "fixtures");
Expand Down Expand Up @@ -53,15 +51,9 @@ const assertTest = function(stdout, stderr, opts) {

if (opts.stderr) {
if (opts.stderrContains) {
assert.ok(
includes(stderr, expectStderr),
"stderr " +
JSON.stringify(stderr) +
" didn't contain " +
JSON.stringify(expectStderr),
);
expect(includes(stderr, expectStderr)).toBeTruthy();
} else {
chai.expect(stderr).to.equal(expectStderr, "stderr didn't match");
expect(stderr).toBe(expectStderr);
}
} else if (stderr) {
throw new Error("stderr:\n" + stderr);
Expand All @@ -73,15 +65,9 @@ const assertTest = function(stdout, stderr, opts) {

if (opts.stdout) {
if (opts.stdoutContains) {
assert.ok(
includes(stdout, expectStdout),
"stdout " +
JSON.stringify(stdout) +
" didn't contain " +
JSON.stringify(expectStdout),
);
expect(includes(stdout, expectStdout)).toBeTruthy();
} else {
chai.expect(stdout).to.equal(expectStdout, "stdout didn't match");
expect(stdout).toBe(expectStdout);
}
} else if (stdout) {
throw new Error("stdout:\n" + stdout);
Expand All @@ -92,24 +78,19 @@ const assertTest = function(stdout, stderr, opts) {

Object.keys(actualFiles).forEach(function(filename) {
if (!opts.inFiles.hasOwnProperty(filename)) {
const expect = opts.outFiles[filename];
const expected = opts.outFiles[filename];
const actual = actualFiles[filename];

chai.expect(expect, "Output is missing: " + filename).to.not.be
.undefined;
expect(expected).not.toBeUndefined();

if (expect) {
chai
.expect(actual)
.to.equal(expect, "Compiled output does not match: " + filename);
if (expected) {
expect(actual).toBe(expected);
}
}
});

Object.keys(opts.outFiles).forEach(function(filename) {
chai
.expect(actualFiles, "Extraneous file in output: " + filename)
.to.contain.key(filename);
expect(actualFiles).toHaveProperty(filename);
});
}
};
Expand Down
@@ -1,4 +1,3 @@
const assert = require("assert");
const babel = require("@babel/core");

test("Doesn't use the same object for two different nodes in the AST", function() {
Expand All @@ -9,26 +8,20 @@ test("Doesn't use the same object for two different nodes in the AST", function(
plugins: [[require("../"), { loose: true }]],
}).ast;

assert.equal(ast.program.body[0].declarations[0].id.type, "Identifier");
assert.equal(ast.program.body[2].expression.type, "MemberExpression");
assert.equal(ast.program.body[2].expression.object.type, "Identifier");
assert.equal(ast.program.body[3].expression.type, "MemberExpression");
assert.equal(ast.program.body[3].expression.object.type, "Identifier");
expect(ast.program.body[0].declarations[0].id.type).toBe("Identifier");
expect(ast.program.body[2].expression.type).toBe("MemberExpression");
expect(ast.program.body[2].expression.object.type).toBe("Identifier");
expect(ast.program.body[3].expression.type).toBe("MemberExpression");
expect(ast.program.body[3].expression.object.type).toBe("Identifier");

assert.notStrictEqual(
ast.program.body[2].expression.object,
expect(ast.program.body[2].expression.object).not.toBe(
ast.program.body[3].expression.object,
"Expected different nodes in the AST to not be the same object (one)",
);

assert.notStrictEqual(
ast.program.body[0].declarations[0].id,
expect(ast.program.body[0].declarations[0].id).not.toBe(
ast.program.body[3].expression.object,
"Expected different nodes in the AST to not be the same object (two)",
);
assert.notStrictEqual(
ast.program.body[0].declarations[0].id,
expect(ast.program.body[0].declarations[0].id).not.toBe(
ast.program.body[2].expression.object,
"Expected different nodes in the AST to not be the same object (three)",
);
});
@@ -1,4 +1,3 @@
const assert = require("assert");
const babel = require("@babel/core");
const vm = require("vm");

Expand Down Expand Up @@ -27,9 +26,5 @@ test("Re-export doesn't overwrite __esModule flag", function() {
vm.runInNewContext(code, context);

// exports.__esModule shouldn't be overwritten.
assert.equal(
context.exports.__esModule,
true,
"Expected exports.__esModule === true",
);
expect(context.exports.__esModule).toBe(true);
});

0 comments on commit e3cab74

Please sign in to comment.