Skip to content

Commit 005cb81

Browse files
test: standardize
1 parent ef752f0 commit 005cb81

36 files changed

+704
-184
lines changed

test/Errors.test.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
1-
/* eslint-disable
2-
no-eval,
3-
import/order,
4-
arrow-parens,
5-
prefer-destructuring,
6-
*/
7-
import loader from '../src/';
8-
9-
import webpack from './webpack';
10-
import { stats } from './helpers';
1+
/* eslint-disable */
2+
import loader from '../src';
3+
import webpack from './helpers/compiler';
114

125
describe('Errors', () => {
13-
test('ValidationError', () => {
14-
const err = () => loader.call({ query: { root: 1 } }, '<html></html>');
6+
test('Loader Error', async () => {
7+
const config = {
8+
loader: {
9+
test: /\.html$/,
10+
options: {
11+
minimize: true
12+
}
13+
}
14+
};
15+
16+
const stats = await webpack('error.js', config);
17+
const { source } = stats.toJson().modules[1];
18+
19+
// eslint-disable-next-line
20+
const err = () => eval(source);
1521

1622
expect(err).toThrow();
1723
expect(err).toThrowErrorMatchingSnapshot();
1824
});
1925

20-
test('LoaderError', () => {
21-
const config = {};
26+
test('Validation Error', () => {
27+
const err = () => loader.call({ query: { template: 1 } });
2228

23-
return webpack('index.js', config)
24-
.then((result) => stats(result))
25-
.then(({ loaders }) => {
26-
expect(() => eval(loaders.err)).toThrow();
27-
expect(() => eval(loaders.err)).toThrowErrorMatchingSnapshot();
28-
})
29-
.catch((err) => err);
29+
expect(err).toThrow();
30+
expect(err).toThrowErrorMatchingSnapshot();
3031
});
3132
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Errors Loader Error 1`] = `
4+
"Module build failed: HTML Loader:
5+
6+
tree.forEach is not a function
7+
"
8+
`;
9+
10+
exports[`Errors Validation Error 1`] = `
11+
"HTML Loader Invalid Options
12+
13+
options.template should be boolean,string
14+
"
15+
`;
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`HTML Loader Should export as ES2015 Module 1`] = `"export default \\"<p>Hello world!</p>\\";"`;
4-
5-
exports[`HTML Loader Should not remove attributes by default 1`] = `"export default \\"<input type=text />\\";"`;
6-
7-
exports[`HTML Loader Should process HTML with defaults 1`] = `"export default \\"<!DOCTYPE html>\\\\n<html lang=\\\\\\"en\\\\\\">\\\\n<head>\\\\n <meta charset=\\\\\\"utf-8\\\\\\">\\\\n <title>HTML Loader</title>\\\\n</head>\\\\n<body>\\\\n <!-- I'm a comment -->\\\\n <div id=\\\\\\"app\\\\\\"></div>\\\\n</body>\\\\n</html>\\\\n\\";"`;
8-
9-
exports[`HTML Loader Should process HTML with options.attrs 1`] = `"throw new Error(\\"Module parse failed: /Users/Cini/Github/Webpack/webpack-loaders/html-loader/src/cjs.js??ref--0-0!/Users/Cini/Github/Webpack/webpack-loaders/html-loader/test/fixtures/attrs.html Unexpected token (1:33)\\\\nYou may need an appropriate loader to handle this file type.\\\\n| export default \\\\\\"<div data-attrs=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"foo\\\\\\\\\\\\\\"\\\\\\"')\\\\\\"></div>\\\\\\\\n\\\\\\\\n<custom-element custom-attrs=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"foo\\\\\\\\\\\\\\"\\\\\\"')\\\\\\"></custom-element>\\\\\\\\n<custom-container custom-attrs=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"foo\\\\\\\\\\\\\\"\\\\\\"')\\\\\\"></custom-container>\\\\\\\\n\\\\\\";\\");"`;
10-
11-
exports[`HTML Loader Should process HTML with options.interpolate {Boolean} 1`] = `"throw new Error(\\"Module parse failed: /Users/Cini/Github/Webpack/webpack-loaders/html-loader/src/cjs.js??ref--0-0!/Users/Cini/Github/Webpack/webpack-loaders/html-loader/test/fixtures/interpolate.html Unexpected token (1:15)\\\\nYou may need an appropriate loader to handle this file type.\\\\n| export default <img src=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"\${img.src}\\\\\\\\\\\\\\"\\\\\\"')\\\\\\" alt=\\\\\\"\${img.alt}\\\\\\">\\\\n| <img src=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"\${require(./image.png)}\\\\\\\\\\\\\\"\\\\\\"')\\\\\\">\\\\n| ;\\");"`;
12-
13-
exports[`HTML Loader Should process HTML with options.interpolate {String} 1`] = `"throw new Error(\\"Module parse failed: /Users/Cini/Github/Webpack/webpack-loaders/html-loader/src/cjs.js??ref--0-0!/Users/Cini/Github/Webpack/webpack-loaders/html-loader/test/fixtures/interpolate.html Unexpected token (1:26)\\\\nYou may need an appropriate loader to handle this file type.\\\\n| export default \\\\\\"<img src=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"\${img.src}\\\\\\\\\\\\\\"\\\\\\"')\\\\\\" alt=\\\\\\\\\\\\\\"\${img.alt}\\\\\\\\\\\\\\">\\\\\\\\n<img src=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"\${require(./image.png)}\\\\\\\\\\\\\\"\\\\\\"')\\\\\\">\\\\\\\\n\\\\\\";\\");"`;
14-
15-
exports[`HTML Loader Should process HTML with options.minimize {Boolean} 1`] = `"export default \\"<!DOCTYPE html> <html lang=en> <head> <meta charset=utf-8> <title>HTML Loader</title> </head> <body> <div id=app></div> </body> </html> \\";"`;
16-
17-
exports[`HTML Loader Should process HTML with options.minimize {Object} 1`] = `"export default \\"<!DOCTYPE html> <html lang=en> <head> <meta charset=utf-8> <title>HTML Loader</title> </head> <body> <!-- I'm a comment --> <div id=app></div> </body> </html> \\";"`;
18-
19-
exports[`HTML Loader Should process HTML with options.root 1`] = `"throw new Error(\\"Module parse failed: /Users/Cini/Github/Webpack/webpack-loaders/html-loader/src/cjs.js??ref--0-0!/Users/Cini/Github/Webpack/webpack-loaders/html-loader/test/fixtures/root.html Unexpected token (1:26)\\\\nYou may need an appropriate loader to handle this file type.\\\\n| export default \\\\\\"<img src=\\\\\\"require('\\\\\\"./\\\\\\\\\\\\\\"./image.png\\\\\\\\\\\\\\"\\\\\\"')\\\\\\">\\\\\\\\n\\\\\\";\\");"`;
3+
exports[`Loader Defaults 1`] = `
4+
"// HTML URLs
5+
import HTML__URL__0 from './file.png';
6+
7+
// HTML Imports
8+
import HTML__IMPORT__0 from './file.html';
9+
10+
// HTML
11+
export default \`<!DOCTYPE html>
12+
<html lang=\\"en\\">
13+
<head>
14+
<meta charset=\\"utf-8\\">
15+
<title>HTML Loader</title>
16+
</head>
17+
<body>
18+
<!-- HTML Content -->
19+
<div id=\\"app\\"></div>
20+
<!-- HTML Import -->
21+
\${HTML__IMPORT__0}
22+
<!-- HTML URL -->
23+
<img src=\\"\${HTML__URL__0}\\">
24+
</body>
25+
</html>
26+
\`"
27+
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`E2E HTML 1`] = `
4+
"<div id=\\"app\\">
5+
<!-- HTML Content -->
6+
Hello World!
7+
<!-- HTML Import -->
8+
<div class=\\"import\\"></div>
9+
10+
<!-- HTML URL -->
11+
<img src=\\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\">
12+
</div>
13+
"
14+
`;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`E2E Templates 1`] = `
4+
"<div id=\\"app\\">
5+
<!-- HTML Template -->
6+
Hello World!
7+
<!-- HTML Import -->
8+
<div class=\\"import\\"></div>
9+
10+
<!-- HTML URL -->
11+
<img src=\\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\">
12+
</div>
13+
"
14+
`;

test/e2e/html.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* eslint-disable */
2+
import path from 'path';
3+
import dom from '../helpers/dom';
4+
import webpack from '../helpers/compiler';
5+
6+
describe('E2E', () => {
7+
test('HTML', async () => {
8+
const config = {
9+
rules: [
10+
{
11+
test: /\.html$/,
12+
use: [
13+
{
14+
loader: path.resolve('./src'),
15+
options: {}
16+
}
17+
]
18+
},
19+
{
20+
test: /\.png$/,
21+
use: [ 'file-loader' ]
22+
}
23+
]
24+
};
25+
26+
const stats = await webpack('e2e/html.js', config);
27+
const { assets } = stats.compilation;
28+
29+
const scripts = {
30+
main: assets['main.js'].source(),
31+
runtime: assets['runtime.js'].source()
32+
};
33+
34+
const { window } = dom([ scripts.runtime, scripts.main ]);
35+
36+
expect(window.document.body.innerHTML).toMatchSnapshot();
37+
})
38+
})

test/e2e/template.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* eslint-disable */
2+
import path from 'path';
3+
import dom from '../helpers/dom';
4+
import webpack from '../helpers/compiler';
5+
6+
describe('E2E', () => {
7+
test('Templates', async () => {
8+
const config = {
9+
rules: [
10+
{
11+
test: /\.html$/,
12+
use: [
13+
{
14+
loader: path.resolve('./src'),
15+
options: { template: true }
16+
}
17+
]
18+
},
19+
{
20+
test: /\.png$/,
21+
use: [ 'file-loader' ]
22+
}
23+
]
24+
};
25+
26+
const stats = await webpack('e2e/template.js', config);
27+
const { assets } = stats.compilation;
28+
29+
const scripts = {
30+
main: assets['main.js'].source(),
31+
runtime: assets['runtime.js'].source()
32+
};
33+
34+
const { window } = dom([ scripts.runtime, scripts.main ]);
35+
36+
expect(window.document.body.innerHTML).toMatchSnapshot();
37+
})
38+
})

test/fixtures/e2e/file.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="import"></div>

test/fixtures/e2e/file.png

6.62 KB
Loading

test/fixtures/e2e/html.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div id="app">
2+
<!-- HTML Content -->
3+
Hello World!
4+
<!-- HTML Import -->
5+
<import src="./file.html"></import>
6+
<!-- HTML URL -->
7+
<img src="./file.png">
8+
</div>

0 commit comments

Comments
 (0)