diff --git a/package.json b/package.json index 87737b9..1322f02 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,8 @@ "style-loader": "^0.13.0", "underscore-template-loader": "^0.7.2", "url-loader": "^0.5.7", - "webpack": "^1.12.14" + "webpack": "^1.12.14", + "webpack-recompilation-simulator": "^1.3.0" }, "dependencies": { "bluebird": "^3.3.4", diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/BasicSpec.js similarity index 99% rename from spec/HtmlWebpackPluginSpec.js rename to spec/BasicSpec.js index bf408c3..af57640 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/BasicSpec.js @@ -1,3 +1,7 @@ +/* + * Integration and unit tests for all features but caching + */ + /* eslint-env jasmine */ 'use strict'; diff --git a/spec/CachingSpec.js b/spec/CachingSpec.js new file mode 100644 index 0000000..a70723d --- /dev/null +++ b/spec/CachingSpec.js @@ -0,0 +1,109 @@ +/* + * Integration tests for caching + */ + +/* eslint-env jasmine */ +'use strict'; + +// Polyfill promisses for node 0.10.x +if (!global.Promise) { + require('es6-promise').polyfill(); +} + +var path = require('path'); +var webpack = require('webpack'); +var rm_rf = require('rimraf'); +var WebpackRecompilationSimulator = require('webpack-recompilation-simulator'); +var HtmlWebpackPlugin = require('../index.js'); + +var OUTPUT_DIR = path.join(__dirname, '../dist'); + +function setUpCompiler (htmlWebpackPlugin) { + spyOn(htmlWebpackPlugin, 'evaluateCompilationResult').and.callThrough(); + var compiler = new WebpackRecompilationSimulator(webpack({ + entry: path.join(__dirname, 'fixtures/index.js'), + output: { + path: OUTPUT_DIR, + filename: 'index_bundle.js' + }, + plugins: [htmlWebpackPlugin] + })); + return compiler; +} + +describe('HtmlWebpackPluginCaching', function () { + beforeEach(function (done) { + rm_rf(OUTPUT_DIR, done); + }); + + it('should not compile the webpack html file if only a javascript file was changed', function (done) { + var htmlWebpackPlugin = new HtmlWebpackPlugin(); + var compiler = setUpCompiler(htmlWebpackPlugin); + var childCompilerHash; + compiler.run() + // Change a js file and compile again + .then(function () { + childCompilerHash = htmlWebpackPlugin.childCompilerHash; + compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), {footer: '//1'}); + return compiler.run(); + }) + .then(function () { + // Verify that the html was processed only once + expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) + .toBe(1); + // Verify that the child compilation was executed only once + expect(htmlWebpackPlugin.childCompilerHash) + .toBe(childCompilerHash); + }) + .then(done); + }); + + it('should compile the webpack html file even if only a javascript file was changed if caching is disabled', function (done) { + var htmlWebpackPlugin = new HtmlWebpackPlugin({ + cache: false + }); + var childCompilerHash; + var compiler = setUpCompiler(htmlWebpackPlugin); + compiler.run() + // Change a js file and compile again + .then(function () { + childCompilerHash = htmlWebpackPlugin.childCompilerHash; + compiler.simulateFileChange(path.join(__dirname, 'fixtures/index.js'), {footer: '//1'}); + return compiler.run(); + }) + .then(function () { + // Verify that the html was processed on every run + expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) + .toBe(2); + // Verify that the child compilation was executed only once + expect(htmlWebpackPlugin.childCompilerHash) + .toBe(childCompilerHash); + }) + .then(done); + }); + + it('should compile the webpack html if the template file was changed', function (done) { + var template = path.join(__dirname, 'fixtures/plain.html'); + var htmlWebpackPlugin = new HtmlWebpackPlugin({ + template: template + }); + var childCompilerHash; + var compiler = setUpCompiler(htmlWebpackPlugin); + compiler.run() + // Change the template file and compile again + .then(function () { + childCompilerHash = htmlWebpackPlugin.childCompilerHash; + compiler.simulateFileChange(template, {footer: ''}); + return compiler.run(); + }) + .then(function () { + // Verify that the html was processed twice + expect(htmlWebpackPlugin.evaluateCompilationResult.calls.count()) + .toBe(2); + // Verify that the child compilation was executed twice + expect(htmlWebpackPlugin.childCompilerHash) + .not.toBe(childCompilerHash); + }) + .then(done); + }); +}); diff --git a/spec/ExampleSpec.js b/spec/ExampleSpec.js index 7588020..aec6f56 100644 --- a/spec/ExampleSpec.js +++ b/spec/ExampleSpec.js @@ -1,4 +1,9 @@ -/* global describe, it, expect */ +/* + * These integration tests compile all cases from the example folder + * and matches them against their dist folder + */ + + /* eslint-env jasmine */ 'use strict'; // Workaround for css-loader issue