undefinedfix
Sign in

A problem of judging undefined by should assertion Library

lightsaber edited in Sat, 03 Sep 2022

Recently, in the practice of the test process of "should + Mocha + Istanbul", there is a problem when judging the return value of the function. I don't know whether it is a code problem or a bug of "should". If you have encountered it before, please give me some solutions or help information.

const should = require('should');

const obj = {
  a: 1,
  b: 2
};

function getting(key) {
  return obj[key];
}

describe(`test/getting`, function () {
  it(`should return 1 when key is a`, function () {
    getting('a').should.equal(1);
  });
  it(`should return undefined when key is a`, function () {
    // 因为自定义函数没找到键值就返回undefined
    // 然后在should官方文档中建议这种写法判断
    (getting('a') === undefined).should.be.true;
    (getting('a') === 2).should.be.true;
  });
});

This is the document address: should.js

Run Mocha ./ aa . JS gets the following return

clipboard.png

If you have experience in this field, I hope you can give me your advice!

1 Replies
blwqh
commented on Sat, 03 Sep 2022
Repo was moved to own organization. See https://github.com/shouldjs/s...

What is the first sentence of the document?

  (getting('a') === undefined).should.be.true();
lock This question has been locked and the reply function has been disabled.