Stay organized with collections Save and categorize content based on your preferences.
After changing or adding code, you should run existing unit tests and consider writing more. All tests are executed on the uncompressed versions of the code.
There are two sets of unit tests: JS tests and block generator tests.
JS TestsThe JS tests confirm the operation of internal JavaScript functions in Blockly's core. We use Mocha to run unit tests, Sinon to stub dependencies, and Chai to make assertions about the code.
Running TestsIn both blockly and blockly-samples, npm run test
will run the unit tests. In blockly, this will also run other tests such as linting and compilation. You can also open tests/mocha/index.html
in a browser to interactively run all mocha tests.
We use the Mocha TDD interface to run tests. Tests are organized into suites, which can contain both additional sub-suites and/or tests. Generally, each component of Blockly (such as toolbox
or workspace
) has its own test file which contains one or more suites. Each suite can have a setup
and teardown
method that will be called before and after, respectively, each test in that suite.
We have a number of helper functions specific to Blockly that may be useful when writing tests. These can be found in core and in blockly-samples.
The helper functions include sharedTestSetup
and sharedTestTeardown
which are required to be called before and after your tests (see Requirements section).
this.clock.runAll
).defineBlocksWithJsonArray
.this
context that are meant to be accessible:
this.clock
(but should not be restored else it will cause issues in sharedTestTeardown
)this.eventsFireStub
this.sharedCleanup
(to be used with addMessageToCleanup
and addBlockTypeToCleanup
) (NOTE: you don't need to use addBlockTypeToCleanup
if you defined the block using defineBlocksWithJsonArray
)The function has one optional options
parameter to configure setup. Currently, it's only used to determine whether to stub Blockly.Events.fire
to fire immediately (will stub by default).
this.workspace
(depending on where it was defined, see Test Requirements section for more information).defineBlocksWithJsonArray
and addBlockTypeToCleanup
.addMessageToCleanup
.sharedTestSetup.call(this);
as the first line in the setup of the outermost suite and sharedTestTeardown.call(this);
as the last line in the teardown of the outermost suite for a file.index.html
. See below for an example.this.workspace
. In most tests, you will define this.workspace
in the outermost suite and use it for all subsequent tests, but in some cases you might define or redefine it in an inner suite (for example, one of your tests requires a workspace with different options than you originally set up). It must be disposed of at the end of the test.
this.workspace
in the outermost suite and never redefine it, no further action is needed. It will be automatically disposed of by sharedTestTeardown
.this.workspace
for the first time in an inner suite (i.e. you did not define it in the outermost suite), you must manually dispose of it by calling workspaceTeardown.call(this, this.workspace)
in the teardown of that suite.this.workpace
in the outermost suite, but then redefine it in an inner test suite, you must first call workspaceTeardown.call(this, this.workspace)
before redefining it to tear down the original workspace defined in the top level suite. You must also manually dispose the new value by calling workspaceTeardown.call(this, this.workspace)
again in the teardown of this inner suite.Unit tests generally follow a set structure, which can be summarized as arrange, act, assert.
In a simple test, there may not be any behavior to arrange, and the act and assert stages can be combined by inlining the call to the code under test in the assertion. For more complex cases, your tests will be more readable if you stick to these 3 stages.
Here is an example test file (simplified from the real thing).
suite('Flyout', function() {
setup(function() {
sharedTestSetup.call(this);
this.toolboxXml = document.getElementById('toolbox-simple');
this.workspace = Blockly.inject('blocklyDiv',
{
toolbox: this.toolboxXml
});
});
teardown(function() {
sharedTestTeardown.call(this);
});
suite('simple flyout', function() {
setup(function() {
this.flyout = this.workspace.getFlyout();
});
test('y is always 0', function() {
// Act and assert stages combined for simple test case
chai.assert.equal(this.flyout.getY(), 0, 'y coordinate in vertical flyout is 0');
});
test('x is right of workspace if flyout at right', function() {
// Arrange
sinon.stub(this.flyout.targetWorkspace, 'getMetrics').returns({
viewWidth: 100,
});
this.flyout.targetWorkspace.toolboxPosition = Blockly.TOOLBOX_AT_RIGHT;
this.flyout.toolboxPosition_ = Blockly.TOOLBOX_AT_RIGHT;
// Act
var x = this.flyout.getX();
// Assert
chai.assert.equal(x, 100, 'x is right of workspace');
});
});
});
Things to note from this example:
setup
and teardown
methods.chai.assert.equal(actualValue, expectedValue, optionalMessage)
. If you swap actual
and expected
, the error messages won't make sense.getMetrics
function to return a canned response which we can easily check for in our test assertions.setup
methods for each suite should contain only generic setup that applies to all tests. If a test for a particular behavior relies on a certain condition, that condition should clearly be stated in the relevant test.Set .only()
or .skip()
on a test or suite to run only that set of tests, or skip a test. For example:
suite.only('Workspace', function () {
suite('updateToolbox', function () {
test('test name', function () {
// ...
});
test.skip('test I don’t care about', function () {
// ...
});
});
});
Remember to remove these before committing your code.
Each block has its own unit tests. These tests verify that blocks generate code than functions as intended.
tests/generators/index.html
in Firefox or Safari. Note that Chrome and Opera have security restrictions that prevent loading the tests from the local "file://" system (Issues 41024 and 47416).tests/generators/index.html
in a browser.tests/generators/
.Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["Blockly has two sets of unit tests: JS tests for internal JavaScript functions and block generator tests for verifying block code generation."],["JS tests are run using Mocha, Sinon, and Chai, and can be executed with `npm run test` or through a browser interface."],["When writing JS tests, follow the arrange, act, assert structure, utilize provided test helpers, and adhere to the outlined test requirements for setup and teardown."],["Block generator tests involve loading predefined blocks or custom XML, generating code in various languages, and then executing that code in the corresponding interpreter to validate its functionality."],["To edit block generator tests, modify blocks in the provided web interface, obtain the generated XML, and update the appropriate file within the tests/generators/ directory."]]],["After coding changes, run unit tests, including JS and block generator tests. JS tests use Mocha, Sinon, and Chai. Run tests with `npm run test` or via `tests/mocha/index.html`. Tests follow an arrange, act, assert structure. Use `sharedTestSetup` and `sharedTestTeardown` helper functions, disposing of any created workspaces. Block generator tests verify code output in JavaScript, Python, PHP, Lua, and Dart interpreters, indicated by \"OK\" output. Edit block tests by modifying them in the workspace, then copy the resulting XML to the relevant file.\n"]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4