I'm creating new rule to avoid using 'contain' in 'should' assertion. I've used 'yo eslint:plugin' and then 'yo eslint:rule' to create structure. Now trying to write a rule.
Unfortunately after npm test I'm getting AssertionError [ERR_ASSERTION]: Should have 1 error but had 0: [].
lib/rules/no-contain-assertion.js
module.exports = {
meta: {
docs: {
description:
"Rule to flag use of should.('contain') in tests, preventing tests with non strict assertion being committed accidentally",
category: "StrictAssertion",
recommended: false
},
fixable: null, // or "code" or "whitespace"
schema: [
// fill in your schema
]
},
create: function(context) {
return {
StrictAssertion(node) {
if (node.object.name === 'should' && node.arguments[0] === 'contain') {
context.report({
node,
message: `Don't use ${node}(${node.arguments[0]})`,
});
}
}
};
}
};
tests/lib/rules/no-contain-assertion.js
var ruleTester = new RuleTester();
ruleTester.run("no-contain-assertion", rule, {
valid: [
"should('have.text')"
],
invalid: [
{
code: "should('contain')",
errors: [{
message: "Don't use 'contain' in assertion!",
type: "StrictAssertion"
}]
}
]
});