开玩笑:测试类型或null [英] Jest : Testing for type or null

查看:53
本文介绍了开玩笑:测试类型或null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试,我想测试我接收到的对象值类型是否与模式匹配.可能是对于某些键,我可能会收到一些东西或为空

I have a test where i want to test if my received object value types match a schema. Prob is that for some keys i may receive something or null

到目前为止,我已经尝试过

I tried this so far

  const attendeeSchema = {
  birthDate: expect.extend(toBeTypeOrNull("Date")),
  contact: expect.extend(toBeTypeOrNull(String)),
  createdAt: expect.any(Date),
  firstName: expect.any(String),
  id: expect.any(Number),
  idDevice: expect.extend(toBeTypeOrNull(Number)),
  information: expect.extend(toBeTypeOrNull(String)),
  lastName: expect.any(String),
  macAddress: expect.extend(toBeTypeOrNull(String)),
  updatedAt: expect.any(Date),
  // state: toBeTypeOrNull()
};

    const toBeTypeOrNull = (received, argument) => {
  const pass = expect(received).toEqual(expect.any(argument));
  if (pass || received === null) {
    return {
      message: () => `Ok`,
      pass: true
    };
  } else {
    return {
      message: () => `expected ${received} to be ${argument} type or null`,
      pass: false
    };
  }
};

在我的测试中

 expect(res.result.data).toBe(attendeeSchema);

我还尝试了bebeE​​qual和其他东西....

i also tried tobeEqual and other stuff....

我的测试成绩通过

TypeError: any() expects to be passed a constructor function. Please pass one or use anything() to match any object.

我不知道该怎么办.. 如果有人有主意 谢谢

I have no idea what to do here.. If someone has an idea Thanks

推荐答案

所有先前给出的响应在其实现中均不正确地使用expect(),因此它们实际上无法正常工作.

All the previously given responses improperly use expect() in their implementation, so they don't really work.

您想要的是一个玩笑匹配器,其作用类似于any(),但是接受空值,并且在其实现中不使用expect()函数.您可以通过基本上复制原始的any()实现(从茉莉花),但开始时添加了空测试:

What you want is a jest matcher that works like any() but accepts null values and doesn't use expect() functions in its implementation. You can implement this as an extension by basically copying the original any() implementation (from Jasmine), but with a null test added to the start:

expect.extend({
  nullOrAny(received, expected) {
    if (received === null) {
      return {
        pass: true,
        message: () => `expected null or instance of ${this.utils.printExpected(expected) }, but received ${ this.utils.printReceived(received) }`
      };
    }

    if (expected == String) {
      return {
        pass: typeof received == 'string' || received instanceof String,
        message: () => `expected null or instance of ${this.utils.printExpected(expected) }, but received ${ this.utils.printReceived(received) }`
      };        
    }

    if (expected == Number) {
      return {
        pass: typeof received == 'number' || received instanceof Number,
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Function) {
      return {
        pass: typeof received == 'function' || received instanceof Function,
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Object) {
      return {
        pass: received !== null && typeof received == 'object',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    if (expected == Boolean) {
      return {
        pass: typeof received == 'boolean',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }

    /* jshint -W122 */
    /* global Symbol */
    if (typeof Symbol != 'undefined' && this.expectedObject == Symbol) {
      return {
        pass: typeof received == 'symbol',
        message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
      };
    }
    /* jshint +W122 */

    return {
      pass: received instanceof expected,
      message: () => `expected null or instance of ${this.utils.printExpected(expected)}, but received ${this.utils.printReceived(received)}`
    };
  }
});

将以上内容放在.js文件中,然后用笑话指向该文件 setupFilesAfterEnv 配置变量.现在,您可以运行测试,例如:

throw the above in a .js file, then point to that file with the jest setupFilesAfterEnv configuration variable. Now you can run your tests like:

const schema = {
  person: expect.nullOrAny(Person),
  age: expect.nullOrAny(Number)
};

expect(object).toEqual(schema);

这篇关于开玩笑:测试类型或null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆