We have already encountered the func-names and prefer-arrow-callback rules before when we wrote our E2E tests with cucumber-js. Back then, we needed to keep using function expressions instead of arrow functions because cucumber-js uses this inside each function to maintain context between different steps of the same scenario. If we'd used arrow functions, this would be bound, in our case, to the global context, and we'd have to go back to using file-scope variables to maintain state between steps.
As it turns out, Mocha also uses this to maintain a "context". However, in Mocha's vocabulary, a "context" is not used to persist state between steps; rather, a Mocha context provides the following methods, which you can use to control the flow of your tests:
- this.timeout(): To specify how long, in milliseconds, to wait for a test to complete before marking it as failed
- this.slow(): To specify how long, in milliseconds, a test should run for before it is considered "slow"
- this.skip() : To skip/abort a test
- this.retries(): To retry a test a specified number of times
It is also impractical to give names to every test function; therefore, we should disable both the func-names and prefer-arrow-callback rules.
So, how do we disable these rules for our test files? For our E2E tests, we created a new .eslintrc.json and placed it inside the spec/ directory. This would apply those configurations to all files under the spec/ directory. However, our test files are not separated into their own directory, but interspersed between all our application code. Therefore, creating a new .eslintrc.json won't work.
Instead, we can add an overrides property to our top-level .eslintrc.json, which allows us to override rules for files that match the specified file glob(s). Update .eslintrc.json to the following:
{
"extends": "airbnb-base",
"rules": {
"no-underscore-dangle": "off"
},
"overrides": [
{
"files": ["*.test.js"],
"rules": {
"func-names": "off",
"prefer-arrow-callback": "off"
}
}
]
}
Here, we are indicating that files with the extension .test.js should have the func-names and prefer-arrow-callback rules turned off.