From 00df737d3da87dacb77e545a7ca3ba2a0cba44ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Wed, 25 Dec 2019 14:42:54 +0100 Subject: [PATCH] use getCode helper where possible --- packages/babel/test/as-input-plugin.js | 70 +++++++++---------- packages/babel/test/as-output-plugin.js | 89 +++++++++++++++---------- 2 files changed, 90 insertions(+), 69 deletions(-) diff --git a/packages/babel/test/as-input-plugin.js b/packages/babel/test/as-input-plugin.js index 71679173d..8bfdd422b 100644 --- a/packages/babel/test/as-input-plugin.js +++ b/packages/babel/test/as-input-plugin.js @@ -5,6 +5,8 @@ import { rollup } from 'rollup'; import { SourceMapConsumer } from 'source-map'; import jsonPlugin from '@rollup/plugin-json'; +import { getCode } from '../../../util/test'; + import babelPlugin from '..'; process.chdir(__dirname); @@ -51,15 +53,14 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp ...rollupOptions }); - const { - output: [generated] - } = await bundle.generate(Object.assign({ format: 'cjs' }, generateOptions)); - - return generated; + return getCode(bundle, { + format: 'cjs', + ...generateOptions + }); } test('runs code through babel', async (t) => { - const { code } = await generate('fixtures/basic/main.js'); + const code = await generate('fixtures/basic/main.js'); t.false(code.includes('const')); t.is( code, @@ -72,17 +73,17 @@ console.log("the answer is ".concat(answer)); }); test('adds helpers', async (t) => { - const { code } = await generate('fixtures/class/main.js'); + const code = await generate('fixtures/class/main.js'); t.true(code.includes('function _classCallCheck')); }); test('adds helpers in loose mode', async (t) => { - const { code } = await generate('fixtures/class-loose/main.js'); + const code = await generate('fixtures/class-loose/main.js'); t.true(code.includes('function _inherits')); }); test('does not babelify excluded code', async (t) => { - const { code } = await generate('fixtures/exclusions/main.js', { exclude: '**/foo.js' }); + const code = await generate('fixtures/exclusions/main.js', { exclude: '**/foo.js' }); // eslint-disable-next-line no-template-curly-in-string t.false(code.includes('${foo()}')); t.true(code.includes('=> 42')); @@ -98,7 +99,15 @@ console.log("the answer is ".concat(foo())); }); test('generates sourcemap by default', async (t) => { - const { code, map } = await generate('fixtures/class/main.js', {}, { sourcemap: true }); + const bundle = await rollup({ + input: 'fixtures/class/main.js', + plugins: [babelPlugin({ babelHelpers: 'bundled' })] + }); + + const { + output: [{ code, map }] + } = await bundle.generate({ format: 'cjs', sourcemap: true }); + const target = 'log'; const smc = new SourceMapConsumer(map); const loc = getLocation(code, code.indexOf(target)); @@ -121,13 +130,7 @@ test('works with proposal-decorators (#18)', async (t) => { }); test('checks config per-file', async (t) => { - const bundle = await rollup({ - input: 'fixtures/checks/main.js', - plugins: [babelPlugin({ babelHelpers: 'bundled' })] - }); - const { - output: [{ code }] - } = await bundle.generate({ output: { format: 'esm' } }); + const code = await generate('fixtures/checks/main.js', {}, { format: 'esm' }); t.true(code.includes('class Foo')); t.true(code.includes('var Bar')); t.false(code.includes('class Bar')); @@ -135,7 +138,7 @@ test('checks config per-file', async (t) => { test('allows transform-runtime to be used instead of bundled helpers', async (t) => { const warnings = []; - const { code } = await generate( + const code = await generate( 'fixtures/runtime-helpers/main.js', { babelHelpers: 'runtime' }, {}, @@ -167,7 +170,7 @@ module.exports = Foo; test('allows transform-runtime to inject esm version of helpers', async (t) => { const warnings = []; - const { code } = await generate( + const code = await generate( 'fixtures/runtime-helpers-esm/main.js', { babelHelpers: 'runtime' }, { @@ -209,7 +212,7 @@ test('allows transform-runtime to be used instead of bundled helpers, but throws }); test('allows using external-helpers plugin in combination with @babel/plugin-external-helpers', async (t) => { - const { code } = await generate('fixtures/external-helpers/main.js', { + const code = await generate('fixtures/external-helpers/main.js', { babelHelpers: 'external' }); t.false(code.includes('function _classCallCheck')); @@ -234,7 +237,7 @@ module.exports = main; }); test('correctly renames helpers (#22)', async (t) => { - const { code } = await generate('fixtures/named-function-helper/main.js'); + const code = await generate('fixtures/named-function-helper/main.js'); t.false(code.includes('babelHelpers_get get'), 'helper was incorrectly renamed'); }); @@ -247,17 +250,17 @@ test('runs preflight check correctly in absence of class transformer (#23)', asy }); test('produces valid code with typeof helper', async (t) => { - const { code } = await generate('fixtures/typeof/main.js'); + const code = await generate('fixtures/typeof/main.js'); t.false(code.includes('var typeof')); }); test('handles babelrc with ignore option used', async (t) => { - const { code } = await generate('fixtures/ignored-file/main.js'); + const code = await generate('fixtures/ignored-file/main.js'); t.true(code.includes('class Ignored')); }); test('transpiles only files with default extensions', async (t) => { - const { code } = await generate( + const code = await generate( 'fixtures/extensions-default/main.js', {}, {}, @@ -274,7 +277,7 @@ test('transpiles only files with default extensions', async (t) => { }); test('transpiles only files with whitelisted extensions', async (t) => { - const { code } = await generate('fixtures/extensions-custom/main.js', { + const code = await generate('fixtures/extensions-custom/main.js', { extensions: ['.js', '.other'] }); t.true(code.includes('class Es '), 'should not transpile .es'); @@ -365,9 +368,8 @@ test('supports customizing the loader', async (t) => { input: 'fixtures/basic/main.js', plugins: [customBabelPlugin({ babelHelpers: 'bundled' })] }); - const { - output: [{ code }] - } = await bundle.generate({ format: 'cjs' }); + const code = await getCode(bundle); + t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment'); t.true(code.includes('console.foobaz'), 'runs the plugin'); }); @@ -401,9 +403,8 @@ test('supports overriding the plugin options in custom loader', async (t) => { input: 'fixtures/basic/main.js', plugins: [customBabelPlugin({ babelHelpers: 'bundled' })] }); - const { - output: [{ code }] - } = await bundle.generate({ format: 'cjs' }); + const code = await getCode(bundle); + t.false( code.includes('// Generated by some custom loader'), 'does not add the comment to ignored file' @@ -412,7 +413,7 @@ test('supports overriding the plugin options in custom loader', async (t) => { }); test('uses babel plugins passed in to the rollup plugin', async (t) => { - const { code } = await generate('fixtures/basic/main.js', { + const code = await generate('fixtures/basic/main.js', { plugins: [[replaceConsoleLogProperty, { replace: 'foobaz' }]] }); t.true(code.includes('console.foobaz')); @@ -427,8 +428,7 @@ test('can be used as an input plugin while transforming the output', async (t) = }) ] }); - const { - output: [{ code }] - } = await bundle.generate({ format: 'cjs' }); + const code = await getCode(bundle); + t.false(code.includes('const')); }); diff --git a/packages/babel/test/as-output-plugin.js b/packages/babel/test/as-output-plugin.js index 96b62300d..db722a7c6 100644 --- a/packages/babel/test/as-output-plugin.js +++ b/packages/babel/test/as-output-plugin.js @@ -4,6 +4,8 @@ import test from 'ava'; import { rollup } from 'rollup'; import { SourceMapConsumer } from 'source-map'; +import { getCode } from '../../../util/test'; + import babelPlugin from '..'; process.chdir(__dirname); @@ -44,31 +46,32 @@ function replaceConsoleLogProperty({ types: t }) { } async function generate(input, babelOptions = {}, generateOptions = {}, rollupOptions = {}) { - const bundle = await rollup(Object.assign({ input }, rollupOptions)); - const { - output: [generated] - } = await bundle.generate({ + const bundle = await rollup({ + input, + ...rollupOptions + }); + + return getCode(bundle, { format: 'cjs', plugins: [babelPlugin.generated(babelOptions)], ...generateOptions }); - return generated; } test('allows running the plugin on the output via output options', async (t) => { - const { code } = await generate('fixtures/basic/main.js', { + const code = await generate('fixtures/basic/main.js', { presets: ['@babel/env'] }); t.false(code.includes('const')); }); test('ignores .babelrc when transforming the output by default', async (t) => { - const { code } = await generate('fixtures/basic/main.js'); + const code = await generate('fixtures/basic/main.js'); t.true(code.includes('const')); }); test("allows transform-runtime to be used with `useESModules: false` (the default) and `format: 'cjs'`", async (t) => { - const { code } = await generate( + const code = await generate( 'fixtures/runtime-helpers/main.js', { presets: ['@babel/env'], @@ -92,7 +95,7 @@ module.exports = Foo; }); test("allows transform-runtime to be used with `useESModules: true` and `format: 'esm'`", async (t) => { - const { code } = await generate( + const code = await generate( 'fixtures/runtime-helpers/main.js', { presets: ['@babel/env'], @@ -114,7 +117,16 @@ export default Foo; }); test('generates sourcemap by default', async (t) => { - const { code, map } = await generate('fixtures/class/main.js', {}, { sourcemap: true }); + const bundle = await rollup({ input: 'fixtures/class/main.js' }); + + const { + output: [{ code, map }] + } = await bundle.generate({ + format: 'cjs', + plugins: [babelPlugin.generated()], + sourcemap: true + }); + const target = 'log'; const smc = new SourceMapConsumer(map); const loc = getLocation(code, code.indexOf(target)); @@ -130,7 +142,7 @@ test('generates sourcemap by default', async (t) => { test('allows using external-helpers plugin even if the externalHelpers flag is not passed', async (t) => { const warnings = []; - const { code } = await generate( + const code = await generate( 'fixtures/external-helpers/main.js', { presets: ['@babel/env'], @@ -185,15 +197,20 @@ test('warns when using the "include" option', async (t) => { test('transforms all chunks in a code-splitting setup', async (t) => { const bundle = await rollup({ input: 'fixtures/chunks/main.js' }); - const { output } = await bundle.generate({ - format: 'esm', - plugins: [ - babelPlugin.generated({ - plugins: ['@babel/syntax-dynamic-import'], - presets: ['@babel/env'] - }) - ] - }); + const output = await getCode( + bundle, + { + format: 'esm', + plugins: [ + babelPlugin.generated({ + plugins: ['@babel/syntax-dynamic-import'], + presets: ['@babel/env'] + }) + ] + }, + true + ); + t.deepEqual( output.map(({ code }) => code), [ @@ -216,14 +233,19 @@ test('transforms all chunks when preserving modules', async (t) => { input: 'fixtures/preserve-modules/main.js', preserveModules: true }); - const { output } = await bundle.generate({ - format: 'esm', - plugins: [ - babelPlugin.generated({ - presets: ['@babel/env'] - }) - ] - }); + const output = await getCode( + bundle, + { + format: 'esm', + plugins: [ + babelPlugin.generated({ + presets: ['@babel/env'] + }) + ] + }, + true + ); + t.deepEqual( output.map(({ code }) => code), [ @@ -262,9 +284,8 @@ test('supports customizing the loader', async (t) => { }; }); const bundle = await rollup({ input: 'fixtures/basic/main.js' }); - const { - output: [{ code }] - } = await bundle.generate({ format: 'cjs', plugins: [customBabelPlugin()] }); + const code = await getCode(bundle, { format: 'cjs', plugins: [customBabelPlugin()] }); + t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment'); t.true(code.includes('console.foobaz'), 'runs the plugin'); }); @@ -282,7 +303,7 @@ test('throws when using a Rollup output format other than esm or cjs', async (t) }); test('allows using a Rollup output format other than esm or cjs with allowAllFormats', async (t) => { - const { code } = await generate( + const code = await generate( 'fixtures/basic/main.js', { presets: ['@babel/env'], allowAllFormats: true }, { format: 'iife' } @@ -300,7 +321,7 @@ test('allows using a Rollup output format other than esm or cjs with allowAllFor }); test('allows using Babel to transform to other formats', async (t) => { - const { code } = await generate( + const code = await generate( 'fixtures/basic/main.js', { presets: [['@babel/env', { modules: 'umd' }]] }, { format: 'esm' } @@ -330,7 +351,7 @@ test('allows using Babel to transform to other formats', async (t) => { }); test('loads configuration files when configFile is passed', async (t) => { - const { code } = await generate('fixtures/config-file/main.js', { + const code = await generate('fixtures/config-file/main.js', { configFile: nodePath.resolve(__dirname, 'fixtures/config-file/config.json') }); t.is(