Home > Net >  Jest saying "Not a function" when it is defined as function
Jest saying "Not a function" when it is defined as function

Time:09-26

I am writing jest for this simple 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

how to resolve it?

CodePudding user response:

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:

// isVowel.js

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

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

module.exports = isVowel;
  • Related