
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
babel-plugin-spock
Advanced tools
Structured JS test cases, inspired by Spock Framework
Note: This module is in an early stage of development and currently provides only a small set of features.
test('basic arithmetic', () => {
expect: {
1 + 2 === 3;
3 * 3 >= 4 * 4; // falsy
}
});
Expected value to be (operator: ==):
true
Received:
false
assert(3 * 3 >= 4 * 4)
| | |
| | 16
9 false
npm install --save-dev babel-plugin-spock power-assert
This module is a Babel plugin that needs to transform your test sources in order to generate assertions, so your test runner will need support for Babel. Babel integrates quite nicely into most modern test runners. Check the documentation of your test runner for instructions on how to configure Babel (e.g. for Jest, AVA etc.) or consult Babel's own documentation.
Once Babel is set up for your test files, simply add "babel-plugin-spock"
to
the plugins
array in your babel configuration and you're good to go.
Note: The last version of this plugin that supports Babel 6 is 0.1.0
Note: At the moment, your test files need to import assert from 'power-assert';
for this to work. In the future, the plugin will be able to handle this for you automatically.
Inside of a block labeled with expect:
or then:
, all statements will be
considered assertions and evaluated to check for truth:
expect: {
1 < 2;
}
when
-then
blocks can be particularly useful and expressive for code with
side effects:
// The 'when' label here does not have a special meaning
// It is used simply to make the test more structured
when: {
abc.setXyz(1);
}
then: {
abc.getXyz() === 1;
}
Single labeled statements are also possible:
expect: 'a' + 'b' === 'ab';
Note that these blocks can only contain statements that can be evaluated as expressions. For example, an if statement would not be valid:
// BAD
expect: {
if (x < 1) x === 0.5;
else x === 2;
}
However, you can nest an assertion block into other structures:
// GOOD
if (x < 1) expect: x === 0.5;
else expect: x === 2;
If you want to perform more complicated checks, it might be helpful to look for small helper libraries on npm. For example, deep-strict-equal can help perform deep equality checks on object structures. In the future, this plugin might provide special syntax for such use cases.
The test you will write using this plugin often employ syntax that is otherwise uncommon in JavaScript code. For this reason, if you use a linter such as ESLint, you will likely see annoying warnings all over your tests. To work around this, most linters will give you multiple options:
You can configure this plugin using Babel's regular plugin configuration mechanism. The following options are available:
powerAssert
The plugin can seamlessly generate assertions that produce detailed mismatch messages to help you figure out what exactly about the assertion went wrong. Turning this feature off could be useful if you're tests appear to run slowly or you are experiencing other issues with your assertions.
This feature is powered by the awesome project power-assert.
default: true
FAQs
Structured JS test cases, inspired by Spock Framework
The npm package babel-plugin-spock receives a total of 2 weekly downloads. As such, babel-plugin-spock popularity was classified as not popular.
We found that babel-plugin-spock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.