How to use the ok function from should

Find comprehensive JavaScript should.ok code examples handpicked from public code repositorys.

The should.ok function is used to assert that a given value is truthy.

97
98
99
100
101
102
103
104
105
106
describe.skip('custom resource function: getBlob()', function () {
  it('should return a file content', function (done) {
    gitlab.repositorys.getBlob({ id: 55045, sha: 'master', filepath: 'README.md' }, function (err, blob) {
      should.not.exists(err);
      should.exists(blob);
      should.ok(Buffer.isBuffer(blob));
      blob.should.be.instanceof(Buffer);
      blob.length.should.above(0);
      blob.toString().should.containEql('gitlab-client-unittest');
      done();
fork icon6
star icon6
watch icon10

+ 5 other calls in file

69
70
71
72
73
74
75
76
77
78
    let remoteObject = new CacheRemoteObject(objectUrl, objectRefreshTime);

    const result = await remoteObject.getFresh(context);       
    should.deepEqual(result, remoteObjectValue);
    await sleep(objectRefreshTime + 1);
    should.ok(remoteObject.isCacheStale(context));
  });
});

describe('.getCached', function () {
fork icon1
star icon3
watch icon0

How does should.ok work?

should.ok is a method in the should.js assertion library that checks whether a given value is truthy or not. If the value is truthy, the assertion passes, and if the value is falsy, the assertion fails with an appropriate error message.

Ai Example

1
2
3
4
5
6
7
8
const should = require("should");
const assert = require("assert");

const myValue = "foo";

should.ok(myValue === "foo", 'myValue should be "foo"');

assert.ok(myValue === "foo", 'myValue should be "foo"');

In this example, should.ok and assert.ok both check if the myValue variable is equal to the string "foo". If it is not, then they throw an assertion error with the provided error message.