Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jestjs 为什么错误字符串中有一些正斜杠?_Jestjs - Fatal编程技术网

Jestjs 为什么错误字符串中有一些正斜杠?

Jestjs 为什么错误字符串中有一些正斜杠?,jestjs,Jestjs,这是我测试后的照片 expect(received).toBe(expected) // Object.is equality Expected: "child 'path1' fails because ['path1' is not allowed to be empty]" Received: "child \"path1\" fails because [\"path1\" is not allowed to be empty]" 39 | } c

这是我测试后的照片

expect(received).toBe(expected) // Object.is equality

    Expected: "child 'path1' fails because ['path1' is not allowed to be empty]"
    Received: "child \"path1\" fails because [\"path1\" is not allowed to be empty]"

      39 |     } catch (error) {
      40 |       expect(error.name).toBe('ValidationError');
    > 41 |       expect(error.message).toBe("child 'path1' fails because ['path1' is not allowed to be empty]");
         |                             ^
      42 |     }
      43 |   });
      44 | });

      at Object.<anonymous> (src/__tests__/models/adChannel/googleadwords/AdGroupAd.spec.ts:41:29)
如您所见,接收的值具有正斜杠\。它与预期值不匹配

我想也许绳子逃走了?我希望字符串没有\

简短的答案

将您的期望更改为:

expecterror.message.toBe'child path1失败,因为[path1不允许为空]; …它会起作用的

细节

JavaScript允许使用单引号:“字符串”或双引号:字符串来定义字符串

从:

与其他一些语言不同,JavaScript不区分单引号字符串和双引号字符串

…所以你使用哪种方法都不重要

单引号在使用双引号定义的字符串中工作正常:

const singleQuotes=包含“单引号”; …对于使用单引号定义的字符串中的双引号也是如此:

constdoublequotes='中有双引号'; …但如果单引号位于用单引号定义的字符串中,则需要对其进行转义:

const singleQuotes='中有'single quotes'; …对于使用双引号定义的字符串中的双引号也是如此:

const doubleQuotes=包含\双引号\项; 您在Jest输出中看到转义字符,因为Jest使用双引号格式化输出字符串,因此需要转义其中的双引号