|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +How to write unit tests for block files. |
| 4 | + |
| 5 | +## Running Tests |
| 6 | + |
| 7 | +```bash |
| 8 | +npm test # Run all tests |
| 9 | +npm test -- path/to/file.test.js # Run specific test file |
| 10 | +npm test -- --coverage # Run with coverage report |
| 11 | +``` |
| 12 | + |
| 13 | +## Testing Blocks |
| 14 | + |
| 15 | +Block files (in `js/blocks/`) need specific mocks. Copy this template and modify for your block: |
| 16 | + |
| 17 | +### Basic Template |
| 18 | + |
| 19 | +Add a license header at the top of your test file: |
| 20 | + |
| 21 | +```javascript |
| 22 | +/** |
| 23 | + * MusicBlocks v3.6.2 |
| 24 | + * |
| 25 | + * @author Your Name |
| 26 | + * |
| 27 | + * @copyright 2026 Your Name |
| 28 | + * |
| 29 | + * @license |
| 30 | + * This program is free software: you can redistribute it and/or modify |
| 31 | + * it under the terms of the GNU Affero General Public License as published by |
| 32 | + * the Free Software Foundation, either version 3 of the License, or |
| 33 | + * (at your option) any later version. |
| 34 | + */ |
| 35 | +``` |
| 36 | + |
| 37 | +Then add the test code: |
| 38 | + |
| 39 | +```javascript |
| 40 | +const { setupYourBlocks } = jest.requireActual("../YourBlocks"); |
| 41 | + |
| 42 | +global._ = s => s; |
| 43 | +global.NOINPUTERRORMSG = "NO_INPUT"; |
| 44 | + |
| 45 | +class BaseBlock { |
| 46 | + constructor(name) { |
| 47 | + this.name = name; |
| 48 | + } |
| 49 | + setPalette(palette) { |
| 50 | + this.palette = palette; |
| 51 | + } |
| 52 | + setHelpString(help) { |
| 53 | + this.help = help; |
| 54 | + } |
| 55 | + formBlock(defn) { |
| 56 | + this.formDefn = defn; |
| 57 | + } |
| 58 | + setup(activity) { |
| 59 | + activity.registeredBlocks = activity.registeredBlocks || {}; |
| 60 | + activity.registeredBlocks[this.name] = this; |
| 61 | + return this; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +class FlowBlock extends BaseBlock { |
| 66 | + constructor(name) { |
| 67 | + super(name); |
| 68 | + } |
| 69 | + flow() {} |
| 70 | +} |
| 71 | + |
| 72 | +class ValueBlock extends BaseBlock { |
| 73 | + constructor(name) { |
| 74 | + super(name); |
| 75 | + } |
| 76 | + arg() {} |
| 77 | +} |
| 78 | + |
| 79 | +global.BaseBlock = BaseBlock; |
| 80 | +global.FlowBlock = FlowBlock; |
| 81 | +global.ValueBlock = ValueBlock; |
| 82 | + |
| 83 | +describe("YourBlocks", () => { |
| 84 | + let activity; |
| 85 | + let logo; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + jest.clearAllMocks(); |
| 89 | + |
| 90 | + activity = { |
| 91 | + registeredBlocks: {}, |
| 92 | + blocks: { |
| 93 | + blockList: [], |
| 94 | + palettes: { dict: {} } |
| 95 | + }, |
| 96 | + turtles: { |
| 97 | + ithTurtle: jest.fn(() => ({ |
| 98 | + singer: { justCounting: [] } |
| 99 | + })) |
| 100 | + }, |
| 101 | + errorMsg: jest.fn() |
| 102 | + }; |
| 103 | + |
| 104 | + logo = { |
| 105 | + parseArg: jest.fn(), |
| 106 | + runFromBlock: jest.fn() |
| 107 | + }; |
| 108 | + |
| 109 | + setupYourBlocks(activity); |
| 110 | + }); |
| 111 | + |
| 112 | + const getBlock = name => activity.registeredBlocks[name]; |
| 113 | + |
| 114 | + test("registers blocks", () => { |
| 115 | + expect(activity.registeredBlocks).toHaveProperty("yourblock"); |
| 116 | + }); |
| 117 | + |
| 118 | + test("block flow works", () => { |
| 119 | + const block = getBlock("yourblock"); |
| 120 | + // Replace with actual args for your block |
| 121 | + const result = block.flow([1 / 4, "sol"], logo, 0, 5); |
| 122 | + expect(result).toEqual(["sol", 1]); |
| 123 | + }); |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
| 127 | +### Common Mocks |
| 128 | + |
| 129 | +| Global | Purpose | |
| 130 | +| ------------------------------ | ---------------------------------- | |
| 131 | +| `activity.errorMsg` | Captures error messages | |
| 132 | +| `activity.blocks.blockList` | Simulates block connections | |
| 133 | +| `logo.parseArg` | Returns values for block arguments | |
| 134 | +| `activity.turtles.ithTurtle()` | Returns turtle state | |
| 135 | + |
| 136 | +### Tips |
| 137 | + |
| 138 | +1. Check the source file's `flow()` or `arg()` method to understand what it expects |
| 139 | +2. Mock only what's needed for the specific test |
| 140 | +3. Use `jest.fn()` for methods you want to verify were called |
| 141 | +4. Run `npx prettier --write` on your test file before committing |
0 commit comments