-1

I am trying to test few test cases for a function where it takes two parameters. these two parameters along with the result I have defined it in three different files and then exporting it. I am using Jest for the same, but it is throwing "TypeError: (0 , _index.default) is not a function" error. Can some one tell where I am going wrong. Testing this in the sandbox

test file:

import appendToFilter from "./index";
import { res1, res2, res3, res4, res5, res6, res7, res8 } from "./Results";
import { src1, src2, src3, src4, src5, src6, src7, src8 } from "./Source";
import { ip1, ip2, ip3, ip4, ip5, ip6, ip7, ip8 } from "./Input";

test("case1", () => {
  expect(appendToFilter(src1, ip1)).toBe(res1);
});

index.js

export function appendToFilter(filter, inputObjects) {
  // logic here
}

Link: https://codesandbox.io/s/optimistic-mirzakhani-pogpw-so-b47y8

skyboyer
  • 22,209
  • 7
  • 57
  • 64
joy08
  • 9,004
  • 8
  • 38
  • 73

2 Answers2

1

It's because you don't have any default export and you are importing appendToFilter from index without named import.

use

import { appendToFilter } from "./index";

instead of

import appendToFilter from "./index";
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
  • the way you are doing it seems correct only i am not sure about what is logic in your function. generally you pass argument to your function expect your return value as expected based on your logic. – Amit Chauhan Jan 23 '20 at 07:41
  • maybe you should create another question about how to test your function. – Amit Chauhan Jan 23 '20 at 07:41
  • 1
    I think you are doing it right from what I see in your test file. What you need to keep an eye on is the assertions - toBe doesn't always work, you need to try other assertions like toEqual for some cases especially arrays and objects. Checkout the Jest documentation for more assertions you can try out – jackthedev Jan 23 '20 at 07:44
1

You are importing the function as a default import but you exported it as a named export. Go to your index.js and

export default appendToFilter

or import the function as the named import it is by doing:

import { appendToFilter } from "./index";

instead of:

import appendToFilter from "./index";
jackthedev
  • 578
  • 5
  • 7