|
| 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