Skip to content

Make unmocked requests fail in Jest again

Martin Hanzel requested to merge mh/jest-fail-unmocked-requests into master

What does this MR do?

EE port: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/15400

Normally, any unmocked HTTP request via axios or jquery fails a test. However, since gitlab-ce/!29272 (merged), where manual mocks were introduced for ~/lib/utils/axios_utils and jquery, unmocked requests simply raise an error, which when caught don't fail the test and pass silently.

This MR makes unmocked requests explicitly call Jasmine's fail() global so that tests fail unconditionally.

Why is this regression annoying?

Because our style linter enforces .catch() blocks on any Promise. That means that any axios request with a .then() must also have a .catch(), which catches the unmocked request error.

Why global.fail()?

  • It's observable and mockable using jest.spyOn(global, 'fail').
  • There's a single instance of it across all tests.
  • It can be used outside the scope of it() and {before,after}{Each,All}().

Does this MR meet the acceptance criteria?

Conformity

Performance and Testing

Edited by Martin Hanzel

Merge request reports