ESLint rules & globals for TestCafe, from the TestCafe community
npm install eslint-plugin-testcafe-community
ESLint rules for TestCafe
from the TestCafe community.
---
This project provides the global variables fixture & test for your ESLint
configuration which are specific variables for _TestCafe_. The
recommended configuration enables common rules for
best practices when writing tests for TestCafe. The rule descriptions are
provided in the table below.
If you use other test suites (Jest/Mocha), make sure to read the
compatibility notes at the bottom.
You'll first need to install ESLint:
``sh`
npm i eslint --save-dev
Next, install eslint-plugin-testcafe-community:
`sh`
npm install eslint-plugin-testcafe-community --save-dev
Note: If you installed ESLint globally (using the -g flag) then you musteslint-plugin-testcafe-community
also install globally.
This plugin export a recommended configuration that enforce good practices.
To enable this configuration use the extends property in your .eslintrc config
file:
`json`
{
"plugins": ["testcafe-community"],
"extends": "plugin:testcafe-community/recommended"
}
You may review our
example TS/JS package
embedded in this repository for an example configuration and plugin testing.
See
ESLint documentation
for more information about extending configuration files.
āļø indicates that a rule is recommended for all users.
š indicates that a rule is fixable.
| Name | āļø | š | Description |
| ------------------------------------------------------------------------------------------------------------------------------------------- | --- | --- | ------------------------------------------------------------------------------------- |
| missing-expect | āļø | | Ensure tests have at least one expect. |
| no-debug | āļø | | t.debug() should not exist permanently, use only for debugging of a test failure. |fixture.skip()
| no-disabled-tests | āļø | | Prevent tests from being disabled by or test.skip() and forgotten. |
| no-duplicate-titles | āļø | | All test case titles should be unique. |
| no-focused-tests | āļø | | Don't allow a single test or fixture to take all the focus. |
This eslint-plugin-testcafe-community plugin was a fork from the originaleslint-plugin-testcafev0.2.1
project, which has not seen an update since December 2016 (). You **do
not** need to install both packages. This package is an expanded replacement for
the original.
Due to the similarities between Test-Driven Development (TDD) terminology
supported by both Jest & Mocha and
the matching terminology for TestCafe's API, you may run into lint confusion
errors. This likely happens when your repository includes both Jest/Mocha for
unit tests and TestCafe for End-to-End (E2E) tests.
You have a couple solutions to avoid these issues:
1. \[RECOMMENDED] Top-level directory overrides. ESLint configurations work
best with a bare minimum base configuration and then specific overrides
entries per additional plugin configuration you need to add.
This is the recommendation since it best conforms to the conventions of
these frameworks. Jest recommends unit testing code is always nearest to__tests__
the code it is testing in multiple directories following thesrc
file tree your or lib directories. Since E2E tests are written fromtests
a higher level abstraction without regard for how the code actually works,
it is best to keep these files at a project root level folder. Ineslintrc
relation to your definition, it is easiest to maintain by*.test.{ts,js}
defining all the overrides at the top level so that it is easier to debug
issues. If you want to share the filename convention, thenexcludedFiles
you should consider the directive for specific directories
(similar to #3).
A such configuration would look something like this:
`js
//
module.exports = {
root: true,
ignorePatterns: ["*.json"],
// These would affect all files. Note that overrides are combined not replaced
// extends:[]
// plugins:[]
overrides: [
{
// For Typescript
files: ["*.ts"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.eslint.json", // Custom lint-only config
sourceType: "module"
},
plugins: ["@typescript-eslint"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:prettier/recommended"
]
},
{
// TS/JS unit tests (inherits any matches from above)
// Make sure this entry is specific enough (through regex or excludedFiles)
// not to match the same files as the e2e files
files: ["*.spec.{js,ts}", "/__tests__/.{js,ts}"],
env: {
"jest/globals": true
},
extends: ["plugin:jest/recommended", "plugin:jest/style"],
plugins: ["jest"]
},
{
// Typescript/JavaScript e2e tests (inherits from any matches above)
files: ["tests/**.test.{js,ts}"],
excludedFiles: ["src/", "lib/"],
extends: ["plugin:testcafe-community/recommended"],
plugins: ["testcafe-community"]
}
]
};
`
2. Filename filters. Use a file naming convention to distinguish the
difference between a unit test and an E2E test. Configure ESLint via
overrides entry to only apply the jest/mocha plugins to *.spec.ts andtestcafe-community
plugin rules to *.test.js files.
3. Single directory configuration. Creates a separate folder to house all
your TestCafe specific tests usually at the root level of the project. In
this directory, create another .eslintrc` configuration file that defines an
override entry for the files in this directory and loads the
testcafe-community plugin/extension.
PR's & Issue contributions welcome! Please adhere to
contributing guidelines
or your submission will be closed or delayed.