How to configurate your library for typescript tests? (ts7016)
Closed this issue · 3 comments
I am using ts-jest & want to mock fetch responses (for browser code).
First of all, after installing fetch-mock-jest, in jest setup file I should write
global.fetchMock = require('fetch-mock-jest');
is it right or it should be global.fetch = require('fetch-mock-jest').sandbox();
?
In ts tests I can't write global.fetchMock or fetchMock without global declaring
or default importing import fetchMock from 'fetch-mock-jest';
But if I am doing such things, why I need to make changes in jest setup script?
Moreover, If I import fetchMock in test, eslint show me suggestion:
Could not find a declaration file for module 'fetch-mock-jest'. ... implicitly has an 'any' type
Try `npm install @types/fetch-mock-jest` if it exists or add a new declaration (.d.ts) file containing `declare module 'fetch-mock-jest'; (ts7016)
Can you provide d.ts file for using typed fetchMock methods?
somemodule.spec.ts
import fetchMock from 'fetch-mock-jest';
fetchMock.mock({
url: 'begin:/activity/findpoints', //relative url
response: Promise.resolve([
{ label: 'foo' },
{ label: 'bar' },
{ label: 'baz' },
]),
});
and typings/fetch-mock-jest.ts
declare module 'fetch-mock-jest';
I have an error:
Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
...
node_modules\fetch-mock\esm\client.mjs:3552
export default client;
^^^^
SyntaxError: Unexpected token export
at ScriptTransformer._transformAndBuildScript (../node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
at ScriptTransformer.transform (../node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
at Object.<anonymous> (../node_modules/fetch-mock-jest/fetch-mock-jest.js:4:19)
Please try v1.2.0 of this library and reopen the issue if it's still broken