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.
Before running or writing tests, ensure that dependencies are installed:
npm installTo run the test suite, use:
npm testFor running tests with detailed logs:
npm test -- --verbose/musicblocks
│── js/ # Source code
│── js/_tests_/ # Test directory
│── js/utils/_tests_/ # Tests for the utils subdirectory (same format is followed for each directory)
│── jest.config.js # Jest configuration (try not to edit this)
│── package.json # Project dependencies
│── js/guide_test.md # This guide
- All test files must be placed inside the
_tests_folder of the respective directory. - Follow the naming convention:
<filename>.test.js. - Ensure 100% function coverage when adding tests.
- Mock dependencies where necessary to isolate unit tests.
- Whenever a function is added or its functionality is changed, ensure that the corresponding test cases are added, updated, or refactored. This ensures that the test suite remains accurate and reliable.
-
The Music Blocks repository strictly follows
constfor imports and exports. -
For CommonJS (
require/module.exports), use:const { functionName } = require('../src/file.js');
Ensure
file.jscontains:if (typeof module !== 'undefined' && module.exports) { module.exports = { functionName }; }
- Use
describeblocks to group related tests. - Use
testoritfor defining test cases. - Use Jest matchers like
toBe,toEqual,toHaveBeenCalled.
const { myFunction } = require('../src/myFile.js');
describe('My Function Tests', () => {
test('should return expected output', () => {
expect(myFunction()).toBe('expectedValue');
});
});❌ Making changes in the root file.
❌ Modifying jest.config.js unnecessarily.
❌ Placing test files outside _tests_ (always keep them inside).
❌ Using var or let for imports (always use const).
❌ Forgetting to mock dependencies when needed.
❌ Not handling async tests properly (use async/await or done).
❌ Neglecting to update or refactor test cases when adding or modifying functions.
To run a specific test file:
npm test _tests_/filename.test.jsTo watch tests while coding:
npm test -- --watchIf using Jest snapshots, update them with:
npm test -- -u- Ensure all tests pass before creating a PR.
- Maintain code readability and add comments where needed.
- Adhere to the import/export conventions stated above.
- Do not merge without proper test coverage.
- Always update or refactor test cases when adding or modifying functions to ensure the test suite remains accurate and reliable.