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

TypeScript integration not working #110

Closed
yordis opened this issue Feb 20, 2019 · 7 comments
Closed

TypeScript integration not working #110

yordis opened this issue Feb 20, 2019 · 7 comments

Comments

@yordis
Copy link

yordis commented Feb 20, 2019

I am not sure what I am doing wrong since I following the documentation

// setupFiles

import { GlobalWithFetchMock } from 'jest-fetch-mock';

const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;

customGlobal.fetch = require('jest-fetch-mock');

customGlobal.fetchMock = customGlobal.fetch;

tests

afterEach(() => {
    fetchMock.resetMocks(); // Cannot find name 'fetchMock'
  });

it keeps saying Cannot find name 'fetchMock' TS2304

@yordis yordis closed this as completed Feb 20, 2019
@vspedr
Copy link

vspedr commented Apr 4, 2019

@yordis I'm running into the same issue, how did you solve this?

@yordis
Copy link
Author

yordis commented Apr 4, 2019

Setup: https://github.com/straw-hat-team/javascript/blob/develop/packages/libraries/fetcher/test/setup-files.ts

import { GlobalWithFetchMock } from 'jest-fetch-mock';

const customGlobal: GlobalWithFetchMock = global as GlobalWithFetchMock;

customGlobal.fetch = require('jest-fetch-mock');

customGlobal.fetchMock = customGlobal.fetch;

Usage:

https://github.com/straw-hat-team/javascript/blob/e428d750c2223c70c362eccd5a31f1022783234c/packages/libraries/fetcher/test/index.test.ts#L15

https://github.com/straw-hat-team/javascript/blob/e428d750c2223c70c362eccd5a31f1022783234c/packages/libraries/fetcher/test/index.test.ts#L15

afterEach(() => {
    fetchMock.resetMocks();
  });

 expect(fetchMock.mock.calls[0][0].method).toEqual('GET');

I think this stop failing for me, I don't remember.

@viT-1
Copy link

viT-1 commented Jan 24, 2020

@yordis These links aren't available =(

@yordis
Copy link
Author

yordis commented Jan 24, 2020

@viT-1 let me fix it, I am so sorry

@viT-1
Copy link

viT-1 commented Jan 27, 2020

just found another setup configuration (#82 (comment)):

in jest setup file:
require('jest-fetch-mock').enableMocks();

in test:

import { FetchMock } from 'jest-fetch-mock';
const fetchMock = fetch as FetchMock;

@yinzara
Copy link
Collaborator

yinzara commented Jan 27, 2020

Your "in test" part isn't actually necessary though it will fix the issue.

As stated in the README, if you have an issue with "fetchMock" not being found, add a "global.d.ts" file at the root of the repository (or next to your tsconfig.json file) with the text import "jest-fetch-mock". That should allow all your tests to reference the "fetchMock" global variable without adding that snippet.

@viT-1
Copy link

viT-1 commented Jan 28, 2020

@yinzara
Quoted from here

"Magically available interfaces" or global types is highly discouraged and should mostly be left to legacy.

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

4 participants