Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Dec 15, 2016
1 parent aa63ab4 commit b761f62
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 101 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
"eslint-config-babel": "^3.0.0",
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flowtype": "^2.25.0",
"mock-fs": "^3.12.1",
"nyc": "^10.0.0",
"rimraf": "^2.4.3",
"webpack": "^2.1.0-beta.22"
"webpack": "^2.2.0-rc"
},
"scripts": {
"clean": "rimraf lib/",
Expand Down
142 changes: 141 additions & 1 deletion test/cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from "path";
import assign from "object-assign";
import rimraf from "rimraf";
import webpack from "webpack";
import mock from "mock-fs";
import createTestDirectory from "./helpers/createTestDirectory";

const defaultCacheDir = path.join(__dirname, "../node_modules/.cache/babel-loader");
Expand Down Expand Up @@ -206,7 +207,6 @@ test.cb("should have one file per module", (t) => {
});
});


test.cb("should generate a new file if the identifier changes", (t) => {
const configs = [
assign({}, globalConfig, {
Expand Down Expand Up @@ -320,3 +320,143 @@ test.cb("should allow to specify the .babelrc file", (t) => {
});
});
});

// TODO all the following tests should be done without webpack, as the combination
// of mock-fs and webpack seems to be not reliable.

test.cb.serial("has error if custom cache directory cannot be created", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
query: {
cacheDirectory: t.context.cacheDirectory,
presets: [],
babelrc: false,
},
},
],
},
});

mock({
[path.dirname(t.context.cacheDirectory)]: mock.directory({
mode: 0o444,
})
});
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.hasErrors(), true);
t.regex(stats.toJson().errors[0], /EACCES, permission denied/);
mock.restore();
t.end();
});
});

test.cb.serial("falls back to tmp dir if default dir is not writable", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: [],
babelrc: false,
},
},
],
},
});

mock({
[path.join(__dirname, "../package.json")]: mock.file(),
[path.dirname(defaultCacheDir)]: mock.directory({
mode: 0o444,
})
});
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.hasErrors(), false);
mock.restore();
t.end();
});
});

test.cb.serial("falls back to tmp dir if no cache dir found", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: [],
babelrc: false,
},
},
],
},
});

mock({
[path.dirname(defaultCacheDir)]: mock.directory({
mode: 0o444,
})
});
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.hasErrors(), false);
mock.restore();
t.end();
});
});


test.cb.serial("reports errors from babel transpile", (t) => {
const config = assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
loaders: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
query: {
cacheDirectory: true,
presets: ["es2015"],
},
},
],
},
});

mock();
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.hasErrors(), true);
t.regex(stats.toJson().errors[0], /ENOENT, no such file or directory/);
t.regex(stats.toJson().errors[0], /\.babelrc/);
mock.restore();
t.end();
});
});

6 changes: 5 additions & 1 deletion test/helpers/createTestDirectory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import mkdirp from "mkdirp";
import rimraf from "rimraf";

export default function createTestDirectory(baseDirectory, testTitle, cb) {
const directory = path.join(baseDirectory, testTitle.replace(/[\/?<>\\:*|"\s]/g, "_"));
const directory = path.join(baseDirectory, escapeDirectory(testTitle));

rimraf(directory, (err) => {
if (err) return cb(err);
mkdirp(directory, (mkdirErr) => cb(mkdirErr, directory));
});
}

function escapeDirectory(directory) {
return directory.replace(/[\/?<>\\:*|"\s]/g, "_");
}
Loading

0 comments on commit b761f62

Please sign in to comment.