From 81c79bec073c02ab03f50234b215fc5c80819ff0 Mon Sep 17 00:00:00 2001 From: Kristofer Baxter Date: Fri, 27 Dec 2019 17:13:39 -0800 Subject: [PATCH] Expand Test Coverage (#262) * functions exported with arguments * Test for strict removal * Test for removing strict declaration on mjs output when the output is an iife. --- .../fixtures/multiple.esm.advanced.js | 2 +- .../fixtures/multiple.esm.default.js | 2 +- .../export-named/fixtures/multiple.esm.es5.js | 2 +- test/export-named/fixtures/multiple.js | 5 +- .../fixtures/mjs-suffix.iife.default.mjs | 1 + test/strict-removal/fixtures/mjs-suffix.mjs | 3 + .../fixtures/top-level.esm.advanced.js | 1 + .../fixtures/top-level.esm.default.js | 1 + .../fixtures/top-level.esm.es5.js | 1 + test/strict-removal/fixtures/top-level.js | 3 + test/strict-removal/mjs-suffix.test.js | 55 +++++++++++++++++++ test/strict-removal/top-level.test.js | 19 +++++++ 12 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 test/strict-removal/fixtures/mjs-suffix.iife.default.mjs create mode 100644 test/strict-removal/fixtures/mjs-suffix.mjs create mode 100644 test/strict-removal/fixtures/top-level.esm.advanced.js create mode 100644 test/strict-removal/fixtures/top-level.esm.default.js create mode 100644 test/strict-removal/fixtures/top-level.esm.es5.js create mode 100644 test/strict-removal/fixtures/top-level.js create mode 100644 test/strict-removal/mjs-suffix.test.js create mode 100644 test/strict-removal/top-level.test.js diff --git a/test/export-named/fixtures/multiple.esm.advanced.js b/test/export-named/fixtures/multiple.esm.advanced.js index 3cff53f5..a938c6ca 100644 --- a/test/export-named/fixtures/multiple.esm.advanced.js +++ b/test/export-named/fixtures/multiple.esm.advanced.js @@ -1 +1 @@ -function bar(){console.log(1)};var foo=1;export{bar,foo}; +function bar(){console.log(1)};function baz(a){console.log(a)};var foo=1;export{bar,baz,foo}; diff --git a/test/export-named/fixtures/multiple.esm.default.js b/test/export-named/fixtures/multiple.esm.default.js index 3cff53f5..a938c6ca 100644 --- a/test/export-named/fixtures/multiple.esm.default.js +++ b/test/export-named/fixtures/multiple.esm.default.js @@ -1 +1 @@ -function bar(){console.log(1)};var foo=1;export{bar,foo}; +function bar(){console.log(1)};function baz(a){console.log(a)};var foo=1;export{bar,baz,foo}; diff --git a/test/export-named/fixtures/multiple.esm.es5.js b/test/export-named/fixtures/multiple.esm.es5.js index 3cff53f5..a938c6ca 100644 --- a/test/export-named/fixtures/multiple.esm.es5.js +++ b/test/export-named/fixtures/multiple.esm.es5.js @@ -1 +1 @@ -function bar(){console.log(1)};var foo=1;export{bar,foo}; +function bar(){console.log(1)};function baz(a){console.log(a)};var foo=1;export{bar,baz,foo}; diff --git a/test/export-named/fixtures/multiple.js b/test/export-named/fixtures/multiple.js index 4f96f326..a51e33ab 100644 --- a/test/export-named/fixtures/multiple.js +++ b/test/export-named/fixtures/multiple.js @@ -2,4 +2,7 @@ const foo = 1; const bar = function() { console.log(foo); } -export{foo, bar}; +const baz = function(name) { + console.log(name); +} +export{foo, bar, baz}; diff --git a/test/strict-removal/fixtures/mjs-suffix.iife.default.mjs b/test/strict-removal/fixtures/mjs-suffix.iife.default.mjs new file mode 100644 index 00000000..8f92af18 --- /dev/null +++ b/test/strict-removal/fixtures/mjs-suffix.iife.default.mjs @@ -0,0 +1 @@ +var modular=function(a){a.thing=1;return a}({}); diff --git a/test/strict-removal/fixtures/mjs-suffix.mjs b/test/strict-removal/fixtures/mjs-suffix.mjs new file mode 100644 index 00000000..9aa42dd9 --- /dev/null +++ b/test/strict-removal/fixtures/mjs-suffix.mjs @@ -0,0 +1,3 @@ +'use strict'; + +export const thing = 1; \ No newline at end of file diff --git a/test/strict-removal/fixtures/top-level.esm.advanced.js b/test/strict-removal/fixtures/top-level.esm.advanced.js new file mode 100644 index 00000000..89ba6b68 --- /dev/null +++ b/test/strict-removal/fixtures/top-level.esm.advanced.js @@ -0,0 +1 @@ +export var thing=1; diff --git a/test/strict-removal/fixtures/top-level.esm.default.js b/test/strict-removal/fixtures/top-level.esm.default.js new file mode 100644 index 00000000..89ba6b68 --- /dev/null +++ b/test/strict-removal/fixtures/top-level.esm.default.js @@ -0,0 +1 @@ +export var thing=1; diff --git a/test/strict-removal/fixtures/top-level.esm.es5.js b/test/strict-removal/fixtures/top-level.esm.es5.js new file mode 100644 index 00000000..89ba6b68 --- /dev/null +++ b/test/strict-removal/fixtures/top-level.esm.es5.js @@ -0,0 +1 @@ +export var thing=1; diff --git a/test/strict-removal/fixtures/top-level.js b/test/strict-removal/fixtures/top-level.js new file mode 100644 index 00000000..9aa42dd9 --- /dev/null +++ b/test/strict-removal/fixtures/top-level.js @@ -0,0 +1,3 @@ +'use strict'; + +export const thing = 1; \ No newline at end of file diff --git a/test/strict-removal/mjs-suffix.test.js b/test/strict-removal/mjs-suffix.test.js new file mode 100644 index 00000000..836ef44a --- /dev/null +++ b/test/strict-removal/mjs-suffix.test.js @@ -0,0 +1,55 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const test = require('ava'); +const { default: compiler } = require('../../transpile/index'); +const rollup = require('rollup'); +const fs = require('fs'); +const path = require('path'); + +test('remove strict declaration from .mjs input', async t => { + const bundle = await rollup.rollup({ + input: 'test/strict-removal/fixtures/mjs-suffix.mjs', + plugins: [compiler()], + onwarn: _ => null, + }); + + const bundles = await bundle.generate({ + format: 'iife', + name: 'modular', + sourcemap: true, + file: 'mjs-suffix.iife.default.mjs' + }); + + const output = []; + if (bundles.output) { + for (const file in bundles.output) { + const minified = await fs.promises.readFile( + path.join('test/strict-removal/fixtures/mjs-suffix.iife.default.mjs'), + 'utf8', + ); + output.push({ + minified, + code: bundles.output[file].code, + }); + } + } + + t.plan(output.length); + for (const result of output) { + t.is(result.code, result.minified); + } +}); diff --git a/test/strict-removal/top-level.test.js b/test/strict-removal/top-level.test.js new file mode 100644 index 00000000..07780fc5 --- /dev/null +++ b/test/strict-removal/top-level.test.js @@ -0,0 +1,19 @@ +/** + * Copyright 2018 The AMP HTML Authors. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {generator} from '../generator'; + +generator('strict-removal', 'top-level'); \ No newline at end of file