kibana/docs/extend/testing-kibana-plugin.md
Colleen McGinnis 1814c60017
[docs] Migrate docs from AsciiDoc to Markdown (#212558)
Migrate docs from AsciiDoc to Markdown. The preview can be built after
#212557 is merged.

@florent-leborgne please tag reviewers, add the appropriate label(s),
and take this out of draft when you're ready.

Note: More files are deleted than added here because the content from
some files was moved to
[elastic/docs-content](https://github.com/elastic/docs-content).

**What has moved to
[elastic/docs-content](https://github.com/elastic/docs-content)?**

Public-facing narrative and conceptual docs have moved. Most can now be
found under the following directories in the new docs:
- explore-analyze: Discover, Dashboards, Visualizations, Reporting,
Alerting, dev tools...
- deploy-manage: Stack management (Spaces, user management, remote
clusters...)
- troubleshooting: .... troubleshooting pages

**What is staying in the Kibana repo?**

- Reference content (= anything that is or could be auto-generated):
Settings, syntax references
- Release notes
- Developer guide

---------

Co-authored-by: Florent Le Borgne <florent.leborgne@elastic.co>
2025-03-04 14:56:07 +01:00

2 KiB
Raw Blame History

mapped_pages
https://www.elastic.co/guide/en/kibana/current/testing-kibana-plugin.html

Testing Kibana Plugins [testing-kibana-plugin]

Writing tests [_writing_tests]

Learn about recommended testing approaches .

Mock {{kib}} Core services in tests [_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

import { configServiceMock } from '@kbn/core/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

import { coreMock } from '@kbn/core/public/mocks';

const coreSetup = coreMock.createSetup();
coreSetup.uiSettings.get.mockImplementation((key: string) => {
  
});

const plugin = new MyPlugin(coreSetup, ...);

Writing mocks for your plugin [_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

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.