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

test: Add test fixtures & implement testDatasource for tag #9

Merged
merged 1 commit into from
Jul 27, 2023

Conversation

mure
Copy link
Collaborator

@mure mure commented Jul 26, 2023

Pull Request

🤨 Rationale

Writing unit tests for data sources is a little tricky because requests are made by the BackendSrv instance retrieved from a getBackendSrv() call. This PR adds some fixtures to make test specs cleaner and implements the tag's testDatasource method as an example.

👩‍💻 Implementation

I really wanted to use Mock Service Worker, so that we could mock routes at the network level, but the BackendSrv implementation is apart of Grafana's unpackaged code, so we have to mock it out.

Previously, we were using jest.mock to monkey patch the @grafana/runtime module, but it only applies to the file that called it, so it's difficult to refactor. Instead, we can just inject the BackendSrv in DataSource's constructors. Then jest-mock-extended does a lot of the heavy lifting, letting us create a type-safe mock of the BackendSrv interface. The mock even comes with a calledWith() extension, so we can verify that the correct route is called.

🧪 Testing

This is self-explanatory 😄

✅ Checklist

package.json Show resolved Hide resolved
@mure mure requested review from ggracechoi and iadamjee July 26, 2023 18:09
@mure mure merged commit c02ff40 into main Jul 27, 2023
1 check passed
@mure mure deleted the tag-testdatasource-and-fixtures branch July 27, 2023 15:46
@mure
Copy link
Collaborator Author

mure commented Jul 31, 2023

🎉 This PR is included in version 1.6.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@mure mure added the released label Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants