Skip to content

Mockify jquery and axios packages

Martin Hanzel requested to merge mh/net-mocks into master

What does this MR do?

Makes manual mocks out of the jquery and ~/lib/utils/axios_helper packages. These mocks will throw Errors on unmocked HTTP requests.

Why?

Unit tests shouldn't make net requests. There was code in a global before_each that mocked Axios so that all attempts at a request result in an Error. I feel like this is a task more suited for a mock.

Advantages

  • Unbloats test_setup.js.
  • Manual mocks are more pluggable if we want to change mock implementations down the road.
  • Manual mocks stay in place if beforeEach isn't called for some reason.
  • Manual mocks stay in place if Jest's module cache is cleared within a test (this is an edge case).
  • Mock implementations live beside their source file.
  • Any file imported in a test setup script is unmockable without jest.resetModules(), so it's better to reduce dependencies of the setup script.

Drawbacks

  • Manual mocks are scattered throughout the codebase, which is arguably less maintainable.
  • There's yet another directory, __mocks__, at the top-level (this can be fixed but I'm not sure what the side-effects are).

Is this a pattern we should adopt going forward?

Related: #60136 (closed)

@winh @ealcantara

Edited by Martin Hanzel

Merge request reports