In my case, I had to mock a Node.js module. I'm using React and Redux in ES6, with Jest and Enzyme for unit tests.
In the file I'm using, and writing a test for, I'm importing the node modules as default:
import nodeModulePackage from 'nodeModulePackage';
So I needed to mock it as a default since I kept getting the error (0, _blah.default) is not a function..
My solution was to do:
jest.mock('nodeModulePackage', () => jest.fn(() => {}));
In my case, I just needed to override the function and make it return an empty object.
If you need to call a function on that node module, you'll do the following:
jest.mock('nodeModulePackage', () => ({ doSomething: jest.fn(() => 'foo') }));
Answer from jenkizenki on Stack OverflowIn my case, I had to mock a Node.js module. I'm using React and Redux in ES6, with Jest and Enzyme for unit tests.
In the file I'm using, and writing a test for, I'm importing the node modules as default:
import nodeModulePackage from 'nodeModulePackage';
So I needed to mock it as a default since I kept getting the error (0, _blah.default) is not a function..
My solution was to do:
jest.mock('nodeModulePackage', () => jest.fn(() => {}));
In my case, I just needed to override the function and make it return an empty object.
If you need to call a function on that node module, you'll do the following:
jest.mock('nodeModulePackage', () => ({ doSomething: jest.fn(() => 'foo') }));
I figured this out. It is to do with hoisting, see: Jest mocking reference error
The reason it had worked in a previous test, where I had done it, was because the testSubject was itself a class. This meant that when the testSubject was instantiated, it was after the variable declaration in the test file, so the mock had access to use it.
So in the above case it was never going to work.
How to fix TypeError is not a function (testing promises with Jest)
Writing tests: type error not a function
React Jest - TypeError: (0 , _[....]) is not a function
Unit testing with jest in React Native project ("Not a function" error)
// sum.js
export const sum = (a,b) => a+b
// sum.test.js
import sum from './sum';
it('sums numbers', () => {
expect(sum(1, 2)).toEqual(3);
expect(sum(2, 2)).toEqual(4);
});I'm new to writing unit tests. I'm trying to write tests for my reducers and actions. However I can't seem to test a simple function that adds two numbers. I keep getting,
TypeError: (0 , _sum2.default) is not a function
What should I do??? Thanks.
Your import statements are off a bit. Try this instead:
import { sum, subtract } from '../src/utils/FakeUtils.js';
Since you aren't exporting with the default keyword, you need to place your imports in curly braces. You can only have one default export, so this approach makes sense.
I happens because you are exporting your functions incorrectly in relation to import. You should export it this way
const function sum(a, b) {
return a + b;
}
const function subtract(a, b) {
return a - b;
}
export { sum, subtract}
or import it this way
import {sum, subtract } from '../src/utils/FakeUtils.js';
restart metro bundler with yarn start or npm start solved my problem
I'm guessing your import in your test:
jest.autoMockOff();
import React, { View } from 'react-native';
might not actually be importing your custom react-native.js
Did you want to use a relative ./react-native import instead?
The jest object is automatically in scope within every test file, so there's no need to import it explicitly. If you do want to import the jest object directly, you want to import the jest-mock module, not the jest-cli module, via:
// Not necessary inside a Jest test file
import jest from 'jest-mock';
const mock = jest.fn();
It is a bit weird that documentation isn't mentioning that jest is not require('jest'); but require('jest-mock'), the following code should work on v22:
const jest = require('jest-mock');
const spy = jest.fn();