Skip to content

Commit bd3cf83

Browse files
committed
Add test guidelines
1 parent 93e5c69 commit bd3cf83

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

usage-rules/elixir.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@
4141
- Read the docs and options before using tasks (by using `mix help task_name`)
4242
- To debug test failures, run tests in a specific file with `mix test test/my_test.exs` or run all previously failed tests with `mix test --failed`
4343
- `mix deps.clean --all` is **almost never needed**. **Avoid** using it unless you have good reason
44+
45+
## Test guidelines
46+
47+
- **Always use `start_supervised!/1`** to start processes in tests as it guarantees cleanup between tests
48+
- **Avoid** `Process.sleep/1` and `Process.alive?/1` in tests
49+
- Instead of sleeping to wait for a process to finish, **always** use `Process.monitor/1` and assert on the DOWN message:
50+
51+
ref = Process.monitor(pid)
52+
assert_receive {:DOWN, ^ref, :process, ^pid, :normal}
53+
54+
- Instead of sleeping to synchronize before the next call, **always** use `_ = :sys.get_state/1` to ensure the process has handled prior messages
55+

0 commit comments

Comments
 (0)