Skip to content

Commit

Permalink
Merge pull request #27 from zoobestik/multi-tests
Browse files Browse the repository at this point in the history
Run tests for diffirent webpack configs
  • Loading branch information
zoobestik authored Apr 1, 2021
2 parents a437094 + ff576d8 commit 9079d92
Show file tree
Hide file tree
Showing 80 changed files with 226 additions and 43 deletions.
5 changes: 3 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"presets": [
[ "@babel/preset-env", {
"corejs": 3,
"targets": {
"node": "8"
"node": "8.0.0"
},
"loose": true,
"useBuiltIns": "entry"
"useBuiltIns": "usage"
} ]
]
}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ yarn.lock

.eslintcache
lib/**.js
test/_out
test/**/_out
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ node_js:
- "13"
- "node"

before_script: npm run test-prepare
script: npm test

branches:
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
},
"scripts": {
"lint": "eslint --cache .",
"test": "npm run build && mocha",
"test": "npm run build && mocha \"./test/*/*.spec.js\"",
"test-prepare": "cd test/webpack2 && npm install && cd ../webpack4 && npm install && cd ../webpack5 && npm install",
"build": "babel src --out-dir lib",
"prepublish": "npm run build"
},
Expand All @@ -55,14 +56,12 @@
"@babel/core": "^7.7.5",
"@babel/preset-env": "^7.7.6",
"babel-eslint": "^10.0.0",
"css-loader": "^0.28.5",
"core-js": "^3.10.0",
"eslint": "^6.7.2",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.7.0",
"extract-text-webpack-plugin": "^2.1.0",
"mocha": "^6.2.2",
"pre-commit": "^1.2.2",
"rimraf": "^3.0.0",
"webpack": "^2.3.3"
"rimraf": "^3.0.0"
}
}
2 changes: 0 additions & 2 deletions test/integrations/source-map-with-import/a.css

This file was deleted.

36 changes: 28 additions & 8 deletions test/integrations.js → test/lib/integrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ const fs = require('fs');
const join = require('path').join;
const rimraf = require('rimraf');
const assert = require('assert');
const webpack = require('webpack');
const getWebpackConfig = require('./webpack.config.js');

const root = join(__dirname, 'integrations');
const output = join(__dirname, '_out');

describe('Integrations with webpack 2', function() {
function runIntegrations(basePath, version) {
this.timeout(5000);

const cases = fs.readdirSync(root);
const output = join(basePath, '_out');

rimraf.sync(output);

const nodeVersions = process.version.split('.');
const nodeMajorVersion = parseInt(nodeVersions[0].substring(1), 10);
const nodeMinorVersion = parseInt(nodeVersions[1], 10);

const isEnvCorrect = !(
(version === '4' && nodeMajorVersion === 8 && nodeMinorVersion === 0) ||
(version === '5' && nodeMajorVersion < 10)
);

if (!isEnvCorrect) {
it('webpack ' + version + ' unsuppported with node ' + process.version, function() {
assert.ok(true);
});
return;
}

const cases = fs.readdirSync(root);
const webpack = require(join(basePath, 'node_modules', 'webpack'));
const getWebpackConfig = require(join(basePath, 'webpack.config.js'));

cases.forEach(function(testCase) {
it('with ' + testCase + ' test', function() {
return new Promise(function(resolve, reject) {
Expand All @@ -23,6 +40,7 @@ describe('Integrations with webpack 2', function() {
const configFile = join(testDirectory, 'webpack.config.js');

var options = getWebpackConfig({
basePath: basePath,
outputDirectory: outputDirectory,
testDirectory: testDirectory
});
Expand All @@ -39,7 +57,7 @@ describe('Integrations with webpack 2', function() {
if (err) return reject(err);
if (stats.hasErrors()) return reject(new Error(stats.toString()));

const expectedCssExt = 'expected.css';
const expectedCssExt = 'expected-v' + version + '.css';

fs.readdir(testDirectory, function(err, files) {
if (err) return reject(err);
Expand All @@ -59,7 +77,7 @@ describe('Integrations with webpack 2', function() {
.replace(/\n$/g, '')
.replace(/%%unit-hash%%/g, stats.hash);

assert.equal(actual, expected,
assert.strictEqual(actual, expected,
'Output ' + testCase + ' — ' + name + ' file isn\'t equals ' + actualName
);
});
Expand All @@ -70,4 +88,6 @@ describe('Integrations with webpack 2', function() {
});
});
});
});
}

module.exports = runIntegrations;
File renamed without changes.
1 change: 1 addition & 0 deletions test/lib/integrations/css-modules/expected-v5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.c .d,.local-a.c .d,.local-c .local-d{color:#fff}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
plugins: [
Expand Down
4 changes: 4 additions & 0 deletions test/lib/integrations/plugin-output-postfix/expected-v4.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.local-a.c .d { color: #fff; }
.c .d { color: white; }
.local-c .local-d { color: #ffffff; }

4 changes: 4 additions & 0 deletions test/lib/integrations/plugin-output-postfix/expected-v5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.local-a.c .d { color: #fff; }
.c .d { color: white; }
.local-c .local-d { color: #ffffff; }

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.c .d,.local-a.c .d,.local-c .local-d{color:#fff}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
plugins: [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/lib/integrations/simple/expected-v5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a,.local-b,.local-c{color:#fff}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
plugins: [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions test/lib/integrations/source-map-false/expected-v2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a,.local-b,.local-c{color:#fff}
1 change: 1 addition & 0 deletions test/lib/integrations/source-map-false/expected-v4.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a,.local-b,.local-c{color:#fff}
1 change: 1 addition & 0 deletions test/lib/integrations/source-map-false/expected-v5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a,.local-b,.local-c{color:#fff}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: false,
Expand Down
2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-filename/expected-v4.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-filename/expected-v5.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: 'source-map',
Expand Down
1 change: 1 addition & 0 deletions test/lib/integrations/source-map-hidden/expected-v4.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a.c .d{color:#fff}
1 change: 1 addition & 0 deletions test/lib/integrations/source-map-hidden/expected-v5.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.local-a.c .d{color:#fff}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: 'hidden-source-map',
Expand Down
2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-inline/expected-v4.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-inline/expected-v5.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: 'inline-source-map',
Expand Down
2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-with-import/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import url('import.css');
.a { color: white; }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-with-import/expected-v4.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/lib/integrations/source-map-with-import/expected-v5.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: 'inline-source-map',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions test/lib/integrations/source-map/expected-v4.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/lib/integrations/source-map/expected-v5.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CssoWebpackPlugin = require('../../../lib').default;
const CssoWebpackPlugin = require('../../../../lib/index').default;

module.exports = {
devtool: 'source-map',
Expand Down
24 changes: 12 additions & 12 deletions test/filter.js → test/unit/filter.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const assert = require('assert');
const CssoWebpackPlugin = require('../lib').default;
const CssoWebpackPlugin = require('../../lib/index').default;

describe('Constructor', function() {
function testDefaultOptions(plugin) {
assert.deepEqual(plugin.options, {}, 'default options inited');
assert.deepStrictEqual(plugin.options, {}, 'default options inited');
}

function testDefaultFilter(plugin) {
assert.equal(plugin.filter('files.css'), true, 'files.css should be true');
assert.equal(plugin.filter('files.js'), false, 'files.js should be false');
assert.equal(plugin.filter('/.css/file'), false, '/.css/file should be false');
assert.strictEqual(plugin.filter('files.css'), true, 'files.css should be true');
assert.strictEqual(plugin.filter('files.js'), false, 'files.js should be false');
assert.strictEqual(plugin.filter('/.css/file'), false, '/.css/file should be false');
}

it('default', function() {
Expand All @@ -21,18 +21,18 @@ describe('Constructor', function() {
it('with options', function() {
const options = { test: 1 };
const plugin = new CssoWebpackPlugin(options);
assert.deepEqual(plugin.options, { test: 1 }, 'single argument with options');
assert.deepEqual(options, { test: 1 }, 'single argument with options immutable');
assert.deepStrictEqual(plugin.options, { test: 1 }, 'single argument with options');
assert.deepStrictEqual(options, { test: 1 }, 'single argument with options immutable');
testDefaultFilter(plugin);
});

it('with regexp filter', function() {
const plugin = new CssoWebpackPlugin(/^text/gi);

testDefaultOptions(plugin);
assert.equal(plugin.filter('text-file.pcss'), true, 'text-file.pcss should be true');
assert.equal(plugin.filter('files.js'), false, 'files.js should be false');
assert.equal(plugin.filter('/.css/text'), false, '/.css/text should be false');
assert.strictEqual(plugin.filter('text-file.pcss'), true, 'text-file.pcss should be true');
assert.strictEqual(plugin.filter('files.js'), false, 'files.js should be false');
assert.strictEqual(plugin.filter('/.css/text'), false, '/.css/text should be false');

assert.doesNotThrow(
function() { new CssoWebpackPlugin({}, /^text/gi); },
Expand All @@ -45,10 +45,10 @@ describe('Constructor', function() {

const oneArgPlugin = new CssoWebpackPlugin(filter);
testDefaultOptions(oneArgPlugin);
assert.equal(oneArgPlugin.filter, filter, 'single `filter` should be set to this.filter');
assert.strictEqual(oneArgPlugin.filter, filter, 'single `filter` should be set to this.filter');

const twoArgPlugin = new CssoWebpackPlugin({}, filter);
assert.equal(twoArgPlugin.filter, filter, 'second argument `filter` should be set to this.filter');
assert.strictEqual(twoArgPlugin.filter, filter, 'second argument `filter` should be set to this.filter');
});

it('unexpected type for filter', function() {
Expand Down
5 changes: 5 additions & 0 deletions test/webpack2/integrations.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const runIntegrations = require('../lib/integrations.js');

describe('Integrations with webpack 2', function() {
runIntegrations.call(this, __dirname, '2');
});
7 changes: 7 additions & 0 deletions test/webpack2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^2.1.0",
"webpack": "^2.7.0"
}
}
6 changes: 3 additions & 3 deletions test/webpack.config.js → test/webpack2/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const DefinePlugin = require('webpack').DefinePlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const DefinePlugin = require('./node_modules/webpack').DefinePlugin;
const ExtractTextPlugin = require('./node_modules/extract-text-webpack-plugin');

const styleExtractPlugin = new ExtractTextPlugin({
filename: '[name].css',
Expand All @@ -24,7 +24,7 @@ module.exports = function (options) {
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: {
loader: 'css-loader',
loader: options.basePath + '/node_modules/css-loader',
options: {
modules: true,
localIdentName: 'local-[local]',
Expand Down
5 changes: 5 additions & 0 deletions test/webpack4/integrations.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const runIntegrations = require('../lib/integrations.js');

describe('Integrations with webpack 4', function() {
runIntegrations.call(this, __dirname, '4');
});
7 changes: 7 additions & 0 deletions test/webpack4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"dependencies": {
"css-loader": "^5.2.0",
"mini-css-extract-plugin": "^1.4.0",
"webpack": "^4.46.0"
}
}
47 changes: 47 additions & 0 deletions test/webpack4/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const DefinePlugin = require('webpack').DefinePlugin;
const ExtractTextPlugin = require('mini-css-extract-plugin');

const styleExtractPlugin = new ExtractTextPlugin({
filename: '[name].css',
});

module.exports = function (options) {
return {
mode: 'none',
entry: {
test: './index.js'
},
cache: false,
performance: false,
context: options.testDirectory,
output: {
filename: '[name].js',
path: options.outputDirectory,
},
module: {
rules: [
{
test: /\.css$/,
use: [
ExtractTextPlugin.loader,
{
loader: options.basePath + '/node_modules/css-loader',
options: {
modules: {
localIdentName: 'local-[local]',
},
sourceMap: true
}
},
],
},
],
},
plugins: [
new DefinePlugin({
'process.env.NODE_ENV': '"production"',
}),
styleExtractPlugin,
],
};
};
Loading

0 comments on commit 9079d92

Please sign in to comment.