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

[docs] fetchMock -> fetchMocker #5

Merged
merged 1 commit into from Feb 3, 2023
Merged

Conversation

Maxim-Mazurok
Copy link
Contributor

@Maxim-Mazurok Maxim-Mazurok commented Jan 20, 2023

I was reviewing code at work and I saw this:

beforeEach(() => {
  const fetchMock = createFetchMock(vi);
  fetchMock.enableMocks();
  fetchMock.mockIf(`/bla`);
});

it("Bla", () => {
  bla();
  expect(fetchMock.mock.calls.length).toEqual(1);
});

I was confused, why we have const fetchMock in beforeEach but then reference fetchMock in the test, it should be undefined there, outside of the scope. Turns out it was because fetchMock is also a global variable. Looking at type definitions, I see that createFetchMock is also called createMocker. So I think it'll make more sense to do this:

beforeEach(() => {
  const fetchMocker = createFetchMock(vi);
  fetchMocker.enableMocks();
  fetchMocker.mockIf(`/bla`);
});

it("Bla", () => {
  bla();
  expect(fetchMock.mock.calls.length).toEqual(1);
});

This way there's no clash and it makes more sense - you create fetchMocker, then fetchMocker.enableMocks() will define global fetchMock for you. Hope this makes sense!

I was reviewing code at work and I saw this:
```ts
beforeEach(() => {
  const fetchMock = createFetchMock(vi);
  fetchMock.enableMocks();
  fetchMock.mockIf(`/bla`);
});

it("Bla", () => {
  bla();
  expect(fetchMock.mock.calls.length).toEqual(1);
});
```

I was confused, why we have `const fetchMock` in `beforeEach` but then reference `fetchMock` in the test, it should be undefined there, out of the scope.
Turns out it was because `fetchMock` is also a global variable.
Looking at type definitions, I see that `createFetchMock` is also called `createMocker`. So I think it'll make more sense to do this:

```ts
beforeEach(() => {
  const fetchMocker = createFetchMock(vi);
  fetchMocker.enableMocks();
  fetchMocker.mockIf(`/bla`);
});

it("Bla", () => {
  bla();
  expect(fetchMock.mock.calls.length).toEqual(1);
});
```

This way there's no clash and it makes more sense - you create `fetchMocker`, then `fetchMocker.enableMocks()` will define global `fetchMock` for you.
Hope this makes sense!
@Maxim-Mazurok Maxim-Mazurok changed the title fetchMock -> fetchMocker [docs] fetchMock -> fetchMocker Jan 20, 2023
Copy link
Owner

@IanVS IanVS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, thanks for taking the time to create a PR.

@IanVS IanVS merged commit b79ac92 into IanVS:main Feb 3, 2023
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

Successfully merging this pull request may close these issues.

None yet

2 participants