How to assert in a JPM test

292 Views Asked by At

My synthetic test:

exports.testAssertObject = function(assert) {
    console.log(arguments);
    assert.ok(assert, "Assert object exists");
};

fails in JPM with TypeError: assert.ok is not a function:

$jpm --version
0.0.29
$jpm test
JPM undefined Starting jpm test on Reuse Tab
Creating XPI
JPM undefined XPI created at ...\AppData\Local\Temp\[email protected] (327ms)
Created XPI at ...\AppData\Local\Temp\[email protected]
JPM undefined Creating a new profile
Running tests on Firefox 38.0a2/Gecko 38.0a2 ({ec8030f7-c20a-464f-9b0e-13a3a9e97384}) under winnt/x86.
console.log: reusetab: {"0":{"console":{},"passed":0,"failed":0,"testRunSummary":[],"expectFailNesting":0,"test":{"name":"./test/test-utils.testAssertObject","passed":0,"failed":0,"errors":{},"last":"START"},"isDone":false,"waitTimeout":null}}
console.error: reusetab: 
JPM undefined   Message: ReferenceError: assert is not defined
  Stack:
    exports.testAssertObject@resource://gre/modules/commonjs/toolkit/loader.js -> resource://jid1-k8e1vrohvmoxwq-at-jetpack/test/test-utils.js:3:2
start@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/deprecated/unit-test.js:559:7
startMany/runNextTest/<@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/deprecated/unit-test.js:522:11
Handler.prototype.process@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:867:23
this.PromiseWalker.walkerLoop@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:746:7
this.PromiseWalker.scheduleWalkerLoop/<@resource://gre/modules/Promise.jsm -> resource://gre/modules/Promise-backend.js:688:37



0 of 1 tests passed.
There were test failures...

I assume, that unlike cfx, jpm does not pass assert object as test method argument?

How do I implement assertion and notify "done" in async tests?

2

There are 2 best solutions below

0
On

To get updated list of assert methods, read

resource://gre/modules/commonjs/sdk/deprecated/unit-test.js

First argument of test method is TestRuner object implemented in this resource and has a different interface:

In particular:

  • ok() method is replaced with assert()
  • throws() is replaced with assertRaises(func, predicate, message) (different signature)
  • equal() is replaced with assertEqual(), many other method are also prefixed with "assert"
  • done() method now belongs to TestRunner too (no second argument for your test method) to declare your method async call waitUntilDone(timeoutInMs) on TestRunner
0
On

JPM (like cfx) does not run tests with the assert method by default, but instead rubs them using an older version of the test harness. All you need to do to use the current test harness is add

require("sdk/test").run(exports);

to your trsts files, as documented on the Unit Testing page you linked.