Skip to content

Commit 1c5857c

Browse files
authored
Test style guidance (#1957)
* Create test-style-guide.md * Create tests.code-snippets * Update test-style-guide.md
1 parent ba741fa commit 1c5857c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.vscode/tests.code-snippets

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"Test: macro": {
3+
"prefix": "macro",
4+
"body": [
5+
"const macro = test.macro(($1) => async (t) => {",
6+
" $0",
7+
"});"
8+
]
9+
},
10+
"Test: macro w/title": {
11+
"prefix": "macroTitle",
12+
"body": [
13+
"const macro = test.macro(($1) => [",
14+
" (given) => given,",
15+
" async (t) => {",
16+
" $0",
17+
" }",
18+
"]);"
19+
]
20+
},
21+
"Test: suite": {
22+
"prefix": "suite",
23+
"body": [
24+
"test.suite(\"$1\", (${2|test,{context},{contextEach}|}) => {",
25+
" ${2/(test$)|(\\{context\\}$)|(\\{contextEach\\}$)/${2:+const test = context(}${3:+const test = contextEach(}/m}$0",
26+
"});"
27+
]
28+
},
29+
"Test: context builder": {
30+
"prefix": "ctx",
31+
"body": [
32+
"export const ctx${1:Name} = async (t) => {",
33+
" $0",
34+
" return {};",
35+
"};",
36+
"export namespace ctx$1 {",
37+
" export type Ctx = Awaited<ReturnType<typeof ctx$1>>;",
38+
" export type T = ExecutionContext<Ctx>;",
39+
"}"
40+
]
41+
},
42+
"Test: before": {
43+
"prefix": "before",
44+
"body": [
45+
"test.before(async (t) => {",
46+
" $0",
47+
"});"
48+
]
49+
},
50+
"Test: beforeEach": {
51+
"prefix": "beforeEach",
52+
"body": [
53+
"test.beforeEach(async (t) => {",
54+
" $0",
55+
"});"
56+
]
57+
},
58+
"Test: teardown": {
59+
"prefix": "teardown",
60+
"body": [
61+
"t.teardown(async (t) => {",
62+
" $0",
63+
"});"
64+
]
65+
}
66+
}

development-docs/test-style-guide.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Test guidelines
2+
3+
*These notes are my attempt at keeping myself consistent when writing tests. Nothing too formal.*
4+
5+
### DRYing up tests with reusable functions
6+
7+
If a reusable function does not need access to anything from the context,
8+
it's a plain function.
9+
10+
If the reusable function needs access to stuff from context, it's a
11+
context helper.
12+
13+
If the reusable function implements all the necessary assertions within itself, it's a macro.
14+
15+
### Function args
16+
17+
Plain functions w/many options should accept a single option-bag.
18+
Should also have `setOptions` method to overlay additional options, and `getOptions` to inspect.
19+
*At time of writing, I have not implemented `setOptions` nor `getOptions`*
20+
21+
Never destructure `t`
22+
Never destructure `test`
23+
Only exception: destructure `{context}` or `{contextEach}` if calling `const test = context(fooCtx)` b/c avoids `test = _test.context()`
24+
25+
### Consistent naming
26+
27+
When exec-ing a process:
28+
29+
Awaited result is in local var `r`
30+
Non-awaited is in local var `p`
31+
Do not destructure either way.
32+
33+
Context builders are named with a `ctx` prefix and also declare a couple types on their namespace. See src/test/helpers/ctx* for format.

0 commit comments

Comments
 (0)