So I'm using Jest, Typescript and Create-react-app
I have this test:
import { defaultHeaders } from './http';
describe('http tests', () => {
test('defaultHeaders()', () => {
const headers = defaultHeaders(undefined);
expect(headers).toEqual({ foo: 2 });
});
});
The code is in same subdir in file http.ts
and looks like this:
export function defaultHeaders(headers?: FetchHeaders): FetchHeaders {
return { 'Content-Type': 'application/json' };
}
When I run my jest tests it throws:
TypeError: (0 , _http.defaultHeaders) is not a function
The weird part is that all other tests that are wrapped in a default function or const do work.
Is there any way to test non default exported functions with Jest?
update:
- also tried converting defaultHeaders into an arrow function and that didn't help.
- and tried changing import as:
import * as http from './http'
that still makes it throw same error
defaultHeaders
export in the same file. A common case for an export to appear undefined is circular dependency but the code you posted doesn't suggest that. Please, provide stackoverflow.com/help/mcve that can reproduce the problem. – Cypherimport * as http from './http'
andimport http from './http'
, it's only possible on your side. Also tryconst { defaultHeaders } = require('./http')
andconst { defaultHeaders } = require('./http').default
. These attempts cover possibilities with incorrect TS module interoperation. – Cypher