Skip to content
← Back to rules

vitest/no-interpolation-in-snapshots Style

作用

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

为什么这不好?

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

示例

以下是此规则的错误代码示例:

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": ["vitest"],
  "rules": {
    "vitest/no-interpolation-in-snapshots": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则于 v0.0.13 中添加。

参考资料