Skip to content

Commit

Permalink
added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alshdavid committed Aug 22, 2023
1 parent 2ada1d4 commit dba4b8e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script type="module" src="./index.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import('./lazy').then(m => alert(m));
emitGlobalThis(globalThis);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const lazy = 'lazy value';
127 changes: 72 additions & 55 deletions packages/core/integration-tests/test/packager.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import assert from 'assert';
import path from 'path';
import nullthrows from 'nullthrows';
import {normalizePath} from '@parcel/utils';
import {createWorkerFarm} from '@parcel/core';
import {md} from '@parcel/diagnostic';
import {
assertBundles,
bundle as _bundle,
bundler as _bundler,
distDir,
findAsset,
findDependency,
getNextBuild,
mergeParcelOptions,
outputFS,
overlayFS,
run,
runBundle,
} from '@parcel/test-utils';

const runBundler = (name, opts = {}) => {
Expand All @@ -27,55 +16,83 @@ const runBundler = (name, opts = {}) => {
);
};

const bundler = (name, opts = {}) => {
return _bundler(
name,
// $FlowFixMe
mergeParcelOptions({}, opts),
);
};
function hasPolyfill(code) {
const noPolyfill = `var $parcel$global = globalThis;`;
const polyfill = `typeof globalThis !== 'undefined'`;
return code.includes(polyfill) && !code.includes(noPolyfill);
}

describe.only('packager', function () {
describe('packager', function () {
describe('globalThis polyfill', function () {
describe('es6', function () {
it('should include globalThis polyfill in ie11 builds', async function () {
const entryPoint = path.join(
__dirname,
'integration/html-js-dynamic/index.html',
);
const options = {
defaultTargetOptions: {
shouldOptimize: true,
engines: {
browsers: 'last 2 Chrome version',
node: '18',
},
it('should exclude globalThis polyfill in modern builds', async function () {
const entryPoint = path.join(
__dirname,
'integration/html-js-dynamic/index.html',
);
const options = {
mode: 'production',
defaultTargetOptions: {
shouldOptimize: false,
engines: {
browsers: 'last 2 Chrome version',
},
};
const bundleGraph = await runBundler(entryPoint, options);
},
};
const bundleGraph = await runBundler(entryPoint, options);

for (const b of bundleGraph.getBundles()) {
let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8');
console.log(b.name);
console.log(code);
console.log();
console.log();
}
});
for (const b of bundleGraph.getBundles()) {
if (b.type !== 'js') continue;
let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8');
assert.ok(!hasPolyfill(code));
}
});
});

// describe('commonjs', function () {
// it('supports require of commonjs modules', async function () {
// let b = await bundle(
// path.join(
// __dirname,
// '/integration/scope-hoisting/commonjs/require/a.js',
// ),
// );
it('should include globalThis polyfill in ie11 builds', async function () {
const entryPoint = path.join(
__dirname,
'integration/packager-global-this/index.html',
);
const options = {
mode: 'production',
defaultTargetOptions: {
shouldOptimize: false,
engines: {
browsers: 'ie 11',
},
},
};

const bundleGraph = await runBundler(entryPoint, options);

// let output = await run(b);
// assert.equal(output, 2);
// });
// });
for (const b of bundleGraph.getBundles()) {
if (b.type !== 'js') continue;
let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8');
assert.ok(hasPolyfill(code));
}
});

it('should exclude globalThis polyfill in node builds', async function () {
const entryPoint = path.join(
__dirname,
'integration/packager-global-this/index.js',
);
const options = {
mode: 'production',
defaultTargetOptions: {
shouldOptimize: false,
engines: {
browsers: 'node 18',
},
},
};

const bundleGraph = await runBundler(entryPoint, options);

for (const b of bundleGraph.getBundles()) {
if (b.type !== 'js') continue;
let code = await overlayFS.readFile(nullthrows(b.filePath), 'utf8');
assert.ok(!hasPolyfill(code));
}
});
});
});
4 changes: 1 addition & 3 deletions packages/core/test-utils/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function getParcelOptions(
entries: FilePath | Array<FilePath>,
opts?: $Shape<InitialParcelOptions>,
): InitialParcelOptions {
const o = mergeParcelOptions(
return mergeParcelOptions(
{
entries,
shouldDisableCache: true,
Expand All @@ -133,8 +133,6 @@ export function getParcelOptions(
},
opts,
);
console.log(o);
return o;
}

export function bundler(
Expand Down
2 changes: 0 additions & 2 deletions packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,6 @@ ${code}
this.usedHelpers.add('$parcel$global');
}

console.log('um', this.bundle.env.supports('global-this'));

for (let helper of this.usedHelpers) {
let currentHelper = helpers[helper];
if (typeof currentHelper === 'function') {
Expand Down

0 comments on commit dba4b8e

Please sign in to comment.