Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Add test for invalid electron version #176

Merged
merged 3 commits into from
Feb 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import browserslist from "browserslist";
import builtInsList from "../data/built-ins.json";
import defaultInclude from "./default-includes";
import { electronToChromium } from "electron-to-chromium";
import moduleTransformations from "./module-transformations";
import normalizeOptions from "./normalize-options.js";
import normalizeOptions, { getElectronChromeVersion } from "./normalize-options.js";
import pluginList from "../data/plugins.json";
import transformPolyfillRequirePlugin from "./transform-polyfill-require-plugin";

Expand Down Expand Up @@ -101,19 +100,13 @@ export const getTargets = (targets = {}) => {
targetOps.node = getCurrentNodeVersion();
}

// Rewrite Electron versions to their Chrome equivalents
// Replace Electron target with its Chrome equivalent
if (targetOps.electron) {
const electronChromeVersion = parseInt(electronToChromium(targetOps.electron), 10);
const electronChromeVersion = getElectronChromeVersion(targetOps.electron);

if (!electronChromeVersion) {
throw new Error(`Electron version ${targetOps.electron} is either too old or too new`);
}

if (targetOps.chrome) {
targetOps.chrome = Math.min(targetOps.chrome, electronChromeVersion);
} else {
targetOps.chrome = electronChromeVersion;
}
targetOps.chrome = targetOps.chrome
? Math.min(targetOps.chrome, electronChromeVersion)
: electronChromeVersion;

delete targetOps.electron;
}
Expand Down
12 changes: 12 additions & 0 deletions src/normalize-options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import invariant from "invariant";
import { electronToChromium } from "electron-to-chromium";
import builtInsList from "../data/built-ins.json";
import defaultInclude from "./default-includes";
import moduleTransformations from "./module-transformations";
Expand Down Expand Up @@ -69,6 +70,17 @@ export const validateModulesOption = (modulesOpt = "commonjs") => {
return modulesOpt;
};

export const getElectronChromeVersion = (electronVersion) => {
const electronChromeVersion = parseInt(electronToChromium(electronVersion), 10);

invariant(
!!electronChromeVersion,
`Electron version ${electronVersion} is either too old or too new`
);

return electronChromeVersion;
};

export default function normalizeOptions(opts) {
// TODO: remove whitelist in favor of include in next major
if (opts.whitelist && !hasBeenWarned) {
Expand Down
17 changes: 17 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ describe("babel-preset-env", () => {
});
});
});

it("should error if electron version is invalid", () => {
const fixtures = [
"0.19",
0.19,
999,
"999",
];

fixtures.forEach((electronVersion) => {
assert.throws(() => {
babelPresetEnv.getTargets({
electron: electronVersion,
});
}, Error);
});
});
});

describe("isPluginRequired", () => {
Expand Down