QUnit, assert not OK?
Asked Answered
A

5

3

Sorry if this is obvious, but is there a notOK or equivalent function in QUnit, if we want to assert that a method returns false?

I can't see a way to negate OK in the documentation.

I tried:

!ok...

but that didn't work.

Almita answered 1/8, 2013 at 12:57 Comment(0)
O
8

You could use: ok(!method_expected_to_be_false)

Oney answered 1/8, 2013 at 13:1 Comment(1)
I just found this answer while searching for an alternative to the now-existing notOk assertion. I was hoping to find a simple assert.false() because expecting something to be "false" is very different than expecting something to be "not ok" (which suggests failure - an exception, rejected promise, etc)Carothers
A
2

According to the documentation :

The most basic assertion in QUnit, ok() requires just one argument. If the argument evaluates to true, the assertion passes; otherwise, it fails.

You can verify that a method return a false value by writing an expression which evaluates to a true value in the case the method returns false, and vice versa. The easiest expression to do this is the NOT operator, which in JavaScript is expressed through !

test( "Test method returns false ", function() {
  ok( method() == false, "Method returned false" );
  // or using a the negation operator
  ok( !method(), "Method returned false" );
});
Amazonite answered 1/8, 2013 at 13:5 Comment(0)
A
1

The better approach would be to use:

notOk(<something>);

as it will be more expressive than stating:

ok(!<something>);
Argentinaargentine answered 22/9, 2015 at 12:25 Comment(1)
Not sure how long ago you checked the QUnit documentation, but there is a notOk (api.qunitjs.com/assert/notOk) so I suggest to use that.Argentinaargentine
I
1

starting from qunit 1.18 there is a dedicated function:

assert.notOk(valueToBeTested);
Italianize answered 29/1, 2016 at 17:9 Comment(0)
E
0

If this is something you really, really want, you can add it with QUnit.extend():

QUnit.extend(QUnit.assert, {
    notOk: function (result, message) {
        message = message || (!result ? "okay" : "failed, expected argument to be falsey, was: " +
        QUnit.dump.parse(result));
        QUnit.push(!result, result, false, message);
    },
});
Eleph answered 18/11, 2014 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.