Skip to content

Commit

Permalink
Migrate to jest (#7455)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Mar 3, 2018
1 parent 53208d6 commit 3e95830
Show file tree
Hide file tree
Showing 29 changed files with 1,853 additions and 1,125 deletions.
21 changes: 13 additions & 8 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
"use strict";

let envOpts = {
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
const envOpts = {
loose: true,
};

switch(env) {
case "development":
envOpts.debug = true;
// fall-through
case "test":
envOpts.targets = {
node: "current"
};
}

const config = {
comments: false,
presets: [["@babel/env", envOpts]],
Expand All @@ -27,16 +38,10 @@ const config = {
],
};

// we need to do this as long as we do not test everything from source
if (process.env.BABEL_ENV === "cov") {
config.auxiliaryCommentBefore = "istanbul ignore next";
config.plugins.push("babel-plugin-istanbul");
}

if (process.env.BABEL_ENV === "development") {
envOpts.targets = {
node: "current",
};
envOpts.debug = true;
}

module.exports = config;
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ jobs:
# test-ci-coverage doesn't test babel-standalone, as trying to gather coverage
# data for a JS file that's several megabytes large is bound to fail. Here,
# we just run the babel-standalone test separately.
- run: ./node_modules/mocha/bin/_mocha packages/babel-standalone/test/ --opts test/mocha.opts
- run: ./node_modules/mocha/bin/_mocha packages/babel-preset-env-standalone/test/ --opts test/mocha.opts
- run: ./node_modules/.bin/jest packages/babel-standalone/test/
- run: ./node_modules/.bin/jest packages/babel-preset-env-standalone/test/
- store_artifacts: *artifact_babel
- store_artifacts: *artifact_babel_min
- store_artifacts: *artifact_env
Expand Down
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"test/**/*.js"
],
"env": {
"mocha": true
"jest": true
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cache:
directories:
- node_modules
node_js:
- '9'
- '8'
- '6'
- '4'
Expand All @@ -19,6 +20,8 @@ env:
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash

install: yarn --ignore-engines

before_script:
- 'if [ "$JOB" = "babylon-flow-tests" ]; then make bootstrap-flow; fi'
- 'if [ "$JOB" = "babylon-test262-tests" ]; then make bootstrap-test262; fi'
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test-ci:
test-ci-coverage: SHELL:=/bin/bash
test-ci-coverage:
BABEL_ENV=cov make bootstrap
./scripts/test-cov.sh
TEST_TYPE=cov ./scripts/test-cov.sh
bash <(curl -s https://codecov.io/bash) -f coverage/coverage-final.json

bootstrap-flow:
Expand Down Expand Up @@ -130,8 +130,8 @@ publish:

bootstrap:
make clean-all
yarn
./node_modules/.bin/lerna bootstrap
yarn --ignore-engines
./node_modules/.bin/lerna bootstrap -- --ignore-engines
make build
cd packages/babel-runtime; \
node scripts/build-dist.js
Expand Down
52 changes: 38 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
"@babel/preset-flow": "7.0.0-beta.40",
"@babel/preset-stage-0": "7.0.0-beta.40",
"@babel/register": "7.0.0-beta.40",
"babel-core": "^7.0.0-0",
"babel-eslint": "^8.0.1",
"babel-jest": "^22.4.1",
"babel-loader": "8.0.0-beta.0",
"babel-plugin-istanbul": "^4.1.5",
"babel-plugin-transform-charcodes": "^0.1.0",
"babylon": "7.0.0-beta.40",
"browserify": "^13.1.1",
Expand All @@ -44,13 +45,12 @@
"gulp-util": "^3.0.7",
"gulp-watch": "^4.3.5",
"husky": "^0.14.3",
"jest": "^22.4.2",
"lerna": "2.0.0",
"lerna-changelog": "^0.5.0",
"lint-staged": "^6.0.1",
"lodash": "^4.2.0",
"merge-stream": "^1.0.1",
"mocha": "^3.0.0",
"nyc": "^11.0.3",
"output-file-sync": "^2.0.0",
"prettier": "1.10.2",
"pump": "^1.0.2",
Expand All @@ -71,20 +71,44 @@
"npm": ">= 2.x <= 5.x",
"yarn": ">=0.27.5 || >=1.0.0-20170811"
},
"nyc": {
"all": true,
"exclude": [
"scripts/*.js",
"packages/*/test/**",
"packages/babel-standalone/**",
"codemods/*/test/**"
],
"sourceMap": false,
"instrument": false
},
"lint-staged": {
"*.js": [
"eslint --format=codeframe --rulesdir='./scripts/eslint_rules'"
]
},
"jest": {
"collectCoverageFrom": [
"packages/*/src/**/*.js",
"codemods/*/src/**/*.js"
],
"testRegex": "./(packages|codemods)/[^/]+/test/.+\\.js$",
"testPathIgnorePatterns": [
"/node_modules/",
"/test/fixtures/",
"/test/debug-fixtures/",
"/babylon/test/expressions/",
"/test/tmp/",
"/test/__data__/",
"/test/helpers/",
"<rootDir>/test/warning.js",
"<rootDir>/build/",
"_browser.js"
],
"testEnvironment": "node",
"setupTestFrameworkScriptFile": "<rootDir>/test/testSetupFile.js",
"transformIgnorePatterns": [
"/node_modules/",
"/test/fixtures/",
"/test/tmp/",
"/test/__data__/",
"/lib/"
],
"modulePaths": [
"<rootDir>/packages/"
],
"modulePathIgnorePatterns": [
"/test/fixtures/",
"<rootDir>/build/"
]
}
}
16 changes: 8 additions & 8 deletions packages/babel-cli/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ const buildTest = function(binName, testName, opts) {
const binLoc = path.join(__dirname, "../lib", binName);

return function(callback) {
clear();
const dir = process.cwd();

process.chdir(__dirname);
if (fs.existsSync(tmpLoc)) rimraf.sync(tmpLoc);
fs.mkdirSync(tmpLoc);
process.chdir(tmpLoc);

saveInFiles(opts.inFiles);

let args = [binLoc];
Expand Down Expand Up @@ -160,6 +166,7 @@ const buildTest = function(binName, testName, opts) {
args.map(arg => `"${arg}"`).join(" ") + ": " + err.message;
}

process.chdir(dir);
callback(err);
});

Expand All @@ -170,13 +177,6 @@ const buildTest = function(binName, testName, opts) {
};
};

const clear = function() {
process.chdir(__dirname);
if (fs.existsSync(tmpLoc)) rimraf.sync(tmpLoc);
fs.mkdirSync(tmpLoc);
process.chdir(tmpLoc);
};

fs.readdirSync(fixtureLoc).forEach(function(binName) {
if (binName[0] === ".") return;

Expand Down
14 changes: 8 additions & 6 deletions packages/babel-code-frame/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,17 @@ export default function(
if (!deprecationWarningShown) {
deprecationWarningShown = true;

const deprecationError = new Error(
"Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.",
);
deprecationError.name = "DeprecationWarning";
const message =
"Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";

if (process.emitWarning) {
process.emitWarning(deprecationError);
// A string is directly supplied to emitWarning, because when supplying an
// Error object node throws in the tests because of different contexts
process.emitWarning(message, "DeprecationWarning");
} else {
console.warn(deprecationError);
const deprecationError = new Error(message);
deprecationError.name = "DeprecationWarning";
console.warn(new Error(message));
}
}

Expand Down
13 changes: 11 additions & 2 deletions packages/babel-core/test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ describe("api", function() {
return { plugins: [pushPlugin(str)] };
}

const oldEnv = process.env.BABEL_ENV;
process.env.BABEL_ENV = "development";

const result = babel.transform("", {
filename: path.join(
__dirname,
Expand All @@ -316,6 +319,12 @@ describe("api", function() {
},
});

if (oldEnv === undefined) {
delete process.env.BABEL_ENV;
} else {
process.env.BABEL_ENV = oldEnv;
}

assert.equal(
result.code,
[
Expand Down Expand Up @@ -535,14 +544,14 @@ describe("api", function() {
const oldBabelEnv = process.env.BABEL_ENV;
const oldNodeEnv = process.env.NODE_ENV;

setup(function() {
beforeEach(function() {
// Tests need to run with the default and specific values for these. They
// need to be cleared for each test.
delete process.env.BABEL_ENV;
delete process.env.NODE_ENV;
});

suiteTeardown(function() {
afterAll(function() {
process.env.BABEL_ENV = oldBabelEnv;
process.env.NODE_ENV = oldNodeEnv;
});
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-core/test/config-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,19 @@ describe("buildConfigChain", function() {
plugins: [],
presets: [],
});
const realEnv = process.env.NODE_ENV;
const realBabelEnv = process.env.BABEL_ENV;

beforeAll(() => {
delete process.env.NODE_ENV;
delete process.env.BABEL_ENV;
});
afterAll(() => {
if (realEnv) {
process.env.NODE_ENV = realEnv;
process.env.NODE_ENV = realBabelEnv;
}
});

it("should load .babelrc", () => {
const filename = fixture("config-files", "babelrc", "src.js");
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-core/test/resolution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import path from "path";

describe("addon resolution", function() {
const base = path.join(__dirname, "fixtures", "resolution");
let cwd;

beforeEach(function() {
this.cwd = process.cwd();
cwd = process.cwd();
process.chdir(base);
});

afterEach(function() {
process.chdir(this.cwd);
process.chdir(cwd);
});

it("should find module: presets", function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@babel/helper-fixtures": "7.0.0-beta.40",
"@babel/polyfill": "7.0.0-beta.40",
"chai": "^4.1.0",
"jest": "^22.4.2",
"lodash": "^4.2.0",
"resolve": "^1.3.2",
"source-map": "^0.5.0"
Expand Down
27 changes: 11 additions & 16 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-env mocha */
/* eslint-env jest */
import * as babel from "@babel/core";
import { buildExternalHelpers } from "@babel/core";
import getFixtures from "@babel/helper-fixtures";
Expand All @@ -11,7 +11,6 @@ import extend from "lodash/extend";
import merge from "lodash/merge";
import resolve from "resolve";
import assert from "assert";
import chai from "chai";
import fs from "fs";
import path from "path";
import vm from "vm";
Expand Down Expand Up @@ -321,7 +320,7 @@ function checkDuplicatedNodes(ast) {

function run(task) {
const actual = task.actual;
const expect = task.expect;
const expected = task.expect;
const exec = task.exec;
const opts = task.options;
const optionsDir = task.optionsDir;
Expand Down Expand Up @@ -376,29 +375,27 @@ function run(task) {
}

let actualCode = actual.code;
const expectCode = expect.code;
const expectCode = expected.code;
if (!execCode || actualCode) {
result = babel.transform(actualCode, getOpts(actual));
checkDuplicatedNodes(result.ast);
if (
!expect.code &&
!expected.code &&
result.code &&
!opts.throws &&
fs.statSync(path.dirname(expect.loc)).isDirectory() &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
console.log(`New test file created: ${expect.loc}`);
fs.writeFileSync(expect.loc, `${result.code}\n`);
console.log(`New test file created: ${expected.loc}`);
fs.writeFileSync(expected.loc, `${result.code}\n`);
} else {
actualCode = result.code.trim();
chai
.expect(actualCode)
.to.be.equal(expectCode, actual.loc + " !== " + expect.loc);
expect(actualCode).toEqual(expectCode);
}
}

if (task.sourceMap) {
chai.expect(result.map).to.deep.equal(task.sourceMap);
expect(result.map).toEqual(task.sourceMap);
}

if (task.sourceMappings) {
Expand All @@ -407,10 +404,8 @@ function run(task) {
task.sourceMappings.forEach(function(mapping) {
const actual = mapping.original;

const expect = consumer.originalPositionFor(mapping.generated);
chai
.expect({ line: expect.line, column: expect.column })
.to.deep.equal(actual);
const expected = consumer.originalPositionFor(mapping.generated);
expect({ line: expected.line, column: expected.column }).toEqual(actual);
});
}

Expand Down

0 comments on commit 3e95830

Please sign in to comment.