kibana/docs/developer/plugin/testing-kibana-plugin.asciidoc
Mikhail Shustov ab8a2f7427
[docs] Convert migration guide to asciidoc (#82600)
* Initial conversion to asciidoc

* Update and split migration guide

* Convert MIGRATION_EXAMPLES to asciidoc

* build with --focus flag

* convert migration guide to asciidoc

* cleanup migration_examples

* fix wrong Heading size

* update links in docs

* Apply suggestions from code review

Co-authored-by: Rudolf Meijering <skaapgif@gmail.com>

* Apply suggestions from code review

Co-authored-by: Rudolf Meijering <skaapgif@gmail.com>

* add tooling section

* explain purpose of each lifecycle method

* cleanup docs

* cleanup p2

* fix wrong link

* resturcture core docs

* fix wrong link

* update missing links

* Apply suggestions from code review

Co-authored-by: gchaps <33642766+gchaps@users.noreply.github.com>

* address comments

* add a commenta about plugin-helpers preconfigured

* improve density of tables

* fix lik

* remove links to the migration guide

* address comments

* Apply suggestions from code review

Co-authored-by: gchaps <33642766+gchaps@users.noreply.github.com>

* address @gchaps comments

* Apply suggestions from code review

Co-authored-by: gchaps <33642766+gchaps@users.noreply.github.com>

* change format of ES client change list

Co-authored-by: Josh Dover <me@joshdover.com>
Co-authored-by: Rudolf Meijering <skaapgif@gmail.com>
Co-authored-by: gchaps <33642766+gchaps@users.noreply.github.com>
2020-11-24 09:46:19 +01:00

63 lines
1.8 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[[testing-kibana-plugin]]
== Testing {kib} Plugins
=== Writing tests
Learn about <<development-tests, recommended testing approaches >>.
=== Mock {kib} Core services in tests
Core services already provide mocks to simplify testing and make sure
plugins always rely on valid public contracts:
*my_plugin/server/plugin.test.ts*
[source,typescript]
----
import { configServiceMock } from 'kibana/server/mocks';
const configService = configServiceMock.create();
configService.atPath.mockReturnValue(config$);
const plugin = new MyPlugin({ configService }, …);
----
Or if you need to get the whole core `setup` or `start` contracts:
*my_plugin/server/plugin.test.ts*
[source,typescript]
----
import { coreMock } from 'kibana/public/mocks';
const coreSetup = coreMock.createSetup();
coreSetup.uiSettings.get.mockImplementation((key: string) => {
});
const plugin = new MyPlugin(coreSetup, ...);
----
=== Writing mocks for your plugin
Although it isnt mandatory, we strongly recommended you export your
plugin mocks as well, in order for dependent plugins to use them in
tests. Your plugin mocks should be exported from the root `/server` and
`/public` directories in your plugin:
*my_plugin/(server|public)/mocks.ts*
[source,typescript]
----
const createSetupContractMock = () => {
const startContract: jest.Mocked<MyPluginStartContract>= {
isValid: jest.fn(),
}
// here we already type check as TS infers to the correct type declared above
startContract.isValid.mockReturnValue(true);
return startContract;
}
export const myPluginMocks = {
createSetup: createSetupContractMock,
createStart: …
}
----
Plugin mocks should consist of mocks for _public APIs only_:
`setup`, `start` & `stop` contracts. Mocks arent necessary for pure functions as
other plugins can call the original implementation in tests.