Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Noiseless testing framework
equals
, notEquals
, deepEquals
and notDeepEquals
assertion typesbefore
/after
/beforeEach
/afterEach
hooks.only
)Both tests and assertions are declared via the o
function. Tests should have a description and a body function. A test may have one or more assertions. Assertions should appear inside a test's body function and compare two values.
var o = require("ospec")
o("addition", function() {
o(1 + 1).equals(2)
})
o("subtraction", function() {
o(1 - 1).notEquals(2)
})
Assertions may have descriptions:
o("addition", function() {
o(1 + 1).equals(2)("addition should work")
/* in ES6, the following syntax is also possible
o(1 + 1).equals(2) `addition should work`
*/
})
/* for a failing test, an assertion with a description outputs this:
addition should work
1 should equal 2
Error
at stacktrace/goes/here.js:1:1
*/
Tests may be organized into logical groups using o.spec
o.spec("math", function() {
o("addition", function() {
o(1 + 1).equals(2)
})
o("subtraction", function() {
o(1 - 1).notEquals(2)
})
})
Group names appear as a breadcrumb trail in test descriptions: math > addition: 2 should equal 2
Groups can be nested to further organize test groups. Note that tests cannot be nested inside other tests.
o.spec("math", function() {
o.spec("arithmetics", function() {
o("addition", function() {
o(1 + 1).equals(2)
})
o("subtraction", function() {
o(1 - 1).notEquals(2)
})
})
})
The o.spy()
method can be used to create a stub function that keeps track of its call count and received parameters
//code to be tested
function call(cb, arg) {cb(arg)}
//test suite
var o = require("ospec")
o.spec("call()", function() {
o("works", function() {
var spy = o.spy()
call(spy, 1)
o(spy.callCount).equals(1)
o(spy.args[0]).equals(1)
})
})
A spy can also wrap other functions, like a decorator:
//code to be tested
var count = 0
function inc() {
count++
}
//test suite
var o = require("ospec")
o.spec("call()", function() {
o("works", function() {
var spy = o.spy(inc)
spy()
o(count).equals(1)
})
})
If a test body function declares a named argument, the test is assumed to be asynchronous, and the argument is a function that must be called exactly one time to signal that the test has completed. As a matter of convention, this argument is typically named done
.
o("setTimeout calls callback", function(done) {
setTimeout(done, 10)
})
By default, asynchronous tests time out after 20ms. This can be changed on a per-test basis using the timeout
argument:
o("setTimeout calls callback", function(done, timeout) {
timeout(50) //wait 50ms before bailing out of the test
setTimeout(done, 30)
})
Note that the timeout
function call must be the first statement in its test.
Asynchronous tests generate an assertion that succeeds upon calling done
or fails on timeout with the error message async test timed out
.
before
, after
, beforeEach
, afterEach
hooksThese hooks can be declared when it's necessary to setup and clean up state for a test or group of tests. The before
and after
hooks run once each per test group, whereas the beforeEach
and afterEach
hooks run for every test.
o.spec("math", function() {
var acc
o.beforeEach(function() {
acc = 0
})
o("addition", function() {
acc += 1
o(acc).equals(1)
})
o("subtraction", function() {
acc -= 1
o(acc).equals(-1)
})
})
It's strongly recommended to ensure that beforeEach
hooks always overwrite all shared variables, and avoid if/else
logic, memoization, undo routines inside beforeEach
hooks.
Like tests, hooks can also be asynchronous. Tests that are affected by asynchronous hooks will wait for the hooks to complete before running.
o.spec("math", function() {
var acc
o.beforeEach(function(done) {
setTimeout(function() {
acc = 0
done()
})
})
//tests only run after async hooks complete
o("addition", function() {
acc += 1
o(acc).equals(1)
})
o("subtraction", function() {
acc -= 1
o(acc).equals(-1)
})
})
A test can be temporarily made to run exclusively by calling o.only()
instead of o
. This is useful when troubleshooting regressions, to zero-in on a failing test, and to avoid saturating console log w/ irrelevant debug information.
o.spec("math", function() {
o("addition", function() {
o(1 + 1).equals(2)
})
//only this test will be run, regardless of how many groups there are
o.only("subtraction", function() {
o(1 - 1).notEquals(2)
})
})
//define a test
o("addition", function() {
o(1 + 1).equals(2)
})
//run the suite
o.run()
The o.new()
method can be used to create new instances of ospec, which can be run in parallel. Note that each instance will report independently, and there's no aggregation of results.
var _o = o.new('optional name')
_o("a test", function() {
_o(1).equals(1)
})
_o.run()
ospec will automatically evaluate all *.js
files in any folder named /tests
.
o.run()
is automatically called by the cli - no need to call it in your test code.
"scripts": {
...
"test": "ospec",
...
}
$ npm test
Ospec doesn't work when installed globally. Using global scripts is generally a bad idea since you can end up with different, incompatible versions of the same package installed locally and globally.
To work around this limitation, you can use npm-run
which enables one to run the binaries of locally installed packages.
npm install npm-run -g
Then, from a project that has ospec installed as a (dev) dependency:
npm-run ospec
Square brackets denote optional arguments
Defines a group of tests. Groups are optional
Defines a test.
If an argument is defined for the assertions
function, the test is deemed to be asynchronous, and the argument is required to be called exactly one time.
Starts an assertion. There are four types of assertion: equals
, notEquals
, deepEquals
and notDeepEquals
.
Assertions have this form:
o(actualValue).equals(expectedValue)
As a matter of convention, the actual value should be the first argument and the expected value should be the second argument in an assertion.
Assertions can also accept an optional description curried parameter:
o(actualValue).equals(expectedValue)("this is a description for this assertion")
Assertion descriptions can be simplified using ES6 tagged template string syntax:
o(actualValue).equals(expectedValue) `this is a description for this assertion`
Asserts that two values are strictly equal (===
)
Asserts that two values are strictly not equal (!==
)
Asserts that two values are recursively equal
Asserts that two values are not recursively equal
Defines code to be run at the beginning of a test group
If an argument is defined for the setup
function, this hook is deemed to be asynchronous, and the argument is required to be called exactly one time.
Defines code to be run at the end of a test group
If an argument is defined for the teardown
function, this hook is deemed to be asynchronous, and the argument is required to be called exactly one time.
Defines code to be run before each test in a group
If an argument is defined for the setup
function, this hook is deemed to be asynchronous, and the argument is required to be called exactly one time.
Defines code to be run after each test in a group
If an argument is defined for the teardown
function, this hook is deemed to be asynchronous, and the argument is required to be called exactly one time.
Declares that only a single test should be run, instead of all of them
Returns a function that records the number of times it gets called, and its arguments
The number of times the function has been called
The arguments that were passed to the function in the last time it was called
Runs the test suite
Returns a new instance of ospec. Useful if you want to run more than one test suite concurrently
var $o = o.new()
$o("a test", function() {
$o(1).equals(1)
})
$o.run()
Explicitly disallowing modularity and configuration in test-space has a few benefits:
.is()
do?)FAQs
Noiseless testing framework
The npm package ospec receives a total of 6,152 weekly downloads. As such, ospec popularity was classified as popular.
We found that ospec demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.