@kiwiproject/kiwi-test-js 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,14 +9,22 @@ declare function startElasticSearchContainer(image?: string): Promise<void>;
9
9
  * previously called.
10
10
  */
11
11
  declare function stopElasticSearchContainer(): Promise<void>;
12
+ declare function setElasticSearchUrl(host: string, port: number): void;
12
13
  /**
13
14
  * Retrieves the URL for accessing the running Elastic Search container. Error will be thrown if startElasticSearchContainer
14
15
  * was not previously called.
15
16
  */
16
17
  declare function getElasticSearchUrl(): string;
18
+ declare function createIndex(indexName: string, indexMapping: unknown, pipelines: Array<unknown>): Promise<void>;
19
+ declare function clearIndex(indexName: string): Promise<void>;
20
+ declare function deleteIndex(indexName: string): Promise<void>;
17
21
  export declare const ElasticSearchExtension: {
18
22
  startElasticSearchContainer: typeof startElasticSearchContainer;
19
23
  stopElasticSearchContainer: typeof stopElasticSearchContainer;
24
+ setElasticSearchUrl: typeof setElasticSearchUrl;
20
25
  getElasticSearchUrl: typeof getElasticSearchUrl;
26
+ createIndex: typeof createIndex;
27
+ clearIndex: typeof clearIndex;
28
+ deleteIndex: typeof deleteIndex;
21
29
  };
22
30
  export {};
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.ElasticSearchExtension = void 0;
13
13
  const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
14
  const elasticsearch_1 = require("@testcontainers/elasticsearch");
15
+ const elasticsearch_2 = require("@elastic/elasticsearch");
15
16
  /**
16
17
  * Starts an Elastic search container and stores the container information in global.ELASTIC_SEARCH_CONTAINER.
17
18
  *
@@ -19,9 +20,12 @@ const elasticsearch_1 = require("@testcontainers/elasticsearch");
19
20
  */
20
21
  function startElasticSearchContainer(image = "elasticsearch:8.6.1") {
21
22
  return __awaiter(this, void 0, void 0, function* () {
22
- global.ELASTIC_SEARCH_CONTAINER = yield new elasticsearch_1.ElasticsearchContainer(image)
23
+ const container = yield new elasticsearch_1.ElasticsearchContainer(image)
23
24
  .withEnvironment({ "xpack.security.enabled": "false" })
24
25
  .start();
26
+ process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI = container.getHttpUrl();
27
+ // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
28
+ global.ELASTIC_SEARCH_CONTAINER = container;
25
29
  });
26
30
  }
27
31
  /**
@@ -30,21 +34,64 @@ function startElasticSearchContainer(image = "elasticsearch:8.6.1") {
30
34
  */
31
35
  function stopElasticSearchContainer() {
32
36
  return __awaiter(this, void 0, void 0, function* () {
33
- kiwi_js_1.KiwiPreconditions.checkState(global.ELASTIC_SEARCH_CONTAINER !== undefined, "Elastic Search container has not been previously started");
37
+ kiwi_js_1.KiwiPreconditions.checkState(global.ELASTIC_SEARCH_CONTAINER !== undefined, "Elastic Search container has not been previously started or is not running in band");
34
38
  yield global.ELASTIC_SEARCH_CONTAINER.stop();
35
39
  global.ELASTIC_SEARCH_CONTAINER = undefined;
40
+ delete process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI;
36
41
  });
37
42
  }
43
+ function setElasticSearchUrl(host, port) {
44
+ process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI = `http://${host}:${port}`;
45
+ }
38
46
  /**
39
47
  * Retrieves the URL for accessing the running Elastic Search container. Error will be thrown if startElasticSearchContainer
40
48
  * was not previously called.
41
49
  */
42
50
  function getElasticSearchUrl() {
43
- kiwi_js_1.KiwiPreconditions.checkState(global.ELASTIC_SEARCH_CONTAINER !== undefined, "Elastic Search container has not been previously started");
44
- return global.ELASTIC_SEARCH_CONTAINER.getHttpUrl();
51
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI !== undefined, "Elastic Search container has not been previously started");
52
+ return process.env.ELASTIC_SEARCH_EXTENSION_BASE_URI;
53
+ }
54
+ function createIndex(indexName, indexMapping, pipelines) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const client = new elasticsearch_2.Client({ node: getElasticSearchUrl() });
57
+ yield client.indices.create({
58
+ index: indexName,
59
+ mappings: indexMapping,
60
+ });
61
+ for (const pipeline of pipelines) {
62
+ yield client.ingest.putPipeline(pipeline);
63
+ }
64
+ yield client.close();
65
+ });
66
+ }
67
+ function clearIndex(indexName) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const client = new elasticsearch_2.Client({ node: getElasticSearchUrl() });
70
+ yield client.deleteByQuery({
71
+ index: indexName,
72
+ query: {
73
+ match_all: {},
74
+ },
75
+ refresh: true,
76
+ });
77
+ yield client.close();
78
+ });
79
+ }
80
+ function deleteIndex(indexName) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ const client = new elasticsearch_2.Client({ node: getElasticSearchUrl() });
83
+ yield client.indices.delete({
84
+ index: indexName,
85
+ });
86
+ yield client.close();
87
+ });
45
88
  }
46
89
  exports.ElasticSearchExtension = {
47
90
  startElasticSearchContainer,
48
91
  stopElasticSearchContainer,
92
+ setElasticSearchUrl,
49
93
  getElasticSearchUrl,
94
+ createIndex,
95
+ clearIndex,
96
+ deleteIndex,
50
97
  };
@@ -11,14 +11,26 @@ declare function startMinioContainer(accessKey?: string, secretKey?: string, ima
11
11
  * previously called.
12
12
  */
13
13
  declare function stopMinioContainer(): Promise<void>;
14
+ declare function setMinioPort(port: number): void;
15
+ declare function setMinioHost(host: string): void;
14
16
  /**
15
17
  * Retrieves the mapped external port of the started Minio container. Error will be thrown if startMinioContainer was
16
18
  * not previously called.
17
19
  */
18
20
  declare function getMinioPort(): number;
21
+ /**
22
+ * Retrieves the host of the started Minio container. Error will be thrown if startMinioContainer was
23
+ * not previously called.
24
+ *
25
+ * Note: In most cases this should return localhost.
26
+ */
27
+ declare function getMinioHost(): string;
19
28
  export declare const MinioExtension: {
20
29
  startMinioContainer: typeof startMinioContainer;
21
30
  stopMinioContainer: typeof stopMinioContainer;
31
+ setMinioPort: typeof setMinioPort;
22
32
  getMinioPort: typeof getMinioPort;
33
+ getMinioHost: typeof getMinioHost;
34
+ setMinioHost: typeof setMinioHost;
23
35
  };
24
36
  export {};
@@ -21,7 +21,7 @@ const testcontainers_1 = require("testcontainers");
21
21
  */
22
22
  function startMinioContainer(accessKey = "minioadmin", secretKey = "keyboard cat", image = "minio/minio:RELEASE.2023-08-09T23-30-22Z") {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- global.MINIO_CONTAINER = yield new testcontainers_1.GenericContainer(image)
24
+ const container = yield new testcontainers_1.GenericContainer(image)
25
25
  .withEnvironment({
26
26
  MINIO_BROWSER: "off",
27
27
  MINIO_ROOT_USER: accessKey,
@@ -32,6 +32,10 @@ function startMinioContainer(accessKey = "minioadmin", secretKey = "keyboard cat
32
32
  .withTmpFs({ "/data": "rw,noexec,nosuid" })
33
33
  .withCommand(["server", "/data"])
34
34
  .start();
35
+ setMinioPort(container.getMappedPort(9000));
36
+ setMinioHost(container.getHost());
37
+ // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
38
+ global.MINIO_CONTAINER = container;
35
39
  });
36
40
  }
37
41
  /**
@@ -40,21 +44,42 @@ function startMinioContainer(accessKey = "minioadmin", secretKey = "keyboard cat
40
44
  */
41
45
  function stopMinioContainer() {
42
46
  return __awaiter(this, void 0, void 0, function* () {
43
- kiwi_js_1.KiwiPreconditions.checkState(global.MINIO_CONTAINER !== undefined, "Minio container has not been previously started");
47
+ kiwi_js_1.KiwiPreconditions.checkState(global.MINIO_CONTAINER !== undefined, "Minio container has not been previously started or is not running in band");
44
48
  yield global.MINIO_CONTAINER.stop();
45
49
  global.MINIO_CONTAINER = undefined;
50
+ delete process.env.MINIO_EXTENSION_PORT;
51
+ delete process.env.MINIO_EXTENSION_HOST;
46
52
  });
47
53
  }
54
+ function setMinioPort(port) {
55
+ process.env.MINIO_EXTENSION_PORT = String(port);
56
+ }
57
+ function setMinioHost(host) {
58
+ process.env.MINIO_EXTENSION_HOST = host;
59
+ }
48
60
  /**
49
61
  * Retrieves the mapped external port of the started Minio container. Error will be thrown if startMinioContainer was
50
62
  * not previously called.
51
63
  */
52
64
  function getMinioPort() {
53
- kiwi_js_1.KiwiPreconditions.checkState(global.MINIO_CONTAINER !== undefined, "Minio container has not been previously started");
54
- return global.MINIO_CONTAINER.getMappedPort(9000);
65
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.MINIO_EXTENSION_PORT !== undefined, "Minio container has not been previously started");
66
+ return parseInt(process.env.MINIO_EXTENSION_PORT, 10);
67
+ }
68
+ /**
69
+ * Retrieves the host of the started Minio container. Error will be thrown if startMinioContainer was
70
+ * not previously called.
71
+ *
72
+ * Note: In most cases this should return localhost.
73
+ */
74
+ function getMinioHost() {
75
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.MINIO_EXTENSION_HOST !== undefined, "Minio container has not been previously started");
76
+ return process.env.MINIO_EXTENSION_HOST;
55
77
  }
56
78
  exports.MinioExtension = {
57
79
  startMinioContainer,
58
80
  stopMinioContainer,
81
+ setMinioPort,
59
82
  getMinioPort,
83
+ getMinioHost,
84
+ setMinioHost,
60
85
  };
@@ -9,6 +9,7 @@ declare function startMongoContainer(image?: string): Promise<void>;
9
9
  * previously called.
10
10
  */
11
11
  declare function stopMongoContainer(): Promise<void>;
12
+ declare function setMongoBaseUrl(host: string, port: number): void;
12
13
  /**
13
14
  * Retrieves the base connection URL for accessing the running Mongo. Error will be thrown if startMongoContainer
14
15
  * is not previously called.
@@ -20,10 +21,13 @@ declare function getMongoBaseUrl(): string;
20
21
  * @param dbName The name of the database in mongo to connect.
21
22
  */
22
23
  declare function getMongoUriWithDb(dbName: string): string;
24
+ declare function dropDatabase(dbName: string): Promise<void>;
23
25
  export declare const MongoExtension: {
24
26
  startMongoContainer: typeof startMongoContainer;
25
27
  stopMongoContainer: typeof stopMongoContainer;
28
+ setMongoBaseUrl: typeof setMongoBaseUrl;
26
29
  getMongoBaseUrl: typeof getMongoBaseUrl;
27
30
  getMongoUriWithDb: typeof getMongoUriWithDb;
31
+ dropDatabase: typeof dropDatabase;
28
32
  };
29
33
  export {};
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.MongoExtension = void 0;
13
13
  const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
14
  const testcontainers_1 = require("testcontainers");
15
+ const mongodb_1 = require("mongodb");
15
16
  /**
16
17
  * Starts a Mongo container and stores the container information in global.MONGO_CONTAINER.
17
18
  *
@@ -19,9 +20,12 @@ const testcontainers_1 = require("testcontainers");
19
20
  */
20
21
  function startMongoContainer(image = "mongo:6") {
21
22
  return __awaiter(this, void 0, void 0, function* () {
22
- global.MONGO_CONTAINER = yield new testcontainers_1.GenericContainer(image)
23
+ const container = yield new testcontainers_1.GenericContainer(image)
23
24
  .withExposedPorts(27017)
24
25
  .start();
26
+ setMongoBaseUrl(container.getHost(), container.getMappedPort(27017));
27
+ // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
28
+ global.MONGO_CONTAINER = container;
25
29
  });
26
30
  }
27
31
  /**
@@ -30,20 +34,22 @@ function startMongoContainer(image = "mongo:6") {
30
34
  */
31
35
  function stopMongoContainer() {
32
36
  return __awaiter(this, void 0, void 0, function* () {
33
- kiwi_js_1.KiwiPreconditions.checkState(global.MONGO_CONTAINER !== undefined, "Mongo container has not been previously started");
37
+ kiwi_js_1.KiwiPreconditions.checkState(global.MONGO_CONTAINER !== undefined, "Mongo container has not been previously started or is not running in band");
34
38
  yield global.MONGO_CONTAINER.stop();
35
39
  global.MONGO_CONTAINER = undefined;
40
+ delete process.env.MONGO_EXTENSION_BASE_URI;
36
41
  });
37
42
  }
43
+ function setMongoBaseUrl(host, port) {
44
+ process.env.MONGO_EXTENSION_BASE_URI = `mongodb://${host}:${port}/`;
45
+ }
38
46
  /**
39
47
  * Retrieves the base connection URL for accessing the running Mongo. Error will be thrown if startMongoContainer
40
48
  * is not previously called.
41
49
  */
42
50
  function getMongoBaseUrl() {
43
- kiwi_js_1.KiwiPreconditions.checkState(global.MONGO_CONTAINER !== undefined, "Mongo container has not been previously started");
44
- const host = global.MONGO_CONTAINER.getHost();
45
- const port = global.MONGO_CONTAINER.getMappedPort(27017);
46
- return `mongodb://${host}:${port}/`;
51
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.MONGO_EXTENSION_BASE_URI !== undefined, "Mongo container has not been previously started");
52
+ return process.env.MONGO_EXTENSION_BASE_URI;
47
53
  }
48
54
  /**
49
55
  * Retrieves the base connection URL plus the given db name for accessing the running Mongo. Error will be thrown if
@@ -53,9 +59,20 @@ function getMongoBaseUrl() {
53
59
  function getMongoUriWithDb(dbName) {
54
60
  return `${getMongoBaseUrl()}${dbName}`;
55
61
  }
62
+ function dropDatabase(dbName) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ const client = new mongodb_1.MongoClient(getMongoBaseUrl());
65
+ yield client.connect();
66
+ const db = client.db(dbName);
67
+ yield db.dropDatabase();
68
+ yield client.close(true);
69
+ });
70
+ }
56
71
  exports.MongoExtension = {
57
72
  startMongoContainer,
58
73
  stopMongoContainer,
74
+ setMongoBaseUrl,
59
75
  getMongoBaseUrl,
60
76
  getMongoUriWithDb,
77
+ dropDatabase,
61
78
  };
@@ -9,6 +9,7 @@ declare function startPostgresContainer(image?: string): Promise<void>;
9
9
  * previously called.
10
10
  */
11
11
  declare function stopPostgresContainer(): Promise<void>;
12
+ declare function setPostgresBaseUrl(host: string, port: number, user?: string, password?: string, dbName?: string): void;
12
13
  /**
13
14
  * Retrieves the base URL for accessing the running Postgres container. Error will be thrown if
14
15
  * startPostgresContainer is not previously called.
@@ -28,11 +29,14 @@ declare function getPostgresUriWithDb(dbName: string): string;
28
29
  * @param dbName The name of the database to create.
29
30
  */
30
31
  declare function setupNewDatabase(dbName: string): Promise<void>;
32
+ declare function dropDatabase(dbName: string): Promise<void>;
31
33
  export declare const PostgresExtension: {
32
34
  startPostgresContainer: typeof startPostgresContainer;
33
35
  stopPostgresContainer: typeof stopPostgresContainer;
36
+ setPostgresBaseUrl: typeof setPostgresBaseUrl;
34
37
  getPostgresBaseUrl: typeof getPostgresBaseUrl;
35
38
  getPostgresUriWithDb: typeof getPostgresUriWithDb;
36
39
  setupNewDatabase: typeof setupNewDatabase;
40
+ dropDatabase: typeof dropDatabase;
37
41
  };
38
42
  export {};
@@ -20,7 +20,10 @@ const pg_1 = require("pg");
20
20
  */
21
21
  function startPostgresContainer(image = "postgres:15") {
22
22
  return __awaiter(this, void 0, void 0, function* () {
23
- global.POSTGRES_CONTAINER = yield new postgresql_1.PostgreSqlContainer(image).start();
23
+ const container = yield new postgresql_1.PostgreSqlContainer(image).start();
24
+ process.env.POSTGRES_EXTENSION_BASE_URI = container.getConnectionUri();
25
+ // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
26
+ global.POSTGRES_CONTAINER = container;
24
27
  });
25
28
  }
26
29
  /**
@@ -29,18 +32,22 @@ function startPostgresContainer(image = "postgres:15") {
29
32
  */
30
33
  function stopPostgresContainer() {
31
34
  return __awaiter(this, void 0, void 0, function* () {
32
- kiwi_js_1.KiwiPreconditions.checkState(global.POSTGRES_CONTAINER !== undefined, "Postgres container has not been previously started");
35
+ kiwi_js_1.KiwiPreconditions.checkState(global.POSTGRES_CONTAINER !== undefined, "Postgres container has not been previously started or is not running in band");
33
36
  yield global.POSTGRES_CONTAINER.stop();
34
37
  global.POSTGRES_CONTAINER = undefined;
38
+ delete process.env.POSTGRES_EXTENSION_BASE_URI;
35
39
  });
36
40
  }
41
+ function setPostgresBaseUrl(host, port, user = "test", password = "test", dbName = "test") {
42
+ process.env.POSTGRES_EXTENSION_BASE_URI = `postgres://${user}:${password}@${host}:${port}/${dbName}`;
43
+ }
37
44
  /**
38
45
  * Retrieves the base URL for accessing the running Postgres container. Error will be thrown if
39
46
  * startPostgresContainer is not previously called.
40
47
  */
41
48
  function getPostgresBaseUrl() {
42
- kiwi_js_1.KiwiPreconditions.checkState(global.POSTGRES_CONTAINER !== undefined, "Postgres container has not been previously started");
43
- return global.POSTGRES_CONTAINER.getConnectionUri();
49
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.POSTGRES_EXTENSION_BASE_URI !== undefined, "Postgres container has not been previously started");
50
+ return process.env.POSTGRES_EXTENSION_BASE_URI;
44
51
  }
45
52
  /**
46
53
  * Retrieves the base URL plus a given database for accessing the running Postgres container. Error will be thrown if
@@ -67,10 +74,23 @@ function setupNewDatabase(dbName) {
67
74
  yield dbClient.end();
68
75
  });
69
76
  }
77
+ function dropDatabase(dbName) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const dbClient = new pg_1.Client({
80
+ connectionString: getPostgresBaseUrl(),
81
+ });
82
+ yield dbClient.connect();
83
+ yield dbClient.query(`select pg_terminate_backend(pid) from pg_stat_activity where datname = '${dbName}'`);
84
+ yield dbClient.query(`drop database ${dbName}`);
85
+ yield dbClient.end();
86
+ });
87
+ }
70
88
  exports.PostgresExtension = {
71
89
  startPostgresContainer,
72
90
  stopPostgresContainer,
91
+ setPostgresBaseUrl,
73
92
  getPostgresBaseUrl,
74
93
  getPostgresUriWithDb,
75
94
  setupNewDatabase,
95
+ dropDatabase,
76
96
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiwiproject/kiwi-test-js",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "kiwi-test-js is a test utility library. Most of these utilities are ports from the Java Kiwi-test library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,26 +22,27 @@
22
22
  ]
23
23
  },
24
24
  "dependencies": {
25
- "@jest/globals": "29.6.2",
25
+ "@elastic/elasticsearch": "8.9.0",
26
+ "@jest/globals": "29.7.0",
26
27
  "@kiwiproject/kiwi-js": "0.8.0",
27
28
  "@testcontainers/elasticsearch": "10.2.1",
28
29
  "@testcontainers/postgresql": "10.2.1",
29
- "jest": "29.6.2",
30
- "mongodb": "5.7.0",
31
- "pg": "8.11.2",
30
+ "jest": "29.7.0",
31
+ "mongodb": "6.0.0",
32
+ "pg": "8.11.3",
32
33
  "testcontainers": "10.2.1"
33
34
  },
34
35
  "devDependencies": {
35
- "@babel/core": "7.22.10",
36
- "@babel/preset-env": "7.22.10",
37
- "@babel/preset-typescript": "7.22.5",
38
- "@types/node": "20.5.0",
36
+ "@babel/core": "7.22.17",
37
+ "@babel/preset-env": "7.22.15",
38
+ "@babel/preset-typescript": "7.22.15",
39
+ "@types/node": "20.6.0",
39
40
  "@types/pg": "8.10.2",
40
- "@typescript-eslint/eslint-plugin": "6.4.0",
41
- "@typescript-eslint/parser": "6.4.0",
42
- "babel-jest": "29.6.2",
43
- "eslint": "8.47.0",
44
- "prettier": "3.0.2",
45
- "typescript": "5.1.6"
41
+ "@typescript-eslint/eslint-plugin": "6.7.0",
42
+ "@typescript-eslint/parser": "6.7.0",
43
+ "babel-jest": "29.7.0",
44
+ "eslint": "8.49.0",
45
+ "prettier": "3.0.3",
46
+ "typescript": "5.2.2"
46
47
  }
47
48
  }