加载中

测试 Kibana 插件

了解 推荐的测试方法

关于 Kibana 代码库中的现代 UI 和 API 端到端测试,请参阅 Scout

核心服务已经提供了模拟对象,以简化测试并确保插件始终依赖于有效的公共契约

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 }, …);
		

或者,如果您需要获取完整的核心 setupstart 契约

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, ...);
		

虽然这不是强制性的,但我们强烈建议您同时也导出插件的模拟对象,以便依赖插件能够在测试中使用它们。您的插件模拟对象应该从插件根目录下的 /server/public 目录中导出

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: …
}
		

插件模拟应当仅包含公共 API 的模拟:setupstartstop 契约。纯函数不需要模拟,因为其他插件可以在测试中调用原始实现。

© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.