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

Commit

Permalink
fix: require default from latest plugins
Browse files Browse the repository at this point in the history
Also added tests
  • Loading branch information
christophehurpeau committed Dec 14, 2019
1 parent 11b0a9c commit 30708ae
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 59 deletions.
17 changes: 17 additions & 0 deletions lib/__tests_fixtures__/optional-chaining.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

exports.actual = `
const foo = { baz: 42 };
const bar = null;
console.log(foo?.baz === 42 && bar?.baz === undefined);
`;

exports.expected = `
"use strict";
const foo = {
baz: 42
};
const bar = null;
console.log((foo === null || foo === void 0 ? void 0 : foo.baz) === 42 && (bar === null || bar === void 0 ? void 0 : bar.baz) === undefined);
`;
50 changes: 50 additions & 0 deletions lib/fixtures.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const fs = require('fs');
const { transform } = require('@babel/core');

describe('fixtures', () => {
const presetPath = require.resolve('.');

test('preset is not mocked', () => {
// eslint-disable-next-line global-require, import/no-dynamic-require
const plugins = require(presetPath)(null, {
target: '12',
modules: false,
shippedProposals: false,
}).plugins;
expect(typeof plugins[0]).not.toBe('string');
});

const tests = fs
.readdirSync(`${__dirname}/__tests_fixtures__`)
.filter((name) => name.endsWith('.js'));

tests.forEach((filename) => {
// eslint-disable-next-line global-require, import/no-dynamic-require
const testContent = require(`${__dirname}/__tests_fixtures__/${filename}`);

test(testContent.name || filename, () => {
try {
const output = transform(testContent.actual, {
babelrc: false,
presets: [[presetPath, testContent.presetOptions || {}]],
});

const actual = output.code.trim();
const expected = testContent.expected.trim();

expect(actual).toBe(expected);
} catch (err) {
if (err._babel && err instanceof SyntaxError) {
console.log(`Unexpected error in test: ${test.name || filename}`);
console.log(`${err.name}: ${err.message}\n${err.codeFrame}`);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
} else {
throw err;
}
}
});
});
});
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ module.exports = function(context, opts = {}) {
/* shippedProposals */
shippedProposals && require('@babel/plugin-syntax-json-strings'),
shippedProposals &&
require('@babel/plugin-proposal-nullish-coalescing-operator'),
shippedProposals && require('@babel/plugin-proposal-optional-chaining'),
require('@babel/plugin-proposal-nullish-coalescing-operator').default,
shippedProposals &&
require('@babel/plugin-proposal-optional-chaining').default,
].filter(Boolean),
};
};
159 changes: 102 additions & 57 deletions lib/index.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
/* eslint-disable prettier/prettier, max-lines */
/* eslint-disable max-lines */

'use strict';

jest.mock('@babel/plugin-transform-modules-commonjs', () => '@babel/plugin-transform-modules-commonjs');
jest.mock('@babel/plugin-syntax-optional-catch-binding', () => '@babel/plugin-syntax-optional-catch-binding');
jest.mock('@babel/plugin-proposal-optional-catch-binding', () => '@babel/plugin-proposal-optional-catch-binding');
jest.mock('@babel/plugin-syntax-json-strings', () => '@babel/plugin-syntax-json-strings');
jest.mock('@babel/plugin-proposal-nullish-coalescing-operator', () => '@babel/plugin-proposal-nullish-coalescing-operator');
jest.mock('@babel/plugin-proposal-optional-chaining', () => '@babel/plugin-proposal-optional-chaining');
jest.mock(
'@babel/plugin-transform-modules-commonjs',
() => '@babel/plugin-transform-modules-commonjs'
);
jest.mock(
'@babel/plugin-syntax-optional-catch-binding',
() => '@babel/plugin-syntax-optional-catch-binding'
);
jest.mock(
'@babel/plugin-proposal-optional-catch-binding',
() => '@babel/plugin-proposal-optional-catch-binding'
);
jest.mock(
'@babel/plugin-syntax-json-strings',
() => '@babel/plugin-syntax-json-strings'
);
jest.mock('@babel/plugin-proposal-nullish-coalescing-operator', () => ({
default: '@babel/plugin-proposal-nullish-coalescing-operator',
}));
jest.mock('@babel/plugin-proposal-optional-chaining', () => ({
default: '@babel/plugin-proposal-optional-chaining',
}));

const plugins = {};

plugins.modulesCommonjs = require('@babel/plugin-transform-modules-commonjs');
plugins.optionalCatchBindingSyntax = require('@babel/plugin-syntax-optional-catch-binding');
plugins.optionalCatchBindingProposal = require('@babel/plugin-proposal-optional-catch-binding');
plugins.jsonStringsSyntax = require('@babel/plugin-syntax-json-strings');
plugins.proposalNullishCoalescingOperator = require('@babel/plugin-proposal-nullish-coalescing-operator').default;
plugins.proposalOptionalChaining = require('@babel/plugin-proposal-optional-chaining').default;


const pluginModulesCommonjs = require('@babel/plugin-transform-modules-commonjs');
const pluginOptionalCatchBindingSyntax = require('@babel/plugin-syntax-optional-catch-binding');
const pluginOptionalCatchBindingProposal = require('@babel/plugin-proposal-optional-catch-binding');
const pluginJsonStringsSyntax = require('@babel/plugin-syntax-json-strings');
const pluginProposalNullishCoalescingOperator = require('@babel/plugin-proposal-nullish-coalescing-operator');
const pluginProposalOptionalChaining = require('@babel/plugin-proposal-optional-chaining');
const preset = require('.');

describe('options', () => {
Expand All @@ -26,7 +44,22 @@ describe('options', () => {
);
});

[ '10', '10.13', '11', '12', '13', 10, 10.13, 11, 12, 13, 'current', undefined, null, ''].forEach(target => {
[
'10',
'10.13',
'11',
'12',
'13',
10,
10.13,
11,
12,
13,
'current',
undefined,
null,
'',
].forEach((target) => {
test(`${target} ${typeof target} is a valid target`, () => {
expect(preset(null, { target })).toBeDefined();
});
Expand All @@ -40,9 +73,11 @@ describe('options', () => {
);
});

['commonjs', false].forEach(option => {
['commonjs', false].forEach((option) => {
test(`${option} is a valid modules option`, () => {
expect(preset(null, { target: 'current', modules: option })).toBeDefined();
expect(
preset(null, { target: 'current', modules: option })
).toBeDefined();
});
});
});
Expand All @@ -54,9 +89,11 @@ describe('options', () => {
);
});

[true, false].forEach(option => {
[true, false].forEach((option) => {
test(`${option} is a valid loose option`, () => {
expect(preset(null, { target: 'current', loose: option })).toBeDefined();
expect(
preset(null, { target: 'current', loose: option })
).toBeDefined();
});
});
});
Expand All @@ -68,23 +105,29 @@ describe('options', () => {
);
});

[true, false].forEach(option => {
[true, false].forEach((option) => {
test(`${option} is a valid es2019 option`, () => {
expect(preset(null, { target: 'current', es2019: option })).toBeDefined();
expect(
preset(null, { target: 'current', es2019: option })
).toBeDefined();
});
});
});

describe('shippedProposals', () => {
test('xx is not a valid shippedProposals option', () => {
expect(() => preset(null, { target: 'current', shippedProposals: 'xx' })).toThrow(
expect(() =>
preset(null, { target: 'current', shippedProposals: 'xx' })
).toThrow(
"Preset latest-node 'shippedProposals' option must be a boolean."
);
});

[true, false].forEach(option => {
[true, false].forEach((option) => {
test(`${option} is a valid shippedProposals option`, () => {
expect(preset(null, { target: 'current', shippedProposals: option })).toBeDefined();
expect(
preset(null, { target: 'current', shippedProposals: option })
).toBeDefined();
});
});
});
Expand All @@ -95,20 +138,20 @@ describe('plugins', () => {
test('default options', () => {
expect(preset(null, { target: '10' })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingProposal,
pluginJsonStringsSyntax,
pluginProposalNullishCoalescingOperator,
pluginProposalOptionalChaining
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingProposal,
plugins.jsonStringsSyntax,
plugins.proposalNullishCoalescingOperator,
plugins.proposalOptionalChaining,
],
});
});

test('{ shippedProposals: false }', () => {
expect(preset(null, { target: '10', shippedProposals: false })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingProposal,
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingProposal,
],
});
});
Expand All @@ -118,20 +161,22 @@ pluginProposalOptionalChaining
test('default options', () => {
expect(preset(null, { target: '10.13' })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
pluginJsonStringsSyntax,
pluginProposalNullishCoalescingOperator,
pluginProposalOptionalChaining
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
plugins.jsonStringsSyntax,
plugins.proposalNullishCoalescingOperator,
plugins.proposalOptionalChaining,
],
});
});

test('{ shippedProposals: false }', () => {
expect(preset(null, { target: '10.13', shippedProposals: false })).toEqual({
expect(
preset(null, { target: '10.13', shippedProposals: false })
).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
],
});
});
Expand All @@ -141,20 +186,20 @@ pluginProposalOptionalChaining
test('default options', () => {
expect(preset(null, { target: '11' })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
pluginJsonStringsSyntax,
pluginProposalNullishCoalescingOperator,
pluginProposalOptionalChaining
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
plugins.jsonStringsSyntax,
plugins.proposalNullishCoalescingOperator,
plugins.proposalOptionalChaining,
],
});
});

test('{ shippedProposals: false }', () => {
expect(preset(null, { target: '11', shippedProposals: false })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
],
});
});
Expand All @@ -164,27 +209,27 @@ pluginProposalOptionalChaining
test('default options', () => {
expect(preset(null, { target: '12' })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
pluginJsonStringsSyntax,
pluginProposalNullishCoalescingOperator,
pluginProposalOptionalChaining
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
plugins.jsonStringsSyntax,
plugins.proposalNullishCoalescingOperator,
plugins.proposalOptionalChaining,
],
});
});

test('{ es2019: false }', () => {
expect(preset(null, { target: '12', es2019: false, shippedProposals: false })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }]
],
expect(
preset(null, { target: '12', es2019: false, shippedProposals: false })
).toEqual({
plugins: [[plugins.modulesCommonjs, { loose: false }]],
});
});
test('{ shippedProposals: false }', () => {
expect(preset(null, { target: '12', shippedProposals: false })).toEqual({
plugins: [
[pluginModulesCommonjs, { loose: false }],
pluginOptionalCatchBindingSyntax,
[plugins.modulesCommonjs, { loose: false }],
plugins.optionalCatchBindingSyntax,
],
});
});
Expand Down

0 comments on commit 30708ae

Please sign in to comment.