Skip to content

Commit

Permalink
Migrates core test_utils to package (#143483)
Browse files Browse the repository at this point in the history
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
TinaHeiligers and kibanamachine authored Oct 18, 2022
1 parent eb666e2 commit 836b60d
Show file tree
Hide file tree
Showing 37 changed files with 263 additions and 47 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ packages/core/status/core-status-server-mocks @elastic/kibana-core
packages/core/test-helpers/core-test-helpers-deprecations-getters @elastic/kibana-core
packages/core/test-helpers/core-test-helpers-http-setup-browser @elastic/kibana-core
packages/core/test-helpers/core-test-helpers-so-type-serializer @elastic/kibana-core
packages/core/test-helpers/core-test-helpers-test-utils @elastic/kibana-core
packages/core/theme/core-theme-browser @elastic/kibana-core
packages/core/theme/core-theme-browser-internal @elastic/kibana-core
packages/core/theme/core-theme-browser-mocks @elastic/kibana-core
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@
"@kbn/core-test-helpers-deprecations-getters": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-deprecations-getters",
"@kbn/core-test-helpers-http-setup-browser": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-http-setup-browser",
"@kbn/core-test-helpers-so-type-serializer": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-so-type-serializer",
"@kbn/core-test-helpers-test-utils": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-test-utils",
"@kbn/core-theme-browser": "link:bazel-bin/packages/core/theme/core-theme-browser",
"@kbn/core-theme-browser-internal": "link:bazel-bin/packages/core/theme/core-theme-browser-internal",
"@kbn/core-theme-browser-mocks": "link:bazel-bin/packages/core/theme/core-theme-browser-mocks",
Expand Down Expand Up @@ -1030,6 +1031,7 @@
"@types/kbn__core-test-helpers-deprecations-getters": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-deprecations-getters/npm_module_types",
"@types/kbn__core-test-helpers-http-setup-browser": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-http-setup-browser/npm_module_types",
"@types/kbn__core-test-helpers-so-type-serializer": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-so-type-serializer/npm_module_types",
"@types/kbn__core-test-helpers-test-utils": "link:bazel-bin/packages/core/test-helpers/core-test-helpers-test-utils/npm_module_types",
"@types/kbn__core-theme-browser": "link:bazel-bin/packages/core/theme/core-theme-browser/npm_module_types",
"@types/kbn__core-theme-browser-internal": "link:bazel-bin/packages/core/theme/core-theme-browser-internal/npm_module_types",
"@types/kbn__core-theme-browser-mocks": "link:bazel-bin/packages/core/theme/core-theme-browser-mocks/npm_module_types",
Expand Down
2 changes: 2 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ filegroup(
"//packages/core/test-helpers/core-test-helpers-deprecations-getters:build",
"//packages/core/test-helpers/core-test-helpers-http-setup-browser:build",
"//packages/core/test-helpers/core-test-helpers-so-type-serializer:build",
"//packages/core/test-helpers/core-test-helpers-test-utils:build",
"//packages/core/theme/core-theme-browser:build",
"//packages/core/theme/core-theme-browser-internal:build",
"//packages/core/theme/core-theme-browser-mocks:build",
Expand Down Expand Up @@ -503,6 +504,7 @@ filegroup(
"//packages/core/test-helpers/core-test-helpers-deprecations-getters:build_types",
"//packages/core/test-helpers/core-test-helpers-http-setup-browser:build_types",
"//packages/core/test-helpers/core-test-helpers-so-type-serializer:build_types",
"//packages/core/test-helpers/core-test-helpers-test-utils:build_types",
"//packages/core/theme/core-theme-browser:build_types",
"//packages/core/theme/core-theme-browser-internal:build_types",
"//packages/core/theme/core-theme-browser-mocks:build_types",
Expand Down
126 changes: 126 additions & 0 deletions packages/core/test-helpers/core-test-helpers-test-utils/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")

PKG_DIRNAME = "core-test-helpers-test-utils"
PKG_REQUIRE_NAME = "@kbn/core-test-helpers-test-utils"

SOURCE_FILES = glob(
[
"**/*.ts",
],
exclude = [
"**/*.config.js",
"**/*.mock.*",
"**/*.test.*",
"**/*.stories.*",
"**/__snapshots__/**",
"**/integration_tests/**",
"**/mocks/**",
"**/scripts/**",
"**/storybook/**",
"**/test_fixtures/**",
"**/test_helpers/**",
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
]

RUNTIME_DEPS = [
"//packages/core/deprecations/core-deprecations-server-mocks",
"//packages/core/execution-context/core-execution-context-server-mocks",
"//packages/core/elasticsearch/core-elasticsearch-server-mocks",
"//packages/core/http/core-http-context-server-internal",
"//packages/core/http/core-http-context-server-mocks",
"//packages/core/http/core-http-server-mocks",
"//packages/core/saved-objects/core-saved-objects-api-server-mocks",
"//packages/core/saved-objects/core-saved-objects-base-server-mocks",
"//packages/core/saved-objects/core-saved-objects-server-mocks",
"//packages/core/ui-settings/core-ui-settings-server-mocks",
]

TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"//packages/core/deprecations/core-deprecations-server-mocks:npm_module_types",
"//packages/core/execution-context/core-execution-context-server-mocks:npm_module_types",
"//packages/core/elasticsearch/core-elasticsearch-server-mocks:npm_module_types",
"//packages/core/http/core-http-context-server-internal:npm_module_types",
"//packages/core/http/core-http-context-server-mocks:npm_module_types",
"//packages/core/http/core-http-server-mocks:npm_module_types",
"//packages/core/saved-objects/core-saved-objects-api-server-mocks:npm_module_types",
"//packages/core/saved-objects/core-saved-objects-base-server-mocks:npm_module_types",
"//packages/core/saved-objects/core-saved-objects-server:npm_module_types",
"//packages/core/saved-objects/core-saved-objects-server-mocks:npm_module_types",
"//packages/core/ui-settings/core-ui-settings-server-mocks:npm_module_types",
]

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)

ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
tsconfig = ":tsconfig",
)

js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)

filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)

pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)

filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @kbn/core-test-helpers-test-utils

This package contains Core's server-side test utils.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@
* Side Public License, v 1.
*/

export { createHttpServer } from '@kbn/core-http-server-mocks';
export { setupServer } from './integration_tests/saved_objects/routes/test_utils';
export {
getDeprecationsFor,
getDeprecationsForGlobalSettings,
} from '@kbn/core-test-helpers-deprecations-getters';
export { setupServer } from './src/setup_server';
export { createExportableType } from './src/create_exportable_type';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../../../..',
roots: ['<rootDir>/packages/core/test-helpers/core-test-helpers-test-utils'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "shared-common",
"id": "@kbn/core-test-helpers-test-utils",
"owner": "@elastic/kibana-core",
"runtimeDeps": [],
"typeDeps": [],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/core-test-helpers-test-utils",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SavedObjectsType } from '@kbn/core-saved-objects-server';
export const createExportableType = (name: string): SavedObjectsType => {
return {
name,
hidden: false,
namespaceType: 'single',
mappings: {
properties: {},
},
management: {
importableAndExportable: true,
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,36 @@
import { executionContextServiceMock } from '@kbn/core-execution-context-server-mocks';
import { ContextService } from '@kbn/core-http-context-server-internal';
import { createHttpServer, createCoreContext } from '@kbn/core-http-server-mocks';
import type { SavedObjectsType } from '@kbn/core-saved-objects-server';
import { contextServiceMock, coreMock } from '../../../mocks';
import { contextServiceMock } from '@kbn/core-http-context-server-mocks';
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks';
import { typeRegistryMock } from '@kbn/core-saved-objects-base-server-mocks';
import { savedObjectsServiceMock } from '@kbn/core-saved-objects-server-mocks';
import { elasticsearchServiceMock } from '@kbn/core-elasticsearch-server-mocks';
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-server-mocks';
import { deprecationsServiceMock } from '@kbn/core-deprecations-server-mocks';

const defaultCoreId = Symbol('core');

function createCoreServerRequestHandlerContextMock() {
return {
savedObjects: {
client: savedObjectsClientMock.create(),
typeRegistry: typeRegistryMock.create(),
getClient: savedObjectsClientMock.create,
getExporter: savedObjectsServiceMock.createExporter,
getImporter: savedObjectsServiceMock.createImporter,
},
elasticsearch: {
client: elasticsearchServiceMock.createScopedClusterClient(),
},
uiSettings: {
client: uiSettingsServiceMock.createClient(),
},
deprecations: {
client: deprecationsServiceMock.createClient(),
},
};
}
export const setupServer = async (coreId: symbol = defaultCoreId) => {
const coreContext = createCoreContext({ coreId });
const contextService = new ContextService(coreContext);
Expand All @@ -24,7 +49,7 @@ export const setupServer = async (coreId: symbol = defaultCoreId) => {
context: contextService.setup({ pluginDependencies: new Map() }),
executionContext: executionContextServiceMock.createInternalSetupContract(),
});
const handlerContext = coreMock.createRequestHandlerContext();
const handlerContext = createCoreServerRequestHandlerContextMock();

httpSetup.registerRouteHandlerContext<any, 'core'>(coreId, 'core', (ctx, req, res) => {
return handlerContext;
Expand All @@ -36,17 +61,3 @@ export const setupServer = async (coreId: symbol = defaultCoreId) => {
handlerContext,
};
};

export const createExportableType = (name: string): SavedObjectsType => {
return {
name,
hidden: false,
namespaceType: 'single',
mappings: {
properties: {},
},
management: {
importableAndExportable: true,
},
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../../../tsconfig.bazel.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "target_types",
"stripInternal": false,
"types": [
"jest",
"node"
]
},
"include": [
"**/*.ts",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';

import {
registerBulkCreateRoute,
type InternalSavedObjectsRequestHandlerContext,
} from '@kbn/core-saved-objects-server-internal';
import { setupServer } from '@kbn/core-test-helpers-test-utils';

type SetupServerReturn = Awaited<ReturnType<typeof setupServer>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerBulkDeleteRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerBulkGetRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerBulkResolveRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerBulkUpdateRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerCreateRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
coreUsageStatsClientMock,
coreUsageDataServiceMock,
} from '@kbn/core-usage-data-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import {
registerDeleteRoute,
type InternalSavedObjectsRequestHandlerContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import supertest from 'supertest';
import { elasticsearchServiceMock } from '@kbn/core-elasticsearch-server-mocks';
import { typeRegistryMock } from '@kbn/core-saved-objects-base-server-mocks';
import { setupServer } from './test_utils';
import { setupServer } from '@kbn/core-test-helpers-test-utils';
import { SavedObjectsType } from '../../..';
import {
registerDeleteUnknownTypesRoute,
Expand Down
Loading

0 comments on commit 836b60d

Please sign in to comment.