Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configurate your library for typescript tests? (ts7016) #6

Closed
viT-1 opened this issue Jan 24, 2020 · 3 comments
Closed

How to configurate your library for typescript tests? (ts7016) #6

viT-1 opened this issue Jan 24, 2020 · 3 comments

Comments

@viT-1
Copy link

viT-1 commented Jan 24, 2020

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?

@viT-1
Copy link
Author

viT-1 commented Jan 24, 2020

@viT-1
Copy link
Author

viT-1 commented Jan 24, 2020

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)

@wheresrhys
Copy link
Owner

Please try v1.2.0 of this library and reopen the issue if it's still broken

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants