Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@
"@kbn/json-ast": "link:packages/kbn-json-ast",
"@kbn/kbn-genai-cli": "link:x-pack/solutions/observability/packages/kbn-genai-cli",
"@kbn/kibana-manifest-schema": "link:packages/kbn-kibana-manifest-schema",
"@kbn/lazy-object": "link:src/platform/packages/shared/kbn-lazy-object",
"@kbn/lint-packages-cli": "link:packages/kbn-lint-packages-cli",
"@kbn/lint-ts-projects-cli": "link:packages/kbn-lint-ts-projects-cli",
"@kbn/managed-vscode-config": "link:packages/kbn-managed-vscode-config",
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-babel-preset/common_preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = (api) => ({
// our explicit plugin configs to a sub-preset
{
plugins: [
require.resolve('@kbn/lazy-object/src/plugin/lazy_babel_plugin'),
require.resolve('babel-plugin-add-module-exports'),

// The class properties proposal was merged with the private fields proposal
// into the "class fields" proposal. Babel doesn't support this combined
// proposal yet, which includes private field, so this transform is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import type { AnalyticsClient } from '@elastic/ebt/client';
import { Subject } from 'rxjs';
import { lazyObject } from '@kbn/lazy-object';

export const analyticsClientMock: jest.Mocked<AnalyticsClient> = {
export const analyticsClientMock: jest.Mocked<AnalyticsClient> = lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
Expand All @@ -20,7 +21,7 @@ export const analyticsClientMock: jest.Mocked<AnalyticsClient> = {
telemetryCounter$: new Subject(),
flush: jest.fn(),
shutdown: jest.fn(),
};
});

jest.doMock('@elastic/ebt/client', () => ({
createAnalytics: () => analyticsClientMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"@kbn/core-injected-metadata-browser-internal",
"@kbn/core-analytics-browser",
"@kbn/core-base-browser-mocks",
"@kbn/core-injected-metadata-browser-mocks"
"@kbn/core-injected-metadata-browser-mocks",
"@kbn/lazy-object"
],
"exclude": ["target/**/*"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,36 @@ import { Subject } from 'rxjs';
import type { PublicMethodsOf } from '@kbn/utility-types';
import type { AnalyticsServiceSetup, AnalyticsServiceStart } from '@kbn/core-analytics-browser';
import type { AnalyticsService } from '@kbn/core-analytics-browser-internal';
import { lazyObject } from '@kbn/lazy-object';

type AnalyticsServiceContract = PublicMethodsOf<AnalyticsService>;

const createAnalyticsServiceSetup = (): jest.Mocked<AnalyticsServiceSetup> => {
return {
return lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
registerContextProvider: jest.fn(),
removeContextProvider: jest.fn(),
registerShipper: jest.fn(),
telemetryCounter$: new Subject(),
};
});
};

const createAnalyticsServiceStart = (): jest.Mocked<AnalyticsServiceStart> => {
return {
return lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
telemetryCounter$: new Subject(),
};
});
};

const createAnalyticsServiceMock = (): jest.Mocked<AnalyticsServiceContract> => {
return {
return lazyObject({
setup: jest.fn().mockImplementation(createAnalyticsServiceSetup),
start: jest.fn().mockImplementation(createAnalyticsServiceStart),
stop: jest.fn(),
};
});
};

export const analyticsServiceMock = {
Expand Down
3 changes: 2 additions & 1 deletion src/core/packages/analytics/browser-mocks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"kbn_references": [
"@kbn/utility-types",
"@kbn/core-analytics-browser",
"@kbn/core-analytics-browser-internal"
"@kbn/core-analytics-browser-internal",
"@kbn/lazy-object"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import type { AnalyticsClient } from '@elastic/ebt/client';
import { Subject } from 'rxjs';
import { lazyObject } from '@kbn/lazy-object';

export const analyticsClientMock: jest.Mocked<AnalyticsClient> = {
export const analyticsClientMock: jest.Mocked<AnalyticsClient> = lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
Expand All @@ -20,7 +21,7 @@ export const analyticsClientMock: jest.Mocked<AnalyticsClient> = {
telemetryCounter$: new Subject(),
shutdown: jest.fn(),
flush: jest.fn(),
};
});

jest.doMock('@elastic/ebt/client', () => ({
createAnalytics: () => analyticsClientMock,
Expand Down
3 changes: 2 additions & 1 deletion src/core/packages/analytics/server-internal/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"@kbn/core-base-server-internal",
"@kbn/core-analytics-server",
"@kbn/config-mocks",
"@kbn/core-base-server-mocks"
"@kbn/core-base-server-mocks",
"@kbn/lazy-object"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,48 +15,49 @@ import type {
AnalyticsServicePreboot,
} from '@kbn/core-analytics-server';
import type { AnalyticsService } from '@kbn/core-analytics-server-internal';
import { lazyObject } from '@kbn/lazy-object';

type AnalyticsServiceContract = PublicMethodsOf<AnalyticsService>;

const createAnalyticsServicePreboot = (): jest.Mocked<AnalyticsServicePreboot> => {
return {
return lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
registerContextProvider: jest.fn(),
removeContextProvider: jest.fn(),
registerShipper: jest.fn(),
telemetryCounter$: new Subject(),
};
});
};

const createAnalyticsServiceSetup = (): jest.Mocked<AnalyticsServiceSetup> => {
return {
return lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
registerEventType: jest.fn(),
registerContextProvider: jest.fn(),
removeContextProvider: jest.fn(),
registerShipper: jest.fn(),
telemetryCounter$: new Subject(),
};
});
};

const createAnalyticsServiceStart = (): jest.Mocked<AnalyticsServiceStart> => {
return {
return lazyObject({
optIn: jest.fn(),
reportEvent: jest.fn(),
telemetryCounter$: new Subject(),
};
});
};

const createAnalyticsServiceMock = (): jest.Mocked<AnalyticsServiceContract> => {
return {
return lazyObject({
preboot: jest.fn().mockImplementation(createAnalyticsServicePreboot),
setup: jest.fn().mockImplementation(createAnalyticsServiceSetup),
start: jest.fn().mockImplementation(createAnalyticsServiceStart),
stop: jest.fn(),
};
});
};

export const analyticsServiceMock = {
Expand Down
3 changes: 2 additions & 1 deletion src/core/packages/analytics/server-mocks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"kbn_references": [
"@kbn/utility-types",
"@kbn/core-analytics-server",
"@kbn/core-analytics-server-internal"
"@kbn/core-analytics-server-internal",
"@kbn/lazy-object"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,27 @@ import type {
InternalApplicationStart,
InternalApplicationSetup,
} from '@kbn/core-application-browser-internal';
import { lazyObject } from '@kbn/lazy-object';

type ApplicationServiceContract = PublicMethodsOf<ApplicationService>;

const createSetupContractMock = (): jest.Mocked<ApplicationSetup> => ({
register: jest.fn(),
registerAppUpdater: jest.fn(),
});
const createSetupContractMock = (): jest.Mocked<ApplicationSetup> =>
lazyObject({
register: jest.fn(),
registerAppUpdater: jest.fn(),
});

const createInternalSetupContractMock = (): jest.Mocked<InternalApplicationSetup> => ({
register: jest.fn(),
registerAppUpdater: jest.fn(),
});
const createInternalSetupContractMock = (): jest.Mocked<InternalApplicationSetup> =>
lazyObject({
register: jest.fn(),
registerAppUpdater: jest.fn(),
});

const createStartContractMock = (): jest.Mocked<ApplicationStart> => {
const currentAppId$ = new Subject<string | undefined>();
const currentLocation$ = new Subject<string>();

return {
return lazyObject({
applications$: new BehaviorSubject<Map<string, PublicAppInfo>>(new Map()),
currentAppId$: currentAppId$.asObservable(),
currentLocation$: currentLocation$.asObservable(),
Expand All @@ -52,11 +55,11 @@ const createStartContractMock = (): jest.Mocked<ApplicationStart> => {
navigateToUrl: jest.fn(),
getUrlForApp: jest.fn(),
isAppRegistered: jest.fn(),
};
});
};

const createHistoryMock = (): jest.Mocked<History> => {
return {
return lazyObject({
block: jest.fn(),
createHref: jest.fn(),
go: jest.fn(),
Expand All @@ -74,7 +77,7 @@ const createHistoryMock = (): jest.Mocked<History> => {
key: '',
state: undefined,
},
};
});
};

const createInternalStartContractMock = (
Expand All @@ -85,7 +88,7 @@ const createInternalStartContractMock = (
: new Subject<string | undefined>();
const currentLocation$ = new Subject<string>();

return {
return lazyObject({
applications$: new BehaviorSubject<Map<string, PublicAppInfo>>(new Map()),
capabilities: capabilitiesServiceMock.createStartContract().capabilities,
currentAppId$: currentAppId$.asObservable(),
Expand All @@ -97,27 +100,28 @@ const createInternalStartContractMock = (
navigateToApp: jest.fn().mockImplementation((appId) => currentAppId$.next(appId)),
navigateToUrl: jest.fn(),
history: createHistoryMock(),
};
});
};

const createAppMountParametersMock = (parts: Partial<AppMountParameters> = {}) => {
const mock: AppMountParameters = {
const mock: AppMountParameters = lazyObject({
element: document.createElement('div'),
history: scopedHistoryMock.create(),
appBasePath: '/app',
onAppLeave: jest.fn(),
setHeaderActionMenu: jest.fn(),
theme$: themeServiceMock.createTheme$(),
...parts,
};
});
return mock;
};

const createMock = (): jest.Mocked<ApplicationServiceContract> => ({
setup: jest.fn().mockReturnValue(createInternalSetupContractMock()),
start: jest.fn().mockReturnValue(createInternalStartContractMock()),
stop: jest.fn(),
});
const createMock = (): jest.Mocked<ApplicationServiceContract> =>
lazyObject({
setup: jest.fn().mockReturnValue(createInternalSetupContractMock()),
start: jest.fn().mockReturnValue(createInternalStartContractMock()),
stop: jest.fn(),
});

export const applicationServiceMock = {
create: createMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import type { Location } from 'history';
import type { ScopedHistory } from '@kbn/core-application-browser';
import { lazyObject } from '@kbn/lazy-object';

export type ScopedHistoryMock = jest.Mocked<ScopedHistory>;

Expand All @@ -19,7 +20,7 @@ const createMock = ({
key,
state,
}: Partial<Location> = {}) => {
const mock: ScopedHistoryMock = {
const mock: ScopedHistoryMock = lazyObject({
block: jest.fn(),
createHref: jest.fn(),
createSubHistory: jest.fn(),
Expand All @@ -38,7 +39,7 @@ const createMock = ({
hash,
key,
},
};
});

return mock;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@kbn/core-capabilities-browser-mocks",
"@kbn/core-theme-browser-mocks",
"@kbn/utility-types",
"@kbn/lazy-object",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import type { MockedLogger } from '@kbn/logging-mocks';
import { loggerMock } from '@kbn/logging-mocks';
import type { CoreContext } from '@kbn/core-base-browser-internal';
import { lazyObject } from '@kbn/lazy-object';

function createCoreContext({ production = false }: { production?: boolean } = {}): CoreContext & {
logger: MockedLogger;
} {
return {
return lazyObject({
coreId: Symbol('core context mock'),
logger: loggerMock.create(),
env: {
Expand All @@ -34,7 +35,7 @@ function createCoreContext({ production = false }: { production?: boolean } = {}
buildFlavor: 'traditional',
},
},
};
});
}

export const coreContextMock = {
Expand Down
3 changes: 2 additions & 1 deletion src/core/packages/base/browser-mocks/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
],
"kbn_references": [
"@kbn/logging-mocks",
"@kbn/core-base-browser-internal"
"@kbn/core-base-browser-internal",
"@kbn/lazy-object"
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { LoggerFactory } from '@kbn/logging';
import { loggerMock } from '@kbn/logging-mocks';
import { configServiceMock, createTestEnv } from '@kbn/config-mocks';
import type { CoreContext } from '@kbn/core-base-server-internal';
import { lazyObject } from '@kbn/lazy-object';

function create({
env = createTestEnv(),
Expand All @@ -23,7 +24,12 @@ function create({
logger?: jest.Mocked<LoggerFactory>;
configService?: jest.Mocked<IConfigService>;
} = {}): DeeplyMockedKeys<CoreContext> {
return { coreId: Symbol(), env: env as DeeplyMockedKeys<typeof env>, logger, configService };
return lazyObject({
coreId: Symbol(),
env: env as DeeplyMockedKeys<typeof env>,
logger,
configService,
});
}

export const mockCoreContext = {
Expand Down
Loading