Skip to content

Commit 05f66f4

Browse files
committed
test: add test to reproduce the issue #190
1 parent 934de27 commit 05f66f4

7 files changed

Lines changed: 76 additions & 6 deletions

File tree

test/issue.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import os from 'os';
33
import path from 'path';
44
import webpack from 'webpack';
55
import HtmlBundlerPlugin from '../src';
6-
import { compareFiles } from './utils/helpers';
6+
import { compareFiles, compareFilesRuns } from './utils/helpers';
77

88
beforeAll(() => {
99
// important: the environment constant is used in code
@@ -25,6 +25,22 @@ describe('issue tests', () => {
2525

2626
test('issue infinity walk by circular dependency', () => compareFiles('issue-mui-css'));
2727

28+
test('filesystem cache restores script on second run, issue #190', async () => {
29+
const testPath = path.join(__dirname, 'issues/issue-190-cache-filesystem-js');
30+
const cleanup = () => {
31+
fs.rmSync(path.join(testPath, 'dist'), { recursive: true, force: true });
32+
fs.rmSync(path.join(testPath, '.cache'), { recursive: true, force: true });
33+
};
34+
35+
cleanup();
36+
37+
try {
38+
await compareFilesRuns('../issues/issue-190-cache-filesystem-js', false, 2);
39+
} finally {
40+
cleanup();
41+
}
42+
});
43+
2844
test('multi compiler invalid hook handles null filename, issue #191', () => {
2945
const context = fs.mkdtempSync(path.join(os.tmpdir(), 'html-bundler-webpack-plugin-'));
3046
const sourcePath = path.join(context, 'src');
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<h1>Hello World!</h1>
8+
<script src="main.6ad9992e.js"></script>
9+
</body>
10+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello World! Test: 123");
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<h1>Hello World!</h1>
8+
<script src="main.js"></script>
9+
</body>
10+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello World! Test: 123');
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const path = require('path');
2+
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
3+
4+
module.exports = {
5+
mode: 'production',
6+
7+
output: {
8+
path: path.join(__dirname, 'dist/'),
9+
clean: false,
10+
},
11+
12+
plugins: [
13+
new HtmlBundlerPlugin({
14+
entry: {
15+
index: {
16+
import: './src/index.html',
17+
filename: () => '[name].html',
18+
},
19+
},
20+
21+
js: {
22+
filename: () => '[name].[contenthash:8].js',
23+
},
24+
}),
25+
],
26+
27+
cache: {
28+
type: 'filesystem',
29+
cacheDirectory: path.join(__dirname, '.cache'),
30+
},
31+
};

test/utils/helpers.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ export const compareFilesRuns = (relTestCasePath, compareContent = true, num = 1
133133
const results = [];
134134
const expected = Array(num).fill(true);
135135
const filter = /.(html|css|css.map|js|js.map|json)$/;
136+
let promise = Promise.resolve();
136137

137138
for (let i = 0; i < num; i++) {
138-
const res = compile(PATHS, relTestCasePath, {})
139+
promise = promise
140+
.then(() => compile(PATHS, relTestCasePath, {}))
139141
.then(() => {
140142
const { received: receivedFiles, expected: expectedFiles } = getCompareFileList(webRootPath, expectedPath);
141143
expect(receivedFiles).toEqual(expectedFiles);
@@ -146,15 +148,14 @@ export const compareFilesRuns = (relTestCasePath, compareContent = true, num = 1
146148
});
147149
}
148150

149-
return Promise.resolve(true);
151+
results.push(true);
150152
})
151153
.catch((error) => {
152-
return Promise.reject(new Error(error.stack));
154+
return Promise.reject(new Error(error.stack ? error.stack : error));
153155
});
154-
results.push(res);
155156
}
156157

157-
return expect(Promise.all(results)).resolves.toEqual(expected);
158+
return expect(promise.then(() => results)).resolves.toEqual(expected);
158159
};
159160

160161
/**

0 commit comments

Comments
 (0)