Skip to content

Commit

Permalink
Allow tests to assert offenders too
Browse files Browse the repository at this point in the history
  • Loading branch information
macbre committed Jun 11, 2021
1 parent 6dbecec commit 3688aca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 15 additions & 4 deletions test/rules.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/*global describe, it */
'use strict';

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 +23,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 +42,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
5 changes: 5 additions & 0 deletions test/rules/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ exports.tests = [
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)"
]
}
}
];

0 comments on commit 3688aca

Please sign in to comment.