Skip to content

Commit 0a5e3a8

Browse files
authored
Update readme.md
1 parent 8f6a742 commit 0a5e3a8

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

readme.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<br/>
88

9-
## 📗 46+ best practices: Super-comprehensive and exhaustive
9+
## 📗 50+ best practices: Super-comprehensive and exhaustive
1010

1111
This is a guide for JavaScript & Node.js reliability from A-Z. It summarizes and curates for you dozens of the best blog posts, books and tools the market has to offer
1212

@@ -958,6 +958,38 @@ it("When updating site name, get successful confirmation", async () => {
958958

959959
</details>
960960

961+
<br/>
962+
963+
## ⚪ ️2.8 Choose a clear data clean-up strategy: After-all (recommended) or after-each
964+
965+
:white_check_mark: **Do:** The timing when the tests clean the database determines the way the tests are being written. The two most viable options are cleaning after all the tests vs cleaning after every single test. Choosing the latter option, cleaning after every single test guarantees clean tables and builds convenient testing perks for the developer. No other records exist when the test starts, one can have certainty which data is being queried and even might be tempted to count rows during assertions. This comes with severe downsides: When running in a multi-process mode, tests are likely to interfere with each other. While process-1 purges tables, at the very moment process-2 queries for data and fail (because the DB was suddenly deleted by process-1). On top of this, It's harder to troubleshoot failing tests - Visiting the DB will show no records.
966+
967+
The second option is to clean up after all the test files have finished (or even daily!). This approach means that the same DB with existing records serves all the tests and processes. To avoid stepping on each other's toes, the tests must add and act on specific records that they have added. Need to check that some record was added? Assume that there are other thousands of records and query for records that were added explicitly. Need to check that a record was deleted? Can't assume an empty table, check that this specific record is not there. This technique brings few powerful gains: It works natively in multi-process mode, when a developer wishes to understand what happened - the data is there and not deleted. It also increases the chance of finding bugs because the DB is full of records and not artificially empty.
968+
<br/>
969+
970+
**Otherwise:** Without a strategy to separate records or clean - Tests will step on each other toes; Using transactions will work only for relational DB and likely to get complicated once there are inner transactions
971+
972+
<br/>
973+
974+
<details><summary>✏ <b>Code Examples</b></summary>
975+
976+
<br/>
977+
978+
### :clap: Cleaning after ALL the tests. Not neccesserily after every run. The more data we have while the tests are running - The more it resembles the production perks
979+
980+
```javascript
981+
// After-all clean up (recommended)
982+
// global-teardown.js
983+
module.exports = async () => {
984+
// ...
985+
if (Math.ceil(Math.random() * 10) === 10) {
986+
await new OrderRepository().cleanup();
987+
}
988+
};
989+
```
990+
991+
</details>
992+
961993
<br/><br/>
962994

963995
# Section 3️⃣: Frontend Testing

0 commit comments

Comments
 (0)