Best Practices
Assigning Return Values
Do not assign the return value of commands with const , let , or var in your spec.ts.
Instead, use the *.command.ts files to declare all the selectors you need and call them in the spec.ts to execute the actions required.
Selecting Elements
A good practice to remove dependencies to CSS or JS changes is to use data-* attributes on the components we are going to test, so we can create a more direct selector with more context.
As you can see in the code above, you can create a data-cy attribute (line 6 of example.html) in the component you want to test from the sample and call the selector using it. This way, if you make changes to, for example, CSS classes, it won't be affected.
Avoid dependence on other tests
Each test has to be independent from the others, remember that they are not unit tests and if you need several actions to test something, all of them can be performed in the same test.
To make sure you have created an independent test just change the it to an it.only, if this test passes it means you have done a good job. Here is an example of using the it.only:
Fixture
The "fixture" command is responsible for loading a fixed set of data located in a file. It is a good practice to use this command instead of declaring local variables for values that I need to use constantly in tests. An example:
In the fixture folder create a .json file where you can store your variables.
2. Use the fixture command in your tests.
Avoid doing things like this:
Avoid declaring global variables in the spec.ts file or in the command.ts file, if you need to use one value in several tests use the fixture command.
Last updated
Was this helpful?