MCPcopy Create free account
hub / github.com/jantimon/html-webpack-plugin / testHtmlPlugin

Function testHtmlPlugin

spec/BasicSpec.js:38–87  ·  view source on GitHub ↗
(webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings)

Source from the content-addressed store, hash-verified

36jasmine.getEnv().defaultTimeoutInterval = 30000;
37
38function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings) {
39 outputFile = outputFile || 'index.html';
40 webpack(webpackConfig, function (err, stats) {
41 expect(err).toBeFalsy();
42 var compilationErrors = (stats.compilation.errors || []).join('\n');
43 if (expectErrors) {
44 expect(compilationErrors).not.toBe('');
45 } else {
46 expect(compilationErrors).toBe('');
47 }
48 var compilationWarnings = (stats.compilation.warnings || []).join('\n');
49 if (expectWarnings) {
50 expect(compilationWarnings).not.toBe('');
51 } else {
52 expect(compilationWarnings).toBe('');
53 }
54 if (outputFile instanceof RegExp) {
55 var matches = Object.keys(stats.compilation.assets).filter(function (item) {
56 return outputFile.test(item);
57 });
58 expect(matches.length).toBe(1);
59 outputFile = matches[0];
60 }
61 expect(outputFile.indexOf('[hash]') === -1).toBe(true);
62 var outputFileExists = fs.existsSync(path.join(OUTPUT_DIR, outputFile));
63 expect(outputFileExists).toBe(true);
64 if (!outputFileExists) {
65 return done();
66 }
67 var htmlContent = fs.readFileSync(path.join(OUTPUT_DIR, outputFile)).toString();
68 var chunksInfo;
69 for (var i = 0; i < expectedResults.length; i++) {
70 var expectedResult = expectedResults[i];
71 if (expectedResult instanceof RegExp) {
72 expect(htmlContent).toMatch(expectedResult);
73 } else if (typeof expectedResult === 'object') {
74 if (expectedResult.type === 'chunkhash') {
75 if (!chunksInfo) {
76 chunksInfo = getChunksInfoFromStats(stats);
77 }
78 var chunkhash = chunksInfo[expectedResult.chunkName].hash;
79 expect(htmlContent).toContain(expectedResult.containStr.replace('%chunkhash%', chunkhash));
80 }
81 } else {
82 expect(htmlContent).toContain(expectedResult.replace('%hash%', stats.hash));
83 }
84 }
85 done();
86 });
87}
88
89function getChunksInfoFromStats (stats) {
90 var chunks = stats.compilation.getStats().toJson().chunks;

Callers 1

BasicSpec.jsFile · 0.85

Calls 1

getChunksInfoFromStatsFunction · 0.85

Tested by

no test coverage detected