Skip to content

Commit 8ad6e6f

Browse files
dwschrashunScottLNorvell
authored andcommitted
Add tests (NMFR#39)
* add Jest, unit tests, webpack integration tests, test helpers * update README Co-authored-by: Scott Norvell <scottlnorvell@gmail.com> Co-authored-by: Doug Schrashun <thejazzface@gmail.com>
1 parent 929ddf0 commit 8ad6e6f

33 files changed

+449
-3
lines changed

.babelrc

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"presets": [
3+
[
4+
"env",
5+
{
6+
"useBuiltIns": true,
7+
"targets": {
8+
"node": "current"
9+
},
10+
"exclude": [
11+
"transform-async-to-generator",
12+
"transform-regenerator"
13+
]
14+
}
15+
]
16+
],
17+
"plugins": [
18+
[
19+
"transform-object-rest-spread",
20+
{
21+
"useBuiltIns": true
22+
}
23+
]
24+
]
25+
}

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
node_modules
22
jsconfig.json
3+
.DS_Store
4+
npm-debug.log
5+
/test/js
6+
7+
logs
8+
*.log
9+
npm-debug.log*
10+
.eslintcache
11+
/dist
12+
/local
13+
/reports
14+
Thumbs.db
15+
.idea
16+
.vscode
17+
*.sublime-project
18+
*.sublime-workspace

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $ npm install --save-dev optimize-css-assets-webpack-plugin
2222
## Configuration:
2323

2424
The plugin can receive the following options (all of them are optional):
25-
* assetNameRegExp: A regular expression that indicates the names of the assets that should be optimized \ minimized, defaults to `/\.css$/g`
25+
* assetNameRegExp: A regular expression that indicates the names of the assets that should be optimized \ minimized. The regular expression provided is run against the filenames of the files exported by the ExtractTextPlugin instances in your configuration, not the filenames of your source CSS files. Defaults to `/\.css$/g`
2626
* cssProcessor: The CSS processor used to optimize \ minimize the CSS, defaults to [cssnano](http://github.com/ben-eb/cssnano). This should be a function that follows cssnano.process interface (receives a CSS and options parameters and returns a Promise).
2727
* cssProcessorOptions: The options passed to the cssProcessor, defaults to `{}`
2828
* canPrint: A boolean indicating if the plugin can print messages to the console, defaults to `true`
@@ -33,7 +33,7 @@ The plugin can receive the following options (all of them are optional):
3333
var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
3434
module.exports = {
3535
module: {
36-
loaders: [
36+
rules: [
3737
{
3838
test: /\.css$/,
3939
loader: ExtractTextPlugin.extract('style-loader', 'css-loader')

package.json

+23-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
"last-call-webpack-plugin": "^3.0.0"
99
},
1010
"main": "src/index.js",
11+
"scripts": {
12+
"test": "jest",
13+
"test:watch": "jest --watch"
14+
},
15+
"jest": {
16+
"watchPathIgnorePatterns": [
17+
"<rootDir>/test/js/*.*"
18+
],
19+
"testEnvironment": "node"
20+
},
1121
"homepage": "http://github.com/NMFR/optimize-css-assets-webpack-plugin",
1222
"repository": {
1323
"type": "git",
@@ -22,5 +32,17 @@
2232
"duplicate",
2333
"extract-text-webpack-plugin"
2434
],
25-
"license": "MIT"
35+
"license": "MIT",
36+
"devDependencies": {
37+
"babel-core": "^6.26.0",
38+
"babel-jest": "^22.1.0",
39+
"babel-plugin-transform-object-rest-spread": "^6.26.0",
40+
"babel-polyfill": "^6.26.0",
41+
"babel-preset-env": "^1.6.1",
42+
"css-loader": "^0.28.9",
43+
"extract-text-webpack-plugin": "next",
44+
"jest": "^22.1.4",
45+
"style-loader": "^0.20.1",
46+
"webpack": "^4.0.0"
47+
}
2648
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Webpack Integration Tests assetNameRegExp-no-source 1`] = `"body{color:red}a{color:blue}body{margin:0;color:red}p{margin:1000px}body{color:red;padding:0;margin:0}p{padding:500px;padding:1000px}"`;
4+
5+
exports[`Webpack Integration Tests duplicate-css-exists-without-plugin 1`] = `"body{color:red}a{color:blue}body{color:red}p{color:green}"`;
6+
7+
exports[`Webpack Integration Tests only-assetNameRegExp-processed 1`] = `
8+
"body {
9+
color: red;
10+
padding: 0;
11+
margin: 0;
12+
}
13+
p {
14+
padding: 500px;
15+
padding: 1000px;
16+
}
17+
"
18+
`;
19+
20+
exports[`Webpack Integration Tests only-assetNameRegExp-processed 2`] = `"a{color:blue}body{margin:0;color:red}p{margin:1000px}"`;
21+
22+
exports[`Webpack Integration Tests removes-duplicate-css 1`] = `"a{color:blue}body{color:red}p{color:green}"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
a {
5+
color: blue;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
margin: 0;
3+
color: red;
4+
}
5+
p {
6+
margin: 1000px;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
color: red;
3+
padding: 0;
4+
margin: 0;
5+
}
6+
p {
7+
padding: 500px;
8+
padding: 1000px;
9+
}

test/cases/assetNameRegExp-no-source/expected/file.css

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
3+
This test is here to confirm that assetNameRegExp option will apply
4+
only to the names of the files exported byt ExtractTextPlugin
5+
6+
*/
7+
8+
require('./a_optimize-me.css');
9+
require('./b_optimize-me.css');
10+
require('./c.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import ExtractTextPlugin from 'extract-text-webpack-plugin';
2+
import OptimizeCssAssetsPlugin from '../../../src/';
3+
4+
module.exports = {
5+
entry: './index',
6+
module: {
7+
rules: [
8+
{
9+
test: /\.css$/,
10+
use: ExtractTextPlugin.extract({
11+
fallback: { loader: 'style-loader' },
12+
use: {
13+
loader: 'css-loader',
14+
options: { minimize: true }
15+
}
16+
})
17+
},
18+
],
19+
},
20+
plugins: [
21+
new ExtractTextPlugin('file.css'),
22+
new OptimizeCssAssetsPlugin({
23+
assetNameRegExp: /optimize-me\.css/g
24+
})
25+
],
26+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
a {
5+
color: blue;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
p {
5+
color: green;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body{color:red}a{color:blue}body{color:red}p{color:green}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./a.css');
2+
require('./b.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import ExtractTextPlugin from 'extract-text-webpack-plugin';
2+
import OptimizeCssAssetsPlugin from '../../../src/';
3+
4+
module.exports = {
5+
entry: './index',
6+
module: {
7+
rules: [
8+
{
9+
test: /\.css$/,
10+
use: ExtractTextPlugin.extract({
11+
fallback: { loader: 'style-loader' },
12+
use: {
13+
loader: 'css-loader',
14+
options: { minimize: true }
15+
}
16+
})
17+
},
18+
],
19+
},
20+
plugins: [
21+
new ExtractTextPlugin('file.css')
22+
],
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
a {
5+
color: blue;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
margin: 0;
3+
color: red;
4+
}
5+
p {
6+
margin: 1000px;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
color: red;
3+
padding: 0;
4+
margin: 0;
5+
}
6+
p {
7+
padding: 500px;
8+
padding: 1000px;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
color: red;
3+
padding: 0;
4+
margin: 0;
5+
}
6+
p {
7+
padding: 500px;
8+
padding: 1000px;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a{color:blue}body{margin:0;color:red}p{margin:1000px}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require('./a_optimize-me.css');
2+
require('./b_optimize-me.css');
3+
require('./c_as-is.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import ExtractTextPlugin from 'extract-text-webpack-plugin';
2+
import OptimizeCssAssetsPlugin from '../../../src/';
3+
4+
const notToProcess = new ExtractTextPlugin('as_is.css');
5+
const toProcess = new ExtractTextPlugin('optimize.css');
6+
7+
module.exports = {
8+
entry: './index',
9+
module: {
10+
rules: [
11+
{
12+
test: /as-is\.css$/,
13+
use: notToProcess.extract({
14+
fallback: { loader: 'style-loader' },
15+
use: {
16+
loader: 'css-loader',
17+
options: { minimize: false }
18+
}
19+
})
20+
},
21+
{
22+
test: /optimize-me\.css$/,
23+
use: toProcess.extract({
24+
fallback: { loader: 'style-loader' },
25+
use: {
26+
loader: 'css-loader',
27+
options: { minimize: false }
28+
}
29+
})
30+
}
31+
],
32+
},
33+
plugins: [
34+
notToProcess,
35+
toProcess,
36+
new OptimizeCssAssetsPlugin({
37+
assetNameRegExp: /optimize\.css/g
38+
})
39+
],
40+
};
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
a {
5+
color: blue;
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
body {
2+
color: red;
3+
}
4+
p {
5+
color: green;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a{color:blue}body{color:red}p{color:green}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('./a.css');
2+
require('./b.css');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ExtractTextPlugin from 'extract-text-webpack-plugin';
2+
import OptimizeCssAssetsPlugin from '../../../src/';
3+
4+
module.exports = {
5+
entry: './index',
6+
module: {
7+
rules: [
8+
{
9+
test: /\.css$/,
10+
use: ExtractTextPlugin.extract({
11+
fallback: { loader: 'style-loader' },
12+
use: {
13+
loader: 'css-loader',
14+
options: { minimize: true }
15+
}
16+
})
17+
},
18+
],
19+
},
20+
plugins: [
21+
new ExtractTextPlugin('file.css'),
22+
new OptimizeCssAssetsPlugin()
23+
],
24+
};

test/plugin.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import OptimizeCssAssetsPlugin from '../src/';
2+
3+
describe('plugin test', () => {
4+
it('does not throw when called', () => {
5+
expect(() => {
6+
new OptimizeCssAssetsPlugin();
7+
}).not.toThrow();
8+
});
9+
10+
it('can override default parameters', () => {
11+
const assetNameRegExp = /\.optimize\.css$/
12+
const cssProcessor = {};
13+
const cssProcessorOptions = { discardComments: { removeAll: true } };
14+
const canPrint = false;
15+
const plugin = new OptimizeCssAssetsPlugin({
16+
assetNameRegExp,
17+
cssProcessor,
18+
cssProcessorOptions,
19+
canPrint
20+
});
21+
expect(plugin.options.assetNameRegExp).toEqual(assetNameRegExp);
22+
expect(plugin.options.cssProcessor).toEqual(cssProcessor);
23+
expect(plugin.options.cssProcessorOptions).toEqual(cssProcessorOptions);
24+
expect(plugin.options.canPrint).toEqual(canPrint);
25+
});
26+
});

test/util/default.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
html{display:none}

0 commit comments

Comments
 (0)