Skip to content

Commit db4f8a4

Browse files
Add tests
1 parent 6aa196c commit db4f8a4

16 files changed

Lines changed: 122 additions & 147 deletions

File tree

packages/babel-core/test/config-chain.js

Lines changed: 114 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "fs";
2+
import os from "os";
23
import path from "path";
34
import escapeRegExp from "lodash/escapeRegExp";
45
import { loadOptions as loadOptionsOrig } from "../lib";
@@ -14,6 +15,24 @@ function loadOptions(opts) {
1415
});
1516
}
1617

18+
function pairs(items) {
19+
const pairs = [];
20+
for (let i = 0; i < items.length - 1; i++) {
21+
for (let j = i + 1; j < items.length; j++) {
22+
pairs.push([items[i], items[j]]);
23+
}
24+
}
25+
return pairs;
26+
}
27+
28+
async function getTemp(name) {
29+
const cwd = await fs.promises.mkdtemp(os.tmpdir() + path.sep + name);
30+
const tmp = name => path.join(cwd, name);
31+
const config = name =>
32+
fs.promises.copyFile(fixture("config-files-templates", name), tmp(name));
33+
return { cwd, tmp, config };
34+
}
35+
1736
describe("buildConfigChain", function() {
1837
describe("test", () => {
1938
describe("single", () => {
@@ -944,157 +963,120 @@ describe("buildConfigChain", function() {
944963
}
945964
});
946965

947-
it("should load babel.config.json", () => {
948-
const filename = fixture("config-files", "babel-config-json", "src.js");
966+
describe("root", () => {
967+
test.each(["babel.config.json", "babel.config.js", "babel.config.cjs"])(
968+
"should load %s",
969+
async name => {
970+
const { cwd, tmp, config } = await getTemp(
971+
`babel-test-load-config-${name}`,
972+
);
973+
const filename = tmp("src.js");
974+
975+
await config(name);
976+
977+
expect(
978+
loadOptions({
979+
filename,
980+
cwd,
981+
}),
982+
).toEqual({
983+
...getDefaults(),
984+
filename,
985+
cwd,
986+
root: cwd,
987+
comments: true,
988+
});
989+
},
990+
);
949991

950-
expect(
951-
loadOptions({
952-
filename,
953-
cwd: path.dirname(filename),
954-
}),
955-
).toEqual({
956-
...getDefaults(),
957-
filename: filename,
958-
cwd: path.dirname(filename),
959-
root: path.dirname(filename),
960-
comments: true,
961-
});
962-
});
992+
test.each(
993+
pairs(["babel.config.json", "babel.config.js", "babel.config.cjs"]),
994+
)("should throw if both %s and %s are used", async (name1, name2) => {
995+
const { cwd, tmp, config } = await getTemp(
996+
`babel-test-dup-config-${name1}-${name2}`,
997+
);
963998

964-
it("should load babel.config.js", () => {
965-
const filename = fixture("config-files", "babel-config-js", "src.js");
999+
await Promise.all([config(name1), config(name2)]);
9661000

967-
expect(
968-
loadOptions({
969-
filename,
970-
cwd: path.dirname(filename),
971-
}),
972-
).toEqual({
973-
...getDefaults(),
974-
filename: filename,
975-
cwd: path.dirname(filename),
976-
root: path.dirname(filename),
977-
comments: true,
1001+
expect(() => loadOptions({ filename: tmp("src.js"), cwd })).toThrow(
1002+
/Multiple configuration files found/,
1003+
);
9781004
});
9791005
});
9801006

981-
it("should whtow if both babel.config.json and babel.config.js are used", () => {
982-
const filename = fixture(
983-
"config-files",
984-
"babel-config-js-and-json",
985-
"src.js",
1007+
describe("relative", () => {
1008+
test.each(["package.json", ".babelrc", ".babelrc.js", ".babelrc.cjs"])(
1009+
"should load %s",
1010+
async name => {
1011+
const { cwd, tmp, config } = await getTemp(
1012+
`babel-test-load-config-${name}`,
1013+
);
1014+
const filename = tmp("src.js");
1015+
1016+
await config(name);
1017+
1018+
expect(
1019+
loadOptions({
1020+
filename,
1021+
cwd,
1022+
}),
1023+
).toEqual({
1024+
...getDefaults(),
1025+
filename,
1026+
cwd,
1027+
root: cwd,
1028+
comments: true,
1029+
});
1030+
},
9861031
);
9871032

988-
expect(() =>
989-
loadOptions({ filename, cwd: path.dirname(filename) }),
990-
).toThrow(/Multiple configuration files found/);
991-
});
1033+
it("should load .babelignore", () => {
1034+
const filename = fixture("config-files", "babelignore", "src.js");
9921035

993-
it("should load .babelrc", () => {
994-
const filename = fixture("config-files", "babelrc", "src.js");
995-
996-
expect(
997-
loadOptions({
998-
filename,
999-
cwd: path.dirname(filename),
1000-
}),
1001-
).toEqual({
1002-
...getDefaults(),
1003-
filename: filename,
1004-
cwd: path.dirname(filename),
1005-
root: path.dirname(filename),
1006-
comments: true,
1036+
expect(
1037+
loadOptions({ filename, cwd: path.dirname(filename) }),
1038+
).toBeNull();
10071039
});
1008-
});
1009-
1010-
it("should load .babelrc.js", () => {
1011-
const filename = fixture("config-files", "babelrc-js", "src.js");
10121040

1013-
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
1014-
...getDefaults(),
1015-
filename: filename,
1016-
cwd: path.dirname(filename),
1017-
root: path.dirname(filename),
1018-
comments: true,
1019-
});
1020-
});
1041+
test.each(
1042+
pairs(["package.json", ".babelrc", ".babelrc.js", ".babelrc.cjs"]),
1043+
)("should throw if both %s and %s are used", async (name1, name2) => {
1044+
const { cwd, tmp, config } = await getTemp(
1045+
`babel-test-dup-config-${name1}-${name2}`,
1046+
);
10211047

1022-
it("should load package.json#babel", () => {
1023-
const filename = fixture("config-files", "pkg", "src.js");
1048+
await Promise.all([config(name1), config(name2)]);
10241049

1025-
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
1026-
...getDefaults(),
1027-
filename: filename,
1028-
cwd: path.dirname(filename),
1029-
root: path.dirname(filename),
1030-
comments: true,
1050+
expect(() => loadOptions({ filename: tmp("src.js"), cwd })).toThrow(
1051+
/Multiple configuration files found/,
1052+
);
10311053
});
1032-
});
1033-
1034-
it("should load .babelignore", () => {
1035-
const filename = fixture("config-files", "babelignore", "src.js");
1036-
1037-
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toBeNull();
1038-
});
1039-
1040-
it("should throw if there are both .babelrc and .babelrc.js", () => {
1041-
const filename = fixture("config-files", "both-babelrc", "src.js");
1042-
1043-
expect(() =>
1044-
loadOptions({ filename, cwd: path.dirname(filename) }),
1045-
).toThrow(/Multiple configuration files found/);
1046-
});
1047-
1048-
it("should throw if there are both .babelrc and package.json", () => {
1049-
const filename = fixture("config-files", "pkg-babelrc", "src.js");
1050-
1051-
expect(() =>
1052-
loadOptions({ filename, cwd: path.dirname(filename) }),
1053-
).toThrow(/Multiple configuration files found/);
1054-
});
1055-
1056-
it("should throw if there are both .babelrc.js and package.json", () => {
1057-
const filename = fixture("config-files", "pkg-babelrc-js", "src.js");
10581054

1059-
expect(() =>
1060-
loadOptions({ filename, cwd: path.dirname(filename) }),
1061-
).toThrow(/Multiple configuration files found/);
1062-
});
1063-
1064-
it("should ignore package.json without a 'babel' property", () => {
1065-
const filename = fixture("config-files", "pkg-ignored", "src.js");
1055+
it("should ignore package.json without a 'babel' property", () => {
1056+
const filename = fixture("config-files", "pkg-ignored", "src.js");
10661057

1067-
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
1068-
...getDefaults(),
1069-
filename: filename,
1070-
cwd: path.dirname(filename),
1071-
root: path.dirname(filename),
1072-
comments: true,
1058+
expect(loadOptions({ filename, cwd: path.dirname(filename) })).toEqual({
1059+
...getDefaults(),
1060+
filename: filename,
1061+
cwd: path.dirname(filename),
1062+
root: path.dirname(filename),
1063+
comments: true,
1064+
});
10731065
});
1074-
});
10751066

1076-
it("should show helpful errors for .babelrc", () => {
1077-
const filename = fixture("config-files", "babelrc-error", "src.js");
1067+
test.each`
1068+
config | dir | error
1069+
${".babelrc"} | ${"babelrc-error"} | ${/Error while parsing config - /}
1070+
${".babelrc.js"} | ${"babelrc-js-error"} | ${/Babelrc threw an error/}
1071+
${".babelrc.cjs"} | ${"babelrc-cjs-error"} | ${/Babelrc threw an error/}
1072+
${"package.json"} | ${"pkg-error"} | ${/Error while parsing JSON - /}
1073+
`("should show helpful errors for $config", ({ dir, error }) => {
1074+
const filename = fixture("config-files", dir, "src.js");
10781075

1079-
expect(() =>
1080-
loadOptions({ filename, cwd: path.dirname(filename) }),
1081-
).toThrow(/Error while parsing config - /);
1082-
});
1083-
1084-
it("should show helpful errors for .babelrc.js", () => {
1085-
const filename = fixture("config-files", "babelrc-js-error", "src.js");
1086-
1087-
expect(() =>
1088-
loadOptions({ filename, cwd: path.dirname(filename) }),
1089-
).toThrow(/Babelrc threw an error/);
1090-
});
1091-
1092-
it("should show helpful errors for package.json", () => {
1093-
const filename = fixture("config-files", "pkg-error", "src.js");
1094-
1095-
expect(() =>
1096-
loadOptions({ filename, cwd: path.dirname(filename) }),
1097-
).toThrow(/Error while parsing JSON - /);
1076+
expect(() =>
1077+
loadOptions({ filename, cwd: path.dirname(filename) }),
1078+
).toThrow(error);
1079+
});
10981080
});
10991081

11001082
it("should throw when `test` presents but `filename` is not passed", () => {

packages/babel-core/test/fixtures/config/config-files/babel-config-json/babel.config.json renamed to packages/babel-core/test/fixtures/config/config-files-templates/.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"comments": true
3-
}
3+
}

packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.js renamed to packages/babel-core/test/fixtures/config/config-files-templates/.babelrc.cjs

File renamed without changes.

packages/babel-core/test/fixtures/config/config-files/babel-config-js/babel.config.js renamed to packages/babel-core/test/fixtures/config/config-files-templates/.babelrc.js

File renamed without changes.

packages/babel-core/test/fixtures/config/config-files/babelrc-js/.babelrc.js renamed to packages/babel-core/test/fixtures/config/config-files-templates/babel.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
comments: true,
2+
comments: true
33
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
comments: true
3+
};

packages/babel-core/test/fixtures/config/config-files/babel-config-js-and-json/babel.config.json renamed to packages/babel-core/test/fixtures/config/config-files-templates/babel.config.json

File renamed without changes.

packages/babel-core/test/fixtures/config/config-files/pkg/package.json renamed to packages/babel-core/test/fixtures/config/config-files-templates/package.json

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function() {
2+
throw new Error("Babelrc threw an error");
3+
};

packages/babel-core/test/fixtures/config/config-files/babelrc/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)