summaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-09-14 15:02:52 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-09-14 15:02:52 +0000
commitb39170277a588c6d5c6054fbf1c703cec8b71696 (patch)
tree91e167b1a2f0fb3806d44a8baf39acc6c57055a9 /doc
parent4588bbc93a7857eb2d031a4f3e0212c0aa46b846 (diff)
downloadgitlab-ce-b39170277a588c6d5c6054fbf1c703cec8b71696.tar.gz
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'doc')
-rw-r--r--doc/development/testing_guide/frontend_testing.md32
1 files changed, 32 insertions, 0 deletions
diff --git a/doc/development/testing_guide/frontend_testing.md b/doc/development/testing_guide/frontend_testing.md
index 42ca65a74f2..83d03097466 100644
--- a/doc/development/testing_guide/frontend_testing.md
+++ b/doc/development/testing_guide/frontend_testing.md
@@ -691,6 +691,38 @@ unit tests.
Instead of `setImmediate`, use `jest.runAllTimers` or `jest.runOnlyPendingTimers` to run pending timers.
The latter is useful when you have `setInterval` in the code. **Remember:** our Jest configuration uses fake timers.
+## Avoid non-deterministic specs
+
+Non-determinism is the breeding ground for flaky and brittle specs. Such specs end up breaking the CI pipeline, interrupting the work flow of other contributors.
+
+1. Make sure your test subject's collaborators (e.g., axios, apollo, lodash helpers) and test environment (e.g., Date) behave consistently across systems and over time.
+1. Make sure tests are focused and not doing "extra work" (e.g., needlessly creating the test subject more than once in an individual test)
+
+### Faking `Date` for determinism
+
+Consider using `useFakeDate` to ensure a consistent value is returned with every `new Date()` or `Date.now()`.
+
+```javascript
+import { useFakeDate } from 'helpers/fake_date';
+
+describe('cool/component', () => {
+ useFakeDate();
+
+ // ...
+});
+```
+
+### Faking `Math.random` for determinism
+
+Consider replacing `Math.random` with a fake when the test subject depends on it.
+
+```javascript
+beforeEach(() => {
+ // https://xkcd.com/221/
+ jest.spyOn(Math, 'random').mockReturnValue(0.4);
+});
+```
+
## Factories
TBU