Skip to content
← Back to rules

jest/no-interpolation-in-snapshots Style

作用

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

为什么这不好?

插值会阻止快照被更新。相反,应使用 属性匹配器 通过 matcher 来重载属性。

示例

以下是此规则的不正确代码示例:

javascript
expect(something).toMatchInlineSnapshot(
  `Object {
    property: ${interpolated}
  }`,
);

expect(something).toMatchInlineSnapshot(
  { other: expect.any(Number) },
  `Object {
    other: Any<Number>,
    property: ${interpolated}
  }`,
);

expect(errorThrowingFunction).toThrowErrorMatchingInlineSnapshot(`${interpolated}`);

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["jest"],
  "rules": {
    "jest/no-interpolation-in-snapshots": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["jest"],
  rules: {
    "jest/no-interpolation-in-snapshots": "error",
  },
});
bash
oxlint --deny jest/no-interpolation-in-snapshots --jest-plugin

版本

此规则于 v0.0.13 中添加。

参考资料