Skip to content

Commit 850942e

Browse files
committed
💥 Add testdouble stubbing chained methods example
1 parent 24b18b1 commit 850942e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# TestDouble.js: Stubbing Chained Methods
2+
3+
```typescript
4+
td.replace(
5+
config.getOptionalConfig,
6+
'getOptionalConfig',
7+
td.function('config.getOptionalConfig'),
8+
)
9+
10+
td.when(config.getOptionalConfig(configKey)).thenReturn({
11+
getOptionalString: td.function('getOptionalString'),
12+
})
13+
14+
const configStubs = [
15+
{ key: 'athenaRoleArn', value: 'arn:aws:iam::123456789:role/dummy-role' },
16+
{ key: 'athenaRegion', value: 'dummy-region' },
17+
{ key: 'athenaRoleSessionName', value: 'dummy-session-name' },
18+
{ key: 'athenaCatalogName', value: 'dummy-catalog-name' },
19+
{ key: 'athenaDatabaseName', value: 'dummy-database-name' },
20+
{ key: 'athenaTableName', value: 'dummy-table-name' },
21+
{ key: 'athenaResultOutputLocation', value: 'dummy-output-location' },
22+
]
23+
24+
configStubs.forEach(({ key, value }) =>
25+
td
26+
.when(config.getOptionalConfig(configKey).getOptionalString(key))
27+
.thenReturn(value),
28+
)
29+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ nav:
6868
- NPKill - node_modules: engineering/javascript/node/node-cleanup.md
6969
- Jest:
7070
- Helpful Tips: engineering/javascript/jest/helpful-tips.md
71+
- Stubbing Chained Methods: engineering/javascript/test-double/stubbing-chained-methods.md
7172
- Ruby:
7273
- Handling JSON: engineering/ruby/handling-json.md
7374
- Git:

0 commit comments

Comments
 (0)