Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/rollup-plugin-polyfills-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
"node": ">=22.0.0"
},
"scripts": {
"test:node": "mocha test/**/*.test.ts --require ts-node/register --reporter dot",
"test:update-snapshots": "mocha test/**/*.test.ts --require ts-node/register --update-snapshots",
"test:watch": "mocha test/**/*.test.ts --require ts-node/register --watch --watch-files src,test"
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
},
"files": [
"*.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { createPolyfillsLoaderConfig } from '../../src/createPolyfillsLoaderConfig.js';
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { createPolyfillsLoaderConfig } from '../../dist/createPolyfillsLoaderConfig.js';

describe('createPolyfillsLoaderConfig()', () => {
it('creates a config for a single module build', () => {
Expand All @@ -12,7 +13,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, bundle);

expect(config).to.eql({
assert.deepEqual(config, {
legacy: undefined,
modern: { files: [{ path: 'app.js', type: 'module', attributes: [] }] },
polyfills: undefined,
Expand All @@ -33,7 +34,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, bundle);

expect(config).to.eql({
assert.deepEqual(config, {
legacy: undefined,
modern: {
files: [
Expand All @@ -59,7 +60,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, bundle);

expect(config).to.eql({
assert.deepEqual(config, {
legacy: undefined,
modern: { files: [{ path: 'app.js', type: 'systemjs', attributes: [] }] },
polyfills: undefined,
Expand All @@ -86,7 +87,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, undefined, bundles);

expect(config).to.eql({
assert.deepEqual(config, {
modern: { files: [{ path: 'app.js', type: 'module', attributes: [] }] },
legacy: [
{
Expand Down Expand Up @@ -125,7 +126,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, undefined, bundles);

expect(config).to.eql({
assert.deepEqual(config, {
modern: { files: [{ path: 'app.js', type: 'module', attributes: [] }] },
legacy: [
{
Expand Down Expand Up @@ -165,7 +166,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, undefined, bundles);

expect(config).to.eql({
assert.deepEqual(config, {
modern: { files: [{ path: 'app.js', type: 'script', attributes: [] }] },
legacy: [
{
Expand All @@ -190,7 +191,7 @@ describe('createPolyfillsLoaderConfig()', () => {
// @ts-ignore
const config = createPolyfillsLoaderConfig(pluginConfig, bundle);

expect(config).to.eql({
assert.deepEqual(config, {
legacy: undefined,
modern: { files: [{ path: 'app.js', type: 'module', attributes: [] }] },
polyfills: { fetch: true, webcomponents: true },
Expand All @@ -210,7 +211,7 @@ describe('createPolyfillsLoaderConfig()', () => {

// @ts-ignore
const action = () => createPolyfillsLoaderConfig(pluginConfig, bundle);
expect(action).to.throw();
assert.throws(action);
});

it('throws when a multiple builds are output while no builds are configured', () => {
Expand All @@ -228,7 +229,7 @@ describe('createPolyfillsLoaderConfig()', () => {

// @ts-ignore
const action = () => createPolyfillsLoaderConfig(pluginConfig, undefined, bundles);
expect(action).to.throw();
assert.throws(action);
});

it('throws when the modern build could not be found', () => {
Expand All @@ -249,6 +250,6 @@ describe('createPolyfillsLoaderConfig()', () => {

// @ts-ignore
const action = () => createPolyfillsLoaderConfig(pluginConfig, undefined, bundles);
expect(action).to.throw();
assert.throws(action);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/* eslint-disable no-await-in-loop */
import { OutputChunk, rollup, OutputAsset, RollupOptions, OutputOptions } from 'rollup';
import { expect } from 'chai';
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { rollup } from 'rollup';
import type { OutputChunk, OutputAsset, RollupOptions, OutputOptions } from 'rollup';
import fs from 'fs';
import path from 'path';
import { rollupPluginHTML as html } from '@web/rollup-plugin-html';
import polyfillsLoader from '../../src/index.js';
import { polyfillsLoader } from '../../dist/index.js';

type Output = (OutputChunk | OutputAsset)[];

const relativeUrl = `./${path.relative(process.cwd(), path.join(__dirname, '..'))}`;
const relativeUrl = `./${path.relative(process.cwd(), path.join(import.meta.dirname, '..'))}`;

const updateSnapshots = process.argv.includes('--update-snapshots');

Expand All @@ -26,7 +28,7 @@ interface SnapshotArgs {
}

async function testSnapshot({ name, fileName, inputOptions, outputOptions }: SnapshotArgs) {
const snapshotPath = path.join(__dirname, '..', 'snapshots', `${name}.html`);
const snapshotPath = path.join(import.meta.dirname, '..', 'snapshots', `${name}.html`);
const bundle = await rollup(inputOptions);
let output;
for (const outputConfig of outputOptions) {
Expand All @@ -42,8 +44,7 @@ async function testSnapshot({ name, fileName, inputOptions, outputOptions }: Sna
fs.writeFileSync(snapshotPath, file.source, 'utf-8');
} else {
const snapshot = fs.readFileSync(snapshotPath, 'utf-8');
expect(file.source.trim()).to.equal(snapshot.trim());
// expect(file.source.replace(/\s/g, '')).to.equal(snapshot.replace(/\s/g, ''));
assert.equal(file.source.trim(), snapshot.trim());
}
return output;
}
Expand All @@ -55,10 +56,7 @@ const defaultOutputOptions: OutputOptions[] = [
},
];

describe('rollup-plugin-polyfills-loader', function describe() {
// bootup of the first test can take a long time in CI to load all the polyfills
this.timeout(5000);

describe('rollup-plugin-polyfills-loader', { timeout: 5000 }, () => {
it('can inject a polyfills loader with a single output', async () => {
const inputOptions: RollupOptions = {
plugins: [
Expand Down Expand Up @@ -225,8 +223,8 @@ describe('rollup-plugin-polyfills-loader', function describe() {
outputOptions: defaultOutputOptions,
});

expect(output.find(o => o.fileName.startsWith('polyfills/webcomponents'))).to.exist;
expect(output.find(o => o.fileName.startsWith('polyfills/fetch'))).to.exist;
assert.ok(output.find(o => o.fileName.startsWith('polyfills/webcomponents')));
assert.ok(output.find(o => o.fileName.startsWith('polyfills/fetch')));
});

it('can inject with multiple build outputs', async () => {
Expand Down
30 changes: 18 additions & 12 deletions packages/rollup-plugin-polyfills-loader/test/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
import { expect } from 'chai';
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { fileTypes } from '@web/polyfills-loader';
import { shouldInjectLoader } from '../../src/utils.js';
import { shouldInjectLoader } from '../../dist/utils.js';

describe('shouldInjectLoader', () => {
it('returns true when modern contains non-module or script', () => {
expect(
assert.equal(
shouldInjectLoader({
modern: { files: [{ type: fileTypes.SYSTEMJS, path: '' }] },
}),
).to.equal(true);
true,
);
});

it('returns true when there are legacy files', () => {
expect(
assert.equal(
shouldInjectLoader({
modern: { files: [{ type: fileTypes.MODULE, path: '' }] },
legacy: [{ test: '', files: [{ type: fileTypes.SYSTEMJS, path: '' }] }],
}),
).to.equal(true);
true,
);
});

it('returns true when there are polyfills', () => {
expect(
assert.equal(
shouldInjectLoader({
modern: { files: [{ type: fileTypes.MODULE, path: '' }] },
polyfills: {
fetch: true,
},
}),
).to.equal(true);
true,
);

expect(
assert.equal(
shouldInjectLoader({
modern: { files: [{ type: fileTypes.MODULE, path: '' }] },
polyfills: {
regeneratorRuntime: 'always',
},
}),
).to.equal(true);
true,
);
});

it('returns true when there are custom polyfills', () => {
expect(
assert.equal(
shouldInjectLoader({
modern: { files: [{ type: fileTypes.MODULE, path: '' }] },
polyfills: {
custom: [{ test: '', path: '', name: '' }],
},
}),
).to.equal(true);
true,
);
});
});
Loading