-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Mocks at the service level #31486
Comments
Pinging @elastic/kibana-platform |
I think this is a good start. I don't really have opinions about all of the action points and such, so I'm only highlighting those that I do have thoughts about.
We currently put jest tests as siblings to the service, does it make sense to do the same for mocks rather than a separate directory? Something like:
I do think we should provide sensible return values out of the box though. Tests can change them if necessary, but if I'm invoking a function in my service that happens to check for That said, the minimal functionality you propose here is a big step up from what exists today. I'd be happy to start with that if the return value thing proves to be impractical.
I'm a big fan of factories for test setup. Without them, I've in seen in the Kibana project developers accidentally leak memory and accidentally introduce state into their tests. |
I see the benefit of it, but also I'm afraid of bloated folders. I'm fine with either option, as I don't have strong arguments here, except visual discoverability (which is quite a subjective thing).
I believe we should provide mocks with meaningful defaults. Probably I should start with |
Does it play well with the Jest
Agree, having mocks for services is good, but having mocks for the |
You answered this :)
Agree. There are not so many examples in the repo, so I'd suggest to implement it as a next step when we merge #28344 updated description.
|
I think it's fine to include the upgrade under the umbrella of this issue, but I do recommend doing it as a standalone pull request. It's totally fine (and encouraged) to submit multiple smaller/focused pull requests for a single issue rather than trying to bundle everything together. |
Motivation
With introducing more and more core services we see the problem that writing unit test requires manual mocking of plenty of dependent services.
Ideally, we want that all mocks were kept up to date automatically and any interface change was reflected across codebase immediately.
Proposal:
Caveats
[Update] Fixed in Update jest to v24 #31825, Update Typescript to the latest version #32063
It's not possible to have proper type checking for:jest.MockInstance
methods with current@types/jest
version. Proper support was added in@types/jest v24
[Updated] For now we can use
node scripts/type_check.js
.The current jest setup doesn't report type-checking errors. Requires additional work: Type error reporting kulshekhar/ts-jest#79, How to fail all tests or exit jest when there are TSC errors? kulshekhar/ts-jest#250there is no such thing as a public interface of a service. thus we have to suppress types when passing in another service
https://github.com/elastic/kibana/blob/master/src/core/server/legacy_compat/legacy_service.test.ts#L64-L69
https://github.com/elastic/kibana/blob/master/src/core/server/index.test.ts#L66
Action points
decide if services should declare their dependencies on interface only.Moved to a separate issue Mock ES6 Classes in New Platform #33396@types/jest
version to have type-checking for jest methods (mockImplementation, mockReturnValue etc.) Update jest to v24 #31825typescript
to the latest version (to be compatible with updated@types/jest
version) Update Typescript to the latest version #32063make tests fail if there are type errors in testsnode scripts/type_check.js
is enough for nowOther alternatives to consider
The text was updated successfully, but these errors were encountered: