简体   繁体   English

将 Array.prototype.includes 与 Typescript 一起使用时开玩笑测试失败?

[英]Jest test failing when using Array.prototype.includes with Typescript?

I started using Array.prototype.includes in this project:我开始在这个项目中使用 Array.prototype.includes:

https://github.com/fireflysemantics/validator https://github.com/fireflysemantics/validator

It is packaged using the Angular Package Format and the source is in this directory:使用Angular Package格式打包,源码在这个目录下:

https://github.com/fireflysemantics/validator/tree/master/projects/validator https://github.com/fireflysemantics/validator/tree/master/projects/validator

The project builds fine, however when the Jest tests are run this error occurs:该项目构建良好,但是在运行 Jest 测试时会发生此错误:

 FAIL  projects/validator/src/lib/decorators/IsDefined.spec.ts
  ● should create an error when using validate

    TypeError: Cannot read property 'includes' of undefined

      35 |   const mc: MetaClass = ValidationContainer.metaClasses.get(cn);
      36 |   if (mc) {
    > 37 |     const properties: string[] = mc.properties.filter(p => !exlude.includes(p))

So it seems as if Jest is not picking up the property typescript configuration, which does include:所以似乎 Jest 没有选择属性 typescript 配置,其中包括:

    "lib": [
      "dom",
      "es2018"
    ]

Thoughts?想法?

You are misinterpreting the error.您误解了错误。 It's not that includes is not allowed.并不是不允许includes

It's that it cannot access the property includes on a value that is undefined , ie apparently you try to do undefined.includes , so that would mean that exlude must be undefined .这是它无法访问属性includes的值undefined ,即显然您尝试执行undefined.includes ,因此这意味着exlude必须是undefined


In your IsDefined.spec.ts you are calling validate like this:在您的IsDefined.spec.ts中,您调用validate如下:

it(`should create an error when using validate`, ()=>{
  let IDI = new IsDefinedInvalid();
  let e = validate(IDI);
  expect(e.valid).toBeFalsy();
});

So, you are passing only 1 argument ( IDI ) to validate .因此,您只传递了 1 个参数 ( IDI ) 来validate However, in validate.ts you define the function like this:但是,在validate.ts中,您可以像这样定义 function:

export function validate(target: any, exlude?: string[]): ObjectErrors {
  let oes: ObjectErrors = new ObjectErrors();
  const cn: string = target.constructor.name;
  const mc: MetaClass = ValidationContainer.metaClasses.get(cn);
  if (mc) {
    const properties: string[] = mc.properties.filter(p => !exlude.includes(p))
    properties.forEach(p => {
      if (!validateProperty(target, p, oes)) {
        oes.valid = false;
      }
    });
  }
  return oes;
}

As you can see, validate takes two arguments - target and exlude .如您所见, validate需要两个arguments - targetexlude (By the way, that is misspelled and should be exclude with a c .) Yes, exlude as marked as optional with ? (顺便说一句,这是拼写错误,应该用c exclude 。)是的, exlude标记为可选用? , so it's contractually OK that you omit it, however inside the validate function you then use exlude (in the line that crashed) without first checking whether it is undefined , so you end up calling undefined.includes(p) ! ,所以在合同上可以省略它,但是在validate function 中,您然后使用exlude (在崩溃的行中)而不首先检查它是否为undefined ,因此您最终调用undefined.includes(p)

To be honest, I'm not sure why the TypeScript compiler isn't complaining at this point.老实说,我不确定为什么 TypeScript 编译器此时没有抱怨。 (Maybe a commenter has an idea why...) (也许评论者知道为什么......)

Anyway, the solution is to set a default value of [] in the function definition in validate.ts :无论如何,解决方案是在validate.ts中的 function 定义中设置[]的默认值:

export function validate(target: any, exlude: string[] = []): ObjectErrors {

(Of course it would also work to pass the second argument as empty array when calling the function ( validate(IDI, []) ), but then the bug in validate will still exist.) (当然,在调用 function( validate(IDI, []) )时,将第二个参数作为空数组传递也可以,但是validate中的错误仍然存在。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM