Skip to content
← Back to rules

jest/no-interpolation-in-snapshots 风格

它的作用

防止在快照中使用字符串插值。

为什么这是个问题?

插值会阻止快照被更新。相反,应通过使用属性匹配器为属性添加匹配器。

示例

此规则的错误代码示例:

javascript
expect(something).toMatchInlineSnapshot(
  `对象 {
    属性: ${插值}
  }`,
);

expect(something).toMatchInlineSnapshot(
  { 其他: expect.any(数字) },
  `对象 {
    其他: Any<数字>,
    属性: ${插值}
  }`,
);

expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot(`${插值}`);

该规则与 eslint-plugin-vitest 兼容。要使用它,请将以下配置添加到您的 .oxlintrc.json 文件中:

json
{
  "rules": {
    "vitest/no-interpolation-in-snapshots": "error"
  }
}

如何使用

要通过配置文件或 CLI 启用此规则,可以使用:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-interpolation-in-snapshots": "error"
  }
}
bash
oxlint --deny jest/no-interpolation-in-snapshots --jest-plugin

参考资料