如何测试摩卡中的替换方法?

How do I test replace method in mocha?

我是 mocha 的新手,无法测试以下功能。我有以下字符串 replace_underscore_with_hyphen。我正在使用以下功能将其替换为 replace-underscore-with-hyphen

const type = "replace_underscore_with_hyphen";
     type = type.replace(/_/ig, '-');

但请问我如何在 mocha 中测试此功能。

您可以测试最终字符串是否包含连字符,而不是下划线:

const replaceUnderscores = () => {
  const type = "replace_underscore_with_hyphen";
  return type.replace(/_/ig, '-');
}

it('should replace underscores with hyphen', () => {
  const replaced = replaceUnderscores();
  expect(replaced).not.toContain('_');
  expect(replaced).toContain('-');
});