assert.ok
and assert.notOk
pass for any truthy/falsy argument. As many expressions evaluate to true/false in JavaScript the usage of assert.ok
is potentially error prone. In general, it should be advisable to always test for exact values in tests which makes tests a lot more solid.
An example when using assert.ok
can involuntarily go wrong:
test('test myFunc returns a truthy value', (assert) => {
assert.ok(myFunc);
});
Here by mistake a developer just passed to assert.ok
a pointer to myFunc
instead of explicitly calling it. This test is going to pass no matter how myFunc
changes. Using assert.strictEqual(myFunc, theReturnValue)
solves the problem as this becomes an explicit check for equality.
The following patterns are considered warnings:
QUnit.test('Name', function (assert) { assert.ok(a); });
QUnit.test('Name', function (foo) { foo.ok(a); });
QUnit.test('Name', function () { ok(a); });
QUnit.test('Name', function (assert) { assert.notOk(a); });
QUnit.test('Name', function (foo) { foo.notOk(a); });
QUnit.test('Name', function () { notOk(a); });
The following patterns are not considered warnings:
QUnit.test('Name', function (assert) { assert.strictEqual(a, true); });
QUnit.test('Name', function (foo) { foo.strictEqual(a, true); });
QUnit.test('Name', function () { strictEqual(a, true); });
QUnit.test('Name', function (assert) { assert.strictEqual(a, false); });
QUnit.test('Name', function (foo) { foo.strictEqual(a, false); });
QUnit.test('Name', function () { strictEqual(a, false); });
QUnit.test('Name', function (assert) { assert.deepEqual(a, b); });
QUnit.test('Name', function (foo) { foo.deepEqual(a, b); });
QUnit.test('Name', function () { deepEqual(a, b); });
QUnit.test('Name', function (assert) { assert.propEqual(a, b); });
QUnit.test('Name', function (foo) { foo.propEqual(a, b); });
QUnit.test('Name', function () { propEqual(a, b); });