Skip to content

Commit

Permalink
Merge pull request #317 from macbre/improve-code-coverage
Browse files Browse the repository at this point in the history
Improve code coverage
  • Loading branch information
macbre committed Jun 11, 2021
2 parents 7e04067 + 09ef47b commit 52a5143
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 64 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ rules:
- error
- allowEmptyCatch: true
no-prototype-builtins: 'off'
"node/no-extraneous-require": 'off'
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ steps.npm-cache.outputs.dir }}
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
key: ${{ runner.os }}-nodev-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
${{ runner.os }}-nodev-${{ matrix.node-version }}-
- name: Install dependencies
run: npm ci

- name: Run tests (with node-sass support)
run: |
time npm install node-sass@5.0.0
time npm install node-sass@6.0.0
npm test
# https://github.com/marketplace/actions/coveralls-github-action
Expand Down
2 changes: 1 addition & 1 deletion lib/preprocessors/sass.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
// check the presense of the optional "node-sass" module (#118)
try {
sass = require("node-sass");
} catch (e) {
} catch (e) /* istanbul ignore next */ {
throw new Error(
"Can't process SASS/SCSS, please run 'npm install node-sass'"
);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
"verbose": true,
"coverageThreshold": {
"global": {
"statements": 97,
"branches": 94,
"statements": 99,
"branches": 95,
"functions": 100,
"lines": 97
"lines": 99
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions test/colors.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*global describe, it */
'use strict';
const { describe, it } = require("@jest/globals");

var extractColors = require('../rules/colors').extractColors,
assert = require('assert');
Expand Down Expand Up @@ -39,7 +38,7 @@ describe('Colors', () => {

testCases.forEach(function(testCase) {
var colors = extractColors(testCase[0]);
assert.deepEqual(colors, testCase[1]);
assert.deepStrictEqual(colors, testCase[1]);
});
});
});
3 changes: 1 addition & 2 deletions test/errors.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*global describe, it */
'use strict';
const { describe, it } = require("@jest/globals");

var analyzer = require('../'),
assert = require('assert'),
Expand Down
3 changes: 1 addition & 2 deletions test/opts.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*global describe, it */
'use strict';
const { describe, it } = require("@jest/globals");

var analyzer = require('../'),
assert = require('assert'),
Expand Down
22 changes: 16 additions & 6 deletions test/rules.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*global describe, it */
'use strict';
const { describe, it } = require("@jest/globals");

var analyzer = require('../'),
const analyzer = require('../'),
assert = require('assert'),
glob = require('glob');

function runTest(tests) {
tests.forEach(function(test, testId) {
new analyzer(test.css, function(err, res) {
var metricsExpected = test.metrics,
metricsActual = res && res.metrics;
const metricsExpected = test.metrics || {},
offendersExpected = test.offenders || {},
metricsActual = res && res.metrics,
offendersActual = res && res.offenders;

if (err) {
throw err;
Expand All @@ -21,6 +22,15 @@ function runTest(tests) {
() => {
assert.strictEqual(metricsActual[metric], metricsExpected[metric], "Testing metric against: " + test.css);
}
);
});

Object.keys(offendersExpected).forEach(function(metric) {
it(
'should emit offender for "' + metric + '" metric with a valid value - #' + (testId + 1),
() => {
assert.deepStrictEqual(offendersActual[metric].map(item => item.message), offendersExpected[metric], "Testing offender against: " + test.css);
}
);
});
});
Expand All @@ -31,7 +41,7 @@ function runTest(tests) {
* Read all files in rules/ subdirectory and perform tests defined there
*/
describe('Rules', () => {
var files = glob.sync(__dirname + "/rules/*.js"),
const files = glob.sync(__dirname + "/rules/*.js"),
nameRe = /([^/]+)\.js$/;

files.forEach(function(file) {
Expand Down
11 changes: 11 additions & 0 deletions test/rules/base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ exports.tests = [
css: '.foo { background-image: url(data:image/gif;base64,R0lGODlhAQABAIABAAAAAP///yH5BAEAAAEALAAAAAABAAEAQAICTAEAOw%3D%3D) }', // blank 1x1 gif
metrics: {
base64Length: 64
},
},
{
css: '.foo { background-image: url(data:image/gif;base64,' + 'FFFFFF'.repeat(1024) + ') }', // to big base64-encoded asset
metrics: {
base64Length: 6144
},
offenders: {
base64Length: [
".foo { background-image: ... } // base64: 6.00 kB, raw: 4.50 kB"
]
}
}
];
13 changes: 13 additions & 0 deletions test/rules/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,18 @@ exports.tests = [
comments: 1,
commentsLength: 22
}
},
{
css: 'body { padding-bottom: 6em; min-width: 40em }' +
'/* really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really really long comment */',
metrics: {
comments: 1,
commentsLength: 273
},
offenders: {
comments: [
"\" really really really really really really really really really really really really really really r\" is too long (273 characters)"
]
}
}
];
90 changes: 46 additions & 44 deletions test/sass.test.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
/*global describe, it */
'use strict';
const { describe, it } = require("@jest/globals");

var analyzer = require('../'),
fs = require('fs'),
isSassInstalled = true,
assert = require('assert'),
scss = 'nav {\nul{ color: white }\n}',
sass = 'nav\n\tul\n\t\tcolor: white\n';
sass = 'nav\n\tul\n\t\tcolor: white\n',
nodeSassInfo;

try {
require('node-sass');
nodeSassInfo = require('node-sass').info;
console.log(`Using ${nodeSassInfo.replace("\n", " ")}`);
} catch (e) {
isSassInstalled = false;
}

/**
* TODO: install and test node-sass
*/
function testSassInstalled(done) {
new analyzer(sass, {
preprocessor: 'sass'
}, function(err, res) {
assert.strictEqual(err, null);
assert.equal(res.metrics.selectors, 1);
done();
});
}

/**
* node-sass is not installed by default (see #118)
*/
function testSassNotInstalled(done) {
try {
new analyzer(scss, {
preprocessor: 'sass'
}, function() {});
} catch (e) {
assert.ok(e instanceof Error);
assert.equal(e.message, 'Preprocessing failed: Error: Can\'t process SASS/SCSS, please run \'npm install node-sass\'');
done();
}
}

describe('SASS preprocessor [' + (isSassInstalled ? 'node-sass installed' : 'node-sass missing') + ']', () => {
it('should be chosen for SCSS files', () => {
var preprocessors = new(require('../lib/preprocessors.js'))();

assert.equal(preprocessors.findMatchingByFileName('test/foo.scss'), 'sass');
assert.equal(preprocessors.findMatchingByFileName('test/foo.sass'), 'sass');
assert.equal(preprocessors.findMatchingByFileName('test/foo.css'), false);
assert.strictEqual(preprocessors.findMatchingByFileName('test/foo.scss'), 'sass');
assert.strictEqual(preprocessors.findMatchingByFileName('test/foo.sass'), 'sass');
assert.strictEqual(preprocessors.findMatchingByFileName('test/foo.css'), false);
});

it('should report parsing error (if not selected)', done => {
new analyzer(scss, function(err, res) {
assert.strictEqual(err, null);
assert.equal(res.metrics.parsingErrors, 3);
assert.strictEqual(res.metrics.parsingErrors, 3);
done();
});
});

it(
'should generate CSS from SCSS correctly',
!isSassInstalled ? testSassNotInstalled : testSassInstalled
);
if (isSassInstalled === false) {
return;
}

it(
'should generate CSS from SASS correctly',
!isSassInstalled ? testSassNotInstalled : testSassInstalled
);
it('should generate CSS from SCSS correctly', done => {
try {
new analyzer(scss, {
preprocessor: 'sass'
}, done);
} catch (e) {
assert.ok(e instanceof Error);
assert.strictEqual(e.message, 'Preprocessing failed: Error: Can\'t process SASS/SCSS, please run \'npm install node-sass\'');
done();
}
});

it('should generate CSS from SASS correctly', done => {
try {
new analyzer(sass, {
preprocessor: 'sass'
}, done);
} catch (e) {
assert.ok(e instanceof Error);
assert.strictEqual(e.message, 'Preprocessing failed: Error: Can\'t process SASS/SCSS, please run \'npm install node-sass\'');
done();
}
});

it('should parse SCSS file correctly', done => {
const file = __dirname + '/../examples/base.scss',
source = fs.readFileSync(file).toString();

new analyzer(source, {
file: file,
preprocessor: 'sass'
}, done);
});
});

0 comments on commit 52a5143

Please sign in to comment.