babel/packages/babel-preset-env/test/normalize-options.spec.js
Denis Pushkarev 3303b079c5 Update to core-js@3 (#7646)
### `@babel/runtime`
- Added `@babel/runtime-corejs3` package and `corejs: 3` options to `@babel/plugin-transform-runtime`.
- Added support of instance methods, fixes #8928.
- Added flag `proposals` (in `corejs: { version: 3, proposals: true }` format) for support all proposals polyfills from `core-js`.
- Used separate directories in runtime for `core-js` entry points with proposals and without.
- Used `get-iterator-method` helper for getting iterators, fixes #2500.
- As a cheap bonus, added support of IE8- (except some cases of `regenerator`).

### `@babel/polyfill`
- Should be deprecated in favor of separate usage required features from `core-js` and `regenerator-runtime` with an informative message.

### `@babel/preset-env`
- Uses for built-ins data from [`core-js-compat`](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat) instead of `compat-table` since information from `compat-table` [is not enough](https://github.com/zloirock/core-js/tree/master/packages/core-js-compat).
- `useBuilIns` now requires direct setting of `corejs` version option, without it will be used `2` by default and shown deprecation warning.
- Added support of minor `core-js` versions for simplify updating in the future.
- For preventing some order-related problems, polyfills in the both `core-js@3` plugins added on `post` stage in the order of `core-js-compat` data.
- Divided plugins and polyfills parts of `preset-env`, instead of 2 internal plugins for adding polyfills, we have 6: usage and entry versions of plugins for `core-js@2`, ### Current state:
`core-js@3`, `regenerator-runtime`.
- Added support `samsung` target (for Samsung Internet) since `core-js-compat` and `compat-table` now contains mapping for this, fixes #6602.

#### `useBuilIns: entry` with `corejs: 3`
- No longer transforms `@babel/polyfill`.
- Transforms **all possible** `core-js` entry points to import of related modules (based on data from [`core-js-compat`](https://unpkg.com/core-js-compat@3.0.0-beta.15/entries.json)).
- Since of this, we no longer need `shippedProposals` / `proposals` flags with `useBuilIns: entry`.
- Removes `regenerator-runtime/runtime` import where it's not required.

#### `useBuilIns: usage` with `corejs: 3`
- In addition to `shippedProposals`, added flag `proposals`  (in `corejs: { version: 3, proposals: true }` format) for polyfill all proposals from `core-js`.
- Fixed list of dependencies in built-in definitions.
- Improved the way of determination method / built-in name and source of this method.
- Adds import of required polyfills on `MemberExpression`, `ObjectPattern`, `in` operator.
- Adds import of required polyfills on access to global object properties.
- Adds import of all required common iterators on all syntax features which use iterators protocol (`for-of`, destructuring, spread, `yield` delegation, etc.).
- Adds import of promises on syntax features which use promises (async functions/generators, dynamic import, etc.), fixes #9250, #7402, etc.

### `core-js@2` stuff
I didn't want to tough `core-js@2`-related stuff, however
- Fixed some serious errors in definitions which breaks `Object.getOwnPropertySymbols`, `Symbol.toStringTag` logic, `Promise#finally`, `Array#forEach`, etc.
- `Array#flatMap` and trim methods moved to stable features as a part of ES2019 and loaded by deprecated `@babel/polyfill` and `@babel/preset-env` with `corejs: 2` option.
2019-03-19 21:07:45 +01:00

214 lines
6.7 KiB
JavaScript

"use strict";
const normalizeOptions = require("../lib/normalize-options.js");
const {
checkDuplicateIncludeExcludes,
validateBoolOption,
validateModulesOption,
normalizePluginName,
} = normalizeOptions;
describe("normalize-options", () => {
describe("normalizeOptions", () => {
it("should return normalized `include` and `exclude`", () => {
const normalized = normalizeOptions.default({
include: [
"babel-plugin-transform-spread",
"transform-classes",
"@babel/plugin-transform-unicode-regex",
"@babel/transform-block-scoping",
],
exclude: [
"babel-plugin-transform-for-of",
"transform-parameters",
"@babel/plugin-transform-regenerator",
"@babel/transform-new-target",
],
});
expect(normalized.include).toEqual([
"transform-spread",
"transform-classes",
"transform-unicode-regex",
"transform-block-scoping",
]);
expect(normalized.exclude).toEqual([
"transform-for-of",
"transform-parameters",
"transform-regenerator",
"transform-new-target",
]);
});
it("should not normalize babel-plugin with prefix", () => {
const normalized = normalizePluginName("prefix-babel-plugin-postfix");
expect(normalized).toBe("prefix-babel-plugin-postfix");
});
test.each`
include | exclude
${["babel-plugin-transform-spread"]} | ${["transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["transform-spread"]}
${["transform-spread"]} | ${["babel-plugin-transform-spread"]}
${["transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["babel-plugin-transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["babel-plugin-transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["babel-plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["babel-plugin-transform-spread"]}
`(
"should throw if with includes $include and excludes $exclude",
({ include, exclude }) => {
expect(() =>
normalizeOptions.default({ include, exclude }),
).toThrowError(/were found in both/);
},
);
});
describe("Config format validation", () => {
it("should throw if top-level option not found", () => {
const unknownTopLevelOption = () => {
normalizeOptions({ unknown: "option" });
};
expect(unknownTopLevelOption).toThrow();
});
});
describe("RegExp include/exclude", () => {
it("should not allow invalid plugins in `include` and `exclude`", () => {
const normalizeWithNonExistingPlugin = () => {
normalizeOptions.default({
include: ["non-existing-plugin"],
});
};
expect(normalizeWithNonExistingPlugin).toThrow(Error);
});
it("should expand regular expressions in `include` and `exclude`", () => {
const normalized = normalizeOptions.default({
include: ["^[a-z]*-spread", "babel-plugin-transform-classes"],
});
expect(normalized.include).toEqual([
"transform-spread",
"transform-classes",
]);
});
it("should expand regular expressions in `include` and `exclude`", () => {
const normalized = normalizeOptions.default({
useBuiltIns: "entry",
corejs: 3,
exclude: ["es.math.log.*"],
});
expect(normalized.exclude).toEqual([
"es.math.log10",
"es.math.log1p",
"es.math.log2",
]);
});
it("should not allow the same modules in `include` and `exclude`", () => {
const normalizeWithNonExistingPlugin = () => {
normalizeOptions.default({
useBuiltIns: "entry",
corejs: 3,
include: ["es.math.log2"],
exclude: ["es.math.log.*"],
});
};
expect(normalizeWithNonExistingPlugin).toThrow(Error);
});
it("should not do partial match if not explicitly defined `include` and `exclude`", () => {
const normalized = normalizeOptions.default({
useBuiltIns: "entry",
corejs: 3,
include: ["es.reflect.set-prototype-of"],
exclude: ["es.reflect.set"],
});
expect(normalized.include).toEqual(["es.reflect.set-prototype-of"]);
expect(normalized.exclude).toEqual(["es.reflect.set"]);
});
});
describe("validateBoolOption", () => {
it("`undefined` option returns false", () => {
expect(validateBoolOption("test", undefined, false)).toBe(false);
});
it("`false` option returns false", () => {
expect(validateBoolOption("test", false, false)).toBe(false);
});
it("`true` option returns true", () => {
expect(validateBoolOption("test", true, false)).toBe(true);
});
it("array option is invalid", () => {
expect(() => {
validateBoolOption("test", [], false);
}).toThrow();
});
});
describe("checkDuplicateIncludeExcludes", function() {
it("should throw if duplicate names in both", function() {
expect(() => {
checkDuplicateIncludeExcludes(
["transform-regenerator", "map"],
["transform-regenerator", "map"],
);
}).toThrow();
});
it("should not throw if no duplicate names in both", function() {
expect(() => {
checkDuplicateIncludeExcludes(["transform-regenerator"], ["map"]);
}).not.toThrow();
});
});
describe("validateModulesOption", () => {
it("`undefined` option returns auto", () => {
expect(validateModulesOption()).toBe("auto");
});
it("`false` option returns false", () => {
expect(validateModulesOption(false)).toBe(false);
});
it("auto option is valid", () => {
expect(validateModulesOption("auto")).toBe("auto");
});
it("commonjs option is valid", () => {
expect(validateModulesOption("commonjs")).toBe("commonjs");
});
it("systemjs option is valid", () => {
expect(validateModulesOption("systemjs")).toBe("systemjs");
});
it("amd option is valid", () => {
expect(validateModulesOption("amd")).toBe("amd");
});
it("umd option is valid", () => {
expect(validateModulesOption("umd")).toBe("umd");
});
it("`true` option is invalid", () => {
expect(() => {
validateModulesOption(true);
}).toThrow();
});
it("array option is invalid", () => {
expect(() => {
validateModulesOption([]);
}).toThrow();
});
});
});