Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianS90 committed Dec 24, 2017
1 parent f4d7039 commit e34c023
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function getLoaderOptions(pluginState, loaderOptions) {
}

if (typeof options.polyfills === 'string') {
options.modules = [options.polyfills];
options.polyfills = [options.polyfills];
} else if (!Array.isArray(options.polyfills) || options.polyfills.length === 0) {
throw new Error('[webpack-polyfill-injector] You need to specify the `polyfills` option!');
}
Expand Down
182 changes: 109 additions & 73 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,92 +302,121 @@ describe('webpack-polyfill-injector', () => {


describe('When configuring multiple entry modules', () => {
let state; // eslint-disable-line init-declarations
before(async () => {
state = await test({
entry: {
app: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry.js',
'./test/fixtures/entry2.js',
],
polyfills: ['Promise'],
})}!`,
other: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry3.js',
],
polyfills: ['Array.prototype.find'],
})}!`,
},
plugins: [new PolyfillInjectorPlugin({polyfills: ['Promise', 'Array.prototype.find']})],
testHooks: {
executeBundle({window}) {
window.eval('delete window.Promise');
window.eval('delete window.Array.prototype.find');
describe('with different polyfills', () => {
let state; // eslint-disable-line init-declarations
before(async () => {
state = await test({
entry: {
app: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry.js',
'./test/fixtures/entry2.js',
],
polyfills: ['Promise'],
})}!`,
other: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry3.js',
],
polyfills: ['Array.prototype.find'],
})}!`,
},
plugins: [new PolyfillInjectorPlugin({polyfills: ['Promise', 'Array.prototype.find']})],
testHooks: {
executeBundle({window}) {
window.eval('delete window.Promise');
window.eval('delete window.Array.prototype.find');
},
},
},
});
});
});

it('creates two assets: app.js, other.js, polyfills.js, polyfills-1.js', () => {
const assets = state.stats.compilation.assets;
expect(Object.keys(assets)).to.have.lengthOf(4);
expect(assets).to.have.property('app.js');
expect(assets).to.have.property('polyfills.js');
expect(assets).to.have.property('other.js');
expect(assets).to.have.property('polyfills-1.js');
});
it('creates four assets: app.js, other.js, polyfills.js, polyfills-1.js', () => {
const assets = state.stats.compilation.assets;
expect(Object.keys(assets)).to.have.lengthOf(4);
expect(assets).to.have.property('app.js');
expect(assets).to.have.property('polyfills.js');
expect(assets).to.have.property('other.js');
expect(assets).to.have.property('polyfills-1.js');
});

it('does not mix the polyfill configurations', () => {
const polyfills2 = state.fs.readFileSync('/polyfills-1.js', 'utf-8');
expect(state.polyfillsFile).to.contain('Promise');
expect(state.polyfillsFile).to.not.contain('Array.prototype.find');
expect(polyfills2).to.not.contain('Promise');
expect(polyfills2).to.contain('Array.prototype.find');
});
it('does not mix the polyfill configurations', () => {
const polyfills2 = state.fs.readFileSync('/polyfills-1.js', 'utf-8');
expect(state.polyfillsFile).to.contain('Promise');
expect(state.polyfillsFile).to.not.contain('Array.prototype.find');
expect(polyfills2).to.not.contain('Promise');
expect(polyfills2).to.contain('Array.prototype.find');
});

it('does not guard the polyfills', () => {
const polyfills2 = state.fs.readFileSync('/polyfills-1.js', 'utf-8');
expect(state.polyfillsFile).to.not.contain(`'Promise' in this`);
expect(polyfills2).to.not.contain(`'find' in Array.prototype`);
});
it('does not guard the polyfills', () => {
const polyfills2 = state.fs.readFileSync('/polyfills-1.js', 'utf-8');
expect(state.polyfillsFile).to.not.contain(`'Promise' in this`);
expect(polyfills2).to.not.contain(`'find' in Array.prototype`);
});

it('executes the "app" entry module correctly', () => {
expect(state.window).to.have.property('Promise');
expect(state.window.Array.prototype).to.not.have.property('find');
expect(state.dom.serialize())
.to.contain('<p>---SUCCESS---</p><p>[entry2]function[/entry2]</p>')
.and.to.not.contain('entry3');
});

it('executes the "app" entry module correctly', () => {
expect(state.window).to.have.property('Promise');
expect(state.window.Array.prototype).to.not.have.property('find');
expect(state.dom.serialize())
.to.contain('<p>---SUCCESS---</p><p>[entry2]function[/entry2]</p>')
.and.to.not.contain('entry3');
it('executes the "other" entry module correctly', () => {
const dom = createDOM();
dom.window.eval('delete window.Promise');
dom.window.eval('delete window.Array.prototype.find');
dom.window.eval(state.fs.readFileSync('/other.js', 'utf-8'));
executeScript(dom, state.fs);
expect(dom.window).to.not.have.property('Promise');
expect(dom.window.Array.prototype).to.have.property('find');
expect(dom.serialize())
.to.contain('<p>[entry3]function[/entry3]</p>')
.and.to.not.contain('---SUCCESS---')
.and.to.not.contain('entry2');
});
});

it('executes the "other" entry module correctly', () => {
const dom = createDOM();
dom.window.eval('delete window.Promise');
dom.window.eval('delete window.Array.prototype.find');
dom.window.eval(state.fs.readFileSync('/other.js', 'utf-8'));
executeScript(dom, state.fs);
expect(dom.window).to.not.have.property('Promise');
expect(dom.window.Array.prototype).to.have.property('find');
expect(dom.serialize())
.to.contain('<p>[entry3]function[/entry3]</p>')
.and.to.not.contain('---SUCCESS---')
.and.to.not.contain('entry2');
describe('with the same polyfills', () => {
it('creates five assets: app.js, other.js, polyfills.01.js, polyfills.10.js, polyfills.11.js', async () => {
const state = await test({
entry: {
app: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry.js',
'./test/fixtures/entry2.js',
],
polyfills: ['Promise', 'Array.prototype.find'],
})}!`,
other: `webpack-polyfill-injector?${JSON.stringify({
modules: [
'./test/fixtures/entry3.js',
],
polyfills: ['Promise', 'Array.prototype.find'],
})}!`,
},
plugins: [new PolyfillInjectorPlugin()],
});
const assets = state.stats.compilation.assets;
expect(Object.keys(assets)).to.have.lengthOf(5);
expect(assets).to.have.property('app.js');
expect(assets).to.have.property('polyfills.01.js');
expect(assets).to.have.property('polyfills.10.js');
expect(assets).to.have.property('polyfills.11.js');
expect(assets).to.have.property('other.js');
});
});
});


describe('When using [hash] as part of the filename', () => {
let state; // eslint-disable-line init-declarations
before(async () => {
state = await compile({
it('creates hashed polyfills files', async () => {
const state = await compile({
output: {filename: '[name].[hash].js'},
entry: {app: 'webpack-polyfill-injector?{modules:["./test/fixtures/entry.js"]}!'},
plugins: [new PolyfillInjectorPlugin({polyfills: ['Promise', 'Array.prototype.find']})],
});
});

it('creates hashed polyfills files', () => {
const polyfillFiles = state.fs.readdirSync('/').filter(name => name.startsWith('polyfills'));
expect(polyfillFiles).to.have.lengthOf(3);
const hash = polyfillFiles[0].replace(/^polyfills\.(.*)\.[01][01]\.js$/, '$1');
Expand All @@ -403,16 +432,13 @@ describe('webpack-polyfill-injector', () => {


describe('When using [chunkhash] as part of the filename', () => {
let state; // eslint-disable-line init-declarations
before(async () => {
state = await compile({
it('creates hashed polyfills files', async () => {
const state = await compile({
output: {filename: '[name].[chunkhash:7].js'},
entry: {app: 'webpack-polyfill-injector?{modules:["./test/fixtures/entry.js"]}!'},
plugins: [new PolyfillInjectorPlugin({polyfills: ['Promise', 'Array.prototype.find']})],
});
});

it('creates hashed polyfills files', () => {
const polyfillFiles = state.fs.readdirSync('/').filter(name => name.startsWith('polyfills'));
expect(polyfillFiles).to.have.lengthOf(3);
const hash = polyfillFiles[0].replace(/^polyfills\.(.*)\.[01][01]\.js$/, '$1');
Expand All @@ -427,6 +453,16 @@ describe('webpack-polyfill-injector', () => {
});


describe('When configuring strings instead of arrays', () => {
it('interprets them as arrays', () => {
expect(compile({
entry: {app: 'webpack-polyfill-injector?{modules:"./test/fixtures/entry.js"}!'},
plugins: [new PolyfillInjectorPlugin({polyfills: 'Promise'})],
})).to.be.fulfilled();
});
});


describe('When using an illegal configuration', () => {
describe('Plugin without loader', () => {
it('yields a useful error message', () =>
Expand Down

0 comments on commit e34c023

Please sign in to comment.