question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct way to cleanup mock

See original GitHub issue

I had side effects between tests because of the mocks. I had 2 files on the following model:

import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'

const mock = new MockAdapter(axios)

describe('eventsActions', () => {

  afterEach(() => {
    mock.reset()
  })

  describe('changePreferences', () => {
    it('should call /api/users/preferences and dispatch events', (done) => {
      mock.onPost('/api/users/preferences')
        .reply(200)
...

When run alone, each file end with success but when run together, the calls to one url are 404. I suspected a side effect due to poor cleaning and I looked at the tests on this repo. My tests now works by removing the afterEach and adding

beforeEach(() => {
    mock = new MockAdapter(axios)
  })

So, problem solved, but I am curious. Is this the intended use ? When am I supposed to use reset() and restore() ?

Thank you

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

12reactions
ctimmermcommented, Feb 16, 2017

You’re creating the mock in the describe function, which is executed before any of the tests are run. You need to create it in before instead.

import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import chai from 'chai'

const expect = chai.expect

describe('test file 1', () => {
  let mock

  before(() => {
    mock = new MockAdapter(axios)
  })

  afterEach(() => {
    mock.reset()
  })

  after(() => {
    mock.restore()
  })

  it('test case 1', (done) => {
    mock.onGet('/api/events').reply(200, [{ id: 1 }])
    axios.get('/api/events')
      .then((response) => {
        expect(response.data).to.eql([{ id: 1 }])
        done()
      })
      .catch((err) => {
        done(err)
      })
  })

  it('test case 2', (done) => {
    mock.onGet('/api/events').reply(200, [{ id: 2 }])
    axios.get('/api/events')
      .then((response) => {
        expect(response.data).to.eql([{ id: 2 }])
        done()
      })
      .catch((err) => {
        done(err)
      })
  })
})
9reactions
kevinchcommented, Feb 19, 2018

FYI, with Jest i had it working like this:

let mock
let store

beforeEach(() => {
  mock = new MockAdapter(axios)
  store = mockStore({})
})

afterEach(() => {
  mock.reset()
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to clean up mocks in spring tests when using Mockito
I solved it by adding reset() in @Before method. My question is what is the best practice to handle this situation (The javadoc...
Read more >
Clean Unit Tests with Mockito - Reflectoring
In this article we will learn how to mock objects with Mockito. ... a look at some best practices to keep our tests...
Read more >
Mockito - Resetting Mock - Tutorialspoint
Here we've reset mock object. MathApplication makes use of calcService and after reset the mock, using mocked method will fail the test.
Read more >
Jest set, clear and reset mock/spy/stub implementation
Another way to do it is to clearAllMocks , this will mean that between tests, the stubs/mocks/spies are cleared, no matter which mock...
Read more >
cleaning up jest mocks - Brad Garropy
In jest, there are three different ways to clean up a mock function. ... Each different method cleans up a different subset of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found