简体   繁体   English

当它被定义为 function 时开玩笑说“不是函数”

[英]Jest saying "Not a function" when it is defined as function

I am writing jest for this simple function:我正在为这个简单的 function 写笑话:

function isVowel(ch) {
  ch = ch.toUpperCase();

  return ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U";
}

this is the jest:这是笑话:

const isVowel = require("./isVowel");
test("a is vowel", () => {
  const ch = isVowel("a");
  expect(ch).toBe("A");
});

on running, it gives error: TypeError: isVowel is not a function运行时出现错误:TypeError: isVowel is not a function

how to resolve it?如何解决?

I assume you write your code in commonjs format due to the way you require it in your test file so you just simply export like this:由于您在测试文件中require它的方式,我假设您以commonjs格式编写代码,因此您只需像这样导出:

// isVowel.js

function isVowel(ch) {
  ch = ch.toUpperCase();

  return ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U";
}

module.exports = isVowel;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM