From 81d26814aece20b7e318cf06a0ea4e98d0a886fa Mon Sep 17 00:00:00 2001 From: Joris Daniel Date: Mon, 23 Dec 2019 22:54:10 +0100 Subject: [PATCH] Remove ignore tags and up to 100 coverage --- src/__tests__/index.test.js | 97 ++++++++++++++++++++++++++++++------- src/__tests__/utils.test.js | 12 ++++- src/index.js | 26 +++------- 3 files changed, 98 insertions(+), 37 deletions(-) diff --git a/src/__tests__/index.test.js b/src/__tests__/index.test.js index 96ecd7c..943dbbc 100644 --- a/src/__tests__/index.test.js +++ b/src/__tests__/index.test.js @@ -79,7 +79,7 @@ beforeEach(() => { }) describe('ChunksWebpackPlugin', () => { - it('should init the constructor function', () => { + it('Initialize the constructor', () => { expect(chunksWebpackPlugin.options).toMatchObject({ outputPath: '/dist/templates', fileExtension: '.html', @@ -91,7 +91,7 @@ describe('ChunksWebpackPlugin', () => { }) }) - it('should init the constructor function without options', () => { + it('Initialize the constructor without options', () => { const instance = new ChunksWebpackPlugin() expect(instance.options).toMatchObject({ outputPath: 'default', @@ -104,7 +104,7 @@ describe('ChunksWebpackPlugin', () => { }) }) - it('should init the apply function', () => { + it('Initialize the apply function', () => { compilerWebpack.hooks.emit.tap = jest.fn() chunksWebpackPlugin.apply(compilerWebpack) @@ -113,7 +113,7 @@ describe('ChunksWebpackPlugin', () => { expect(compilerWebpack.hooks.emit.tap).toHaveBeenCalled() }) - it('should init the hookEmit function', () => { + it('Initialize the hookEmit function', () => { chunksWebpackPlugin.createHtmlChunksFiles = jest.fn() chunksWebpackPlugin.hookEmit(compilationWebpack) @@ -121,7 +121,35 @@ describe('ChunksWebpackPlugin', () => { expect(chunksWebpackPlugin.createHtmlChunksFiles).toHaveBeenCalled() }) - it('should init the hookEmit function with wrong returns of customFormatTags', () => { + it('Initialize the hookEmit function without chunks', () => { + chunksWebpackPlugin.sortsChunksByType = jest.fn() + + compilationWebpack.chunkGroups[0].chunks = [] + chunksWebpackPlugin.hookEmit(compilationWebpack) + + expect(chunksWebpackPlugin.sortsChunksByType).not.toHaveBeenCalled() + }) + + it('Initialize the hookEmit function without generating chunk files', () => { + chunksWebpackPlugin.createHtmlChunksFiles = jest.fn() + + chunksWebpackPlugin.options.generateChunksFiles = false + chunksWebpackPlugin.hookEmit(compilationWebpack) + + expect(chunksWebpackPlugin.createHtmlChunksFiles).not.toHaveBeenCalled() + }) + + it('Initialize the hookEmit function without generating chunk manifest', () => { + chunksWebpackPlugin.createHtmlChunksFiles = jest.fn() + chunksWebpackPlugin.updateManifest = jest.fn() + + chunksWebpackPlugin.options.generateChunksManifest = false + chunksWebpackPlugin.hookEmit(compilationWebpack) + + expect(chunksWebpackPlugin.updateManifest).not.toHaveBeenCalled() + }) + + it('Initialize the hookEmit function with wrong returns of customFormatTags', () => { chunksWebpackPlugin.createHtmlChunksFiles = jest.fn() utils.setError = jest.fn() @@ -132,7 +160,7 @@ describe('ChunksWebpackPlugin', () => { expect(chunksWebpackPlugin.createHtmlChunksFiles).toHaveBeenCalled() }) - it('should init the hookEmit function with wrong declaration of customFormatTags', () => { + it('Initialize the hookEmit function with wrong declaration of customFormatTags', () => { chunksWebpackPlugin.createHtmlChunksFiles = jest.fn() chunksWebpackPlugin.generateTags = jest.fn() @@ -142,7 +170,7 @@ describe('ChunksWebpackPlugin', () => { expect(chunksWebpackPlugin.generateTags).toHaveBeenCalled() }) - it('should init the updateManifest function', () => { + it('Initialize the updateManifest function', () => { updateManifest() expect(chunksWebpackPlugin.manifest).toMatchObject({ @@ -153,33 +181,40 @@ describe('ChunksWebpackPlugin', () => { }) }) - it('should init the getPublicPath function', () => { + it('Initialize the getPublicPath function', () => { const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack) expect(publicPath).toBe('/dist/') }) - it('should init the getPublicPath function with empty value', () => { + it('Initialize the getPublicPath function with empty value', () => { compilationWebpack.options.output.publicPath = false const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack) expect(publicPath).toBe('') }) - it('should init the getOutputPath function', () => { + // it('Initialize the getPublicPath function with publicPath not ending with slash', () => { + // compilationWebpack.options.output.publicPath = false + // const publicPath = chunksWebpackPlugin.getPublicPath(compilationWebpack) + + // expect(publicPath).toBe('') + // }) + + it('Initialize the getOutputPath function', () => { const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack) expect(outputPath).toBe('/dist/templates') }) - it('should init the getOutputPath function with default outputPath', () => { + it('Initialize the getOutputPath function with default outputPath', () => { chunksWebpackPlugin.options.outputPath = 'default' const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack) expect(outputPath).toBe('/dist/') }) - it('should init the getOutputPath function with default outputPath and without value', () => { + it('Initialize the getOutputPath function with default outputPath and without value', () => { compilationWebpack.options.output.path = false chunksWebpackPlugin.options.outputPath = 'default' const outputPath = chunksWebpackPlugin.getOutputPath(compilationWebpack) @@ -187,7 +222,7 @@ describe('ChunksWebpackPlugin', () => { expect(outputPath).toBe('') }) - it('should init the getOutputPath function with wrong outputPath', () => { + it('Initialize the getOutputPath function with wrong outputPath', () => { utils.setError = jest.fn() chunksWebpackPlugin.options.outputPath = '' @@ -196,14 +231,27 @@ describe('ChunksWebpackPlugin', () => { expect(utils.setError).toHaveBeenCalled() }) - it('should init the sortsChunksByType function', () => { + it('Initialize the sortsChunksByType function', () => { expect(chunksSorted).toMatchObject({ styles: ['/dist/css/vendors~app-a~app-b.css'], scripts: ['/dist/js/vendors~app-a~app-b.js'] }) }) - it('should init the generateTags function', () => { + it('Initialize the sortsChunksByType function with scripts only', () => { + let chunksSorted = chunksWebpackPlugin.sortsChunksByType({ + chunks: [{ + files: [ + 'css/vendors~app-a~app-b.css' + ] + }], + publicPath: chunksWebpackPlugin.getPublicPath(compilationWebpack) + }) + + expect(chunksSorted.scripts.length).toEqual(0) + }) + + it('Initialize the generateTags function', () => { const tags = chunksWebpackPlugin.generateTags(chunksSorted) expect(tags).toMatchObject({ @@ -212,7 +260,7 @@ describe('ChunksWebpackPlugin', () => { }) }) - it('should init the createHtmlChunksFiles function', () => { + it('Initialize the createHtmlChunksFiles function', () => { utils.writeFile = jest.fn() chunksWebpackPlugin.createHtmlChunksFiles({ @@ -227,7 +275,22 @@ describe('ChunksWebpackPlugin', () => { expect(utils.writeFile).toHaveBeenCalled() }) - it('should init the createChunksManifestFile function', () => { + it('Initialize the createHtmlChunksFiles function without styles and scripts', () => { + utils.writeFile = jest.fn() + + chunksWebpackPlugin.createHtmlChunksFiles({ + entry: 'app-a', + tagsHTML: { + styles: '', + scripts: '' + }, + outputPath: '/dist/' + }) + + expect(utils.writeFile).not.toHaveBeenCalled() + }) + + it('Initialize the createChunksManifestFile function', () => { updateManifest() chunksWebpackPlugin.createChunksManifestFile({ diff --git a/src/__tests__/utils.test.js b/src/__tests__/utils.test.js index 7a52dbe..d28e146 100644 --- a/src/__tests__/utils.test.js +++ b/src/__tests__/utils.test.js @@ -4,13 +4,21 @@ import utils from '../utils' import fse from 'fs-extra' describe('Utils', () => { - it('should init the setError function', () => { + it('Initialize the setError function', () => { expect(() => { utils.setError('message') }).toThrow(new Error('message')) }) - it('should init the writeFile function', () => { + it('Initialize the isAbsolutePath function', () => { + expect(utils.isAbsolutePath('dist')).toBe(false) + }) + + it('Initialize the getFileExtension function', () => { + expect(utils.getFileExtension('css/vendors~app-a~app-b.css')).toBe('css') + }) + + it('Initialize the writeFile function', () => { fse.outputFileSync = jest.fn() utils.writeFile({ diff --git a/src/index.js b/src/index.js index 85f5b68..5152789 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,6 @@ module.exports = class ChunksWebpackPlugin { compilation.chunkGroups.forEach(chunkGroup => { // Check if chunkGroup contains chunks - /* istanbul ignore else */ if (chunkGroup.chunks.length) { const entryName = chunkGroup.options.name let chunksSorted = this.sortsChunksByType({ @@ -59,7 +58,6 @@ module.exports = class ChunksWebpackPlugin { }) // Check if chunks files generation is enabled - /* istanbul ignore else */ if (this.options.generateChunksFiles) { let tagsHTML = null @@ -85,7 +83,6 @@ module.exports = class ChunksWebpackPlugin { } // Check if manifest option is enabled - /* istanbul ignore else */ if (this.options.generateChunksManifest) { this.updateManifest({ entryName: entryName, @@ -96,7 +93,6 @@ module.exports = class ChunksWebpackPlugin { }) // Check if manifest option is enabled - /* istanbul ignore else */ if (this.options.generateChunksManifest) { this.createChunksManifestFile({ compilation, outputPath }) } @@ -126,12 +122,8 @@ module.exports = class ChunksWebpackPlugin { getPublicPath (compilation) { let publicPath = compilation.options.output.publicPath || '' - /* istanbul ignore else */ - if (publicPath) { - /* istanbul ignore else */ - if (publicPath.substr(-1) !== '/') { - publicPath = `${publicPath}/` - } + if (publicPath && publicPath.substr(-1) !== '/') { + publicPath = `${publicPath}/` } return publicPath @@ -175,15 +167,15 @@ module.exports = class ChunksWebpackPlugin { 'styles': [], 'scripts': [] } + let extensionKeys = { + css: 'styles', + js: 'scripts' + } chunks.forEach(chunk => { chunk.files.forEach(file => { - /* istanbul ignore else */ - if (utils.getFileExtension(file) === 'css') { - files['styles'].push(`${publicPath}${file}`) - } else if (utils.getFileExtension(file) === 'js') { - files['scripts'].push(`${publicPath}${file}`) - } + let extension = utils.getFileExtension(file) + files[extensionKeys[extension]].push(`${publicPath}${file}`) }) }) @@ -226,14 +218,12 @@ module.exports = class ChunksWebpackPlugin { tagsHTML, outputPath }) { - /* istanbul ignore else */ if (tagsHTML.scripts.length) { utils.writeFile({ outputPath: `${outputPath}/${entry}-scripts${this.options.fileExtension}`, output: tagsHTML.scripts }) } - /* istanbul ignore else */ if (tagsHTML.styles.length) { utils.writeFile({ outputPath: `${outputPath}/${entry}-styles${this.options.fileExtension}`,