gaffe
v1.1.0
Published
We, as humans, sometimes accidentally make mistakes in test cases. Such as leave '.only', 'iit', 'ddescribe'
Downloads
9
Maintainers
Readme
Gaffe
We, as humans, sometimes accidentally make mistakes in test cases. Such as leave '.only', 'iit', 'ddescribe'.
Story so far
Most of the time, Developers need to run a single test case or a single block of test cases. To achieve this requirement, test frameworks provide different functionalities.
Eg:
So, what is the issue?
Sometimes, developers accidently commit their test cases with these keywords. As the result, when running tests through a CI interface or any other testing environments, all the test cases won't get executed.
And.. we have a big issue :).
So, Can we introduce some kind of Linting ?
I dig around and try to add a new rule to ESLint since, its my favourite. But, the contribution guide states that, Rules must be based solely on JavaScript runtime environments and not on specific libraries or frameworks.
So, I thought of developing this module.
How to define rules
A set of default set of rules are defined in the gaffe.json as follows.
[
{
"suite": "mocha",
"rules": [
"no_it_only",
"no_describe_only"
]
},
{
"suite": "jasmine",
"rules": [
"no_ddescribe",
"no_fdescribe",
"no_fit",
"no_iit"
]
},
{
"suite": "custom",
"rules" : [
"^[\\s]*lab\\.test\\.only[\\(\\s]"
]
}
]
To override these rules, you have to add your own gaffe.json to your application folder. Then add rules.
Eg:
- If you only use Mocha then,
[
{
"suite": "mocha",
"rules": [
"no_it_only",
"no_describe_only"
]
}
]
- If you use only Jasmine then,
[
{
"suite": "jasmine",
"rules": [
"no_ddescribe",
"no_fdescribe",
"no_fit",
"no_iit"
]
}
]
- if you want to add only the custom rules,
[
{
"suite": "custom",
"rules" : [
"^[\\s]*lab\\.test\\.only[\\(\\s]"
]
}
]
How to use a rule file in a different location
Its easy. create a rule file by following above samples and save it as a json file. Then follow the below mentioned command.
./gaffe -s <path to your rule file> <your test cases>
eg:
./gaffe -s ../my_app/gaffe.json `find test -name \'*.js\'`
What is a 'Custom' rule
This feature enables you to add your own set of experience to run against the files which contains the test cases.
Add the custom rule as follows.
/*
1. Only one 'custom' object is allowed in the config.
2. All the reqular expressions are must be escaped.
eg: ^[\s]*lab\.test\.only[\(\s] has to used as "^[\\s]*lab\\.test\\.only[\\(\\s]"
*/
[
{
"suite": "custom",
"rules" : [
"Your regular expression 1"
"Your regular expression 2"
]
}
]
How to use
After defining the set of rules, use one of following commands to execute the module.
./node_modules/.bin/gaffe <list of .js or .coffee files with test cases>
Eg:
./node_modules/.bin/gaffe `find test -name '*.js'`
Adding to your linting script in package.json
This is how I usually do it.
{
"name": "myapp",
"version": "1.0.0",
"description": "my test app",
"scripts": {
"start": "node main",
"lint-js": "eslint -c eslint.json .",
"lint-test": "./node_modules/.bin/gaffe `find test -name '*.js'`",
"lint": "npm run lint-js && npm run lint-test"
},
"author": "Ruwan Geeganage",
"license": "MIT",
}
and run below command,
npm run lint
Contribute
Fork this repository and add your test cases under rules/ folder and add test cases. Follow the existing rule implementation.