Skip to content

Commit c81fbdf

Browse files
authored
Creating guide_addingtest.md (#4388)
a proper guiding steps to add test for the root directory
1 parent 15d4aae commit c81fbdf

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

β€Žjs/guide_addingtest.mdβ€Ž

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Test Suite Guidelines
2+
3+
The Music Blocks repository follows a structured approach for testing using **Jest**. All tests should be placed inside the `_tests_` directory to ensure consistency and maintainability.
4+
5+
## πŸ›  Setting Up the Test Environment
6+
7+
Before running or writing tests, ensure that dependencies are installed:
8+
9+
```sh
10+
npm install
11+
```
12+
13+
To run the test suite, use:
14+
15+
```sh
16+
npm test
17+
```
18+
19+
For running tests with detailed logs:
20+
21+
```sh
22+
npm test -- --verbose
23+
```
24+
25+
## πŸ“‚ Directory Structure
26+
27+
```
28+
/musicblocks
29+
│── js/ # Source code
30+
│── js/_tests_/ # Test directory
31+
│── js/utils/_tests_/ # Tests for the utils subdirectory (same format is followed for each directory)
32+
│── jest.config.js # Jest configuration (try not to edit this)
33+
│── package.json # Project dependencies
34+
│── js/guide_test.md # This guide
35+
```
36+
37+
## πŸ“Œ Key Guidelines Before Writing Tests
38+
39+
### βœ… General Rules
40+
- All test files **must be placed inside the `_tests_` folder of the respective directory**.
41+
- Follow the naming convention: **`<filename>.test.js`**.
42+
- Ensure **100% function coverage** when adding tests.
43+
- **Mock dependencies** where necessary to isolate unit tests.
44+
45+
### πŸ”„ Import/Export Conventions
46+
- The Music Blocks repository **strictly follows `const` for imports and exports**.
47+
- **For CommonJS (`require/module.exports`)**, use:
48+
49+
```js
50+
const { functionName } = require('../src/file.js');
51+
```
52+
53+
Ensure `file.js` contains:
54+
55+
```js
56+
if (typeof module !== 'undefined' && module.exports) {
57+
module.exports = { functionName };
58+
}
59+
```
60+
61+
### πŸ“‘ Writing Tests
62+
- Use `describe` blocks to group related tests.
63+
- Use `test` or `it` for defining test cases.
64+
- Use **Jest matchers** like `toBe`, `toEqual`, `toHaveBeenCalled`.
65+
66+
#### πŸ”Ή Example Test Structure:
67+
68+
```js
69+
const { myFunction } = require('../src/myFile.js');
70+
71+
describe('My Function Tests', () => {
72+
test('should return expected output', () => {
73+
expect(myFunction()).toBe('expectedValue');
74+
});
75+
});
76+
```
77+
78+
## πŸ›‘ Common Mistakes to Avoid
79+
❌ Making changes in the root file.
80+
❌ Modifying `jest.config.js` unnecessarily.
81+
❌ Placing test files **outside** `_tests_` (always keep them inside).
82+
❌ Using `var` or `let` for imports (always use `const`).
83+
❌ Forgetting to mock dependencies when needed.
84+
❌ Not handling async tests properly (use `async/await` or `done`).
85+
86+
## πŸš€ Running Specific Tests
87+
To run a specific test file:
88+
89+
```sh
90+
npm test _tests_/filename.test.js
91+
```
92+
93+
To watch tests while coding:
94+
95+
```sh
96+
npm test -- --watch
97+
```
98+
99+
## πŸ”„ Updating Snapshots
100+
If using Jest snapshots, update them with:
101+
102+
```sh
103+
npm test -- -u
104+
```
105+
106+
## 🎯 Contribution Guidelines
107+
- Ensure all tests pass before creating a PR.
108+
- Maintain code readability and add comments where needed.
109+
- Adhere to the **import/export conventions** stated above.
110+
- **Do not merge** without proper test coverage.

0 commit comments

Comments
Β (0)