Skip to content

Commit d72854a

Browse files
author
niminjie
committed
Add tests
1 parent e361cf7 commit d72854a

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# HTML STRING REPLACE WEBPACK PLUGIN
22

3+
[![npm version](https://badge.fury.io/js/html-string-replace-webpack-plugin.svg)](https://badge.fury.io/js/html-string-replace-webpack-plugin)
4+
35
This is a webpack plugin that replace string in html files that created by `html-webpack-plugin`.
46
This is especially useful for adding some cdn prefix.
57

@@ -69,6 +71,7 @@ new HtmlStringReplace({
6971
// <script src="//cdn.baidu.com/static/build.js">
7072
match: /src=\"([^\"]*)\"/g,
7173
replacement: 'href="' + CDN_PREFIX + '$1"'
74+
}
7275
]
7376
})
7477
```

Diff for: package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "webpack html string replacement plugin",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"test": "jest"
88
},
99
"repository": {
1010
"type": "git",
@@ -21,5 +21,11 @@
2121
"bugs": {
2222
"url": "https://github.com/erraX/html-string-replace-webpack-plugin/issues"
2323
},
24-
"homepage": "https://github.com/erraX/html-string-replace-webpack-plugin#readme"
24+
"homepage": "https://github.com/erraX/html-string-replace-webpack-plugin#readme",
25+
"dependencies": {
26+
"jest": "^20.0.4"
27+
},
28+
"jest": {
29+
"testRegex": "(/test/.*\\.spec.js)$"
30+
}
2531
}

Diff for: test/replacestring.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var HtmlStringReplace = require('../lib/HtmlStringReplace');
2+
3+
describe('test string replacement', function () {
4+
it('should replace string', function () {
5+
var plugin = new HtmlStringReplace({
6+
enable: true,
7+
patterns: [
8+
{
9+
// eg.
10+
// <link href="build.css"> =>
11+
// <link href="//cdn.baidu.com/static/build.css">
12+
match: /href=\"([^\"]*)\"/g,
13+
replacement: function (match, $1) {
14+
return 'href="//cdn.baidu.com/static/' + $1 + '"';
15+
}
16+
},
17+
{
18+
// eg.
19+
// <script src="build.js"> =>
20+
// <script src="//cdn.baidu.com/static/build.js">
21+
match: /src=\"([^\"]*)\"/g,
22+
replacement: 'src="//cdn.baidu.com/static/$1"'
23+
}
24+
]
25+
});
26+
27+
expect(plugin.replaceString('<link href="build.css">')).toBe('<link href="//cdn.baidu.com/static/build.css">');
28+
expect(plugin.replaceString('<script src="build.js">')).toBe('<script src="//cdn.baidu.com/static/build.js">');
29+
});
30+
});

0 commit comments

Comments
 (0)