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.
Why do I keep getting type error, "x" is not a function when using jest with create-react-app?
You need to do 'export default' in the sum function if you want to import the function without unpacking. Otherwise, you need to unpack when importing like 'import {sum} from...'
More on reddit.comWriting tests: type error not a function
Getting TypeError: foo_1.default is not a function
lightning web components - LWC JEST: TypeError: expect(...).toBeAccessible is not a function - Salesforce Stack Exchange
// 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.
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();
Remove this line:
Copyimport { test, expect } from 'jest'
You don't need to import anything from Jest. See example.
I've got it running with flow using
Copyimport expect from "expect";
const test = window.test;
Maybe it helps you, too. It seems to make flow treat them both as any, which is far from perfect, but at least the other parts of the file can be checked.
A slightly better "solution" is to make an own file my-test.test.js containing
Copyexport const test = window.test;
export const expect = window.expect;
test("dummy", () => {});
and use it like
Copyimport {test, expect} from '../my/my-test.test';
This concentrates the ugliness in a single file.
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?