@kiwiproject/kiwi-test-js 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Kiwi Project
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,18 @@
1
+ ### KiwiTestJS
2
+ [![Build](https://github.com/kiwiproject/kiwi-test-js/workflows/build/badge.svg)](https://github.com/kiwiproject/kiwi-test-js/actions?query=workflow%3Abuild)
3
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=kiwiproject_kiwi-test-js&metric=alert_status)](https://sonarcloud.io/dashboard?id=kiwiproject_kiwi-test-js)
4
+ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=kiwiproject_kiwi-test-js&metric=coverage)](https://sonarcloud.io/dashboard?id=kiwiproject_kiwi-test-js)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
6
+ [![NPM](https://img.shields.io/npm/v/@kiwiproject/kiwi-test-js)](https://www.npmjs.com/package/@kiwiproject/kiwi-test-js)
7
+
8
+ kiwi-test-js is a test utility library. Most of these utilities are ports from the Java Kiwi-test library (https://github.com/kiwiproject/kiwi-test).
9
+
10
+ ### TestContainers
11
+ There are a few prebuilt implementations of starting up TestContainers to use in tests. These utilities allow for global
12
+ setup and teardown methods to be created to run containers before all tests and after all tests not just per test file.
13
+ Currently there are 4 implementations:
14
+
15
+ * PostgresExtension - Spins up and shuts down a Postgres container for use and allows for creating databases
16
+ * MongoExtension - Spins up and shuts down a Mongo container for use
17
+ * ElasticSearchExtension - Spins up and shuts down a Elastic Search container for use
18
+ * MinioExtension - Spins up and shuts down a Minio container for use
@@ -0,0 +1,9 @@
1
+ declare function startElasticSearchContainer(): Promise<void>;
2
+ declare function stopElasticSearchContainer(): Promise<void>;
3
+ declare function getElasticSearchUrl(): string;
4
+ export declare const ElasticSearchExtension: {
5
+ startElasticSearchContainer: typeof startElasticSearchContainer;
6
+ stopElasticSearchContainer: typeof stopElasticSearchContainer;
7
+ getElasticSearchUrl: typeof getElasticSearchUrl;
8
+ };
9
+ export {};
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ElasticSearchExtension = void 0;
13
+ const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
+ const elasticsearch_1 = require("@testcontainers/elasticsearch");
15
+ function startElasticSearchContainer() {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ global.ELASTIC_SEARCH_CONTAINER = yield new elasticsearch_1.ElasticsearchContainer("elasticsearch:8.6.1")
18
+ .withEnvironment({ "xpack.security.enabled": "false" })
19
+ .start();
20
+ });
21
+ }
22
+ function stopElasticSearchContainer() {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ kiwi_js_1.KiwiPreconditions.checkState(global.ELASTIC_SEARCH_CONTAINER !== undefined, "Elastic Search container has not been previously started");
25
+ yield global.ELASTIC_SEARCH_CONTAINER.stop();
26
+ global.ELASTIC_SEARCH_CONTAINER = undefined;
27
+ });
28
+ }
29
+ function getElasticSearchUrl() {
30
+ kiwi_js_1.KiwiPreconditions.checkState(global.ELASTIC_SEARCH_CONTAINER !== undefined, "Elastic Search container has not been previously started");
31
+ return global.ELASTIC_SEARCH_CONTAINER.getHttpUrl();
32
+ }
33
+ exports.ElasticSearchExtension = {
34
+ startElasticSearchContainer,
35
+ stopElasticSearchContainer,
36
+ getElasticSearchUrl,
37
+ };
@@ -0,0 +1,4 @@
1
+ export { MongoExtension } from "./mongo/mongo-extension";
2
+ export { PostgresExtension } from "./postgres/postgres-extension";
3
+ export { ElasticSearchExtension } from "./elasticsearch/elastic-search-extension";
4
+ export { MinioExtension } from "./minio/minio-extension";
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MinioExtension = exports.ElasticSearchExtension = exports.PostgresExtension = exports.MongoExtension = void 0;
4
+ var mongo_extension_1 = require("./mongo/mongo-extension");
5
+ Object.defineProperty(exports, "MongoExtension", { enumerable: true, get: function () { return mongo_extension_1.MongoExtension; } });
6
+ var postgres_extension_1 = require("./postgres/postgres-extension");
7
+ Object.defineProperty(exports, "PostgresExtension", { enumerable: true, get: function () { return postgres_extension_1.PostgresExtension; } });
8
+ var elastic_search_extension_1 = require("./elasticsearch/elastic-search-extension");
9
+ Object.defineProperty(exports, "ElasticSearchExtension", { enumerable: true, get: function () { return elastic_search_extension_1.ElasticSearchExtension; } });
10
+ var minio_extension_1 = require("./minio/minio-extension");
11
+ Object.defineProperty(exports, "MinioExtension", { enumerable: true, get: function () { return minio_extension_1.MinioExtension; } });
@@ -0,0 +1,9 @@
1
+ declare function startMinioSearchContainer(accessKey: string, secretKey: string): Promise<void>;
2
+ declare function stopMinioSearchContainer(): Promise<void>;
3
+ declare function getMinioPort(): number;
4
+ export declare const MinioExtension: {
5
+ startMinioSearchContainer: typeof startMinioSearchContainer;
6
+ stopMinioSearchContainer: typeof stopMinioSearchContainer;
7
+ getMinioPort: typeof getMinioPort;
8
+ };
9
+ export {};
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MinioExtension = void 0;
13
+ const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
+ const testcontainers_1 = require("testcontainers");
15
+ function startMinioSearchContainer(accessKey, secretKey) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ console.log(accessKey);
18
+ console.log(secretKey);
19
+ global.MINIO_CONTAINER = yield new testcontainers_1.GenericContainer('quay.io/minio/minio:latest')
20
+ .withEnvironment({
21
+ MINIO_BROWSER: 'off',
22
+ MINIO_ROOT_USER: accessKey,
23
+ MINIO_ROOT_PASSWORD: secretKey,
24
+ })
25
+ .withExposedPorts(9000)
26
+ .withWaitStrategy(testcontainers_1.Wait.forAll([testcontainers_1.Wait.forListeningPorts(), testcontainers_1.Wait.forLogMessage(/1 Online/)]))
27
+ .withTmpFs({ '/data': 'rw,noexec,nosuid' })
28
+ .withCommand(['server', '/data'])
29
+ .start();
30
+ });
31
+ }
32
+ function stopMinioSearchContainer() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ kiwi_js_1.KiwiPreconditions.checkState(global.MINIO_CONTAINER !== undefined, "Minio container has not been previously started");
35
+ yield global.MINIO_CONTAINER.stop();
36
+ global.MINIO_CONTAINER = undefined;
37
+ });
38
+ }
39
+ function getMinioPort() {
40
+ kiwi_js_1.KiwiPreconditions.checkState(global.MINIO_CONTAINER !== undefined, "Minio container has not been previously started");
41
+ return global.MINIO_CONTAINER.getMappedPort(9000);
42
+ }
43
+ exports.MinioExtension = {
44
+ startMinioSearchContainer,
45
+ stopMinioSearchContainer,
46
+ getMinioPort,
47
+ };
@@ -0,0 +1,11 @@
1
+ declare function startMongoContainer(): Promise<void>;
2
+ declare function stopMongoContainer(): Promise<void>;
3
+ declare function getMongoBaseUrl(): string;
4
+ declare function getMongoUriWithDb(dbName: string): string;
5
+ export declare const MongoExtension: {
6
+ startMongoContainer: typeof startMongoContainer;
7
+ stopMongoContainer: typeof stopMongoContainer;
8
+ getMongoBaseUrl: typeof getMongoBaseUrl;
9
+ getMongoUriWithDb: typeof getMongoUriWithDb;
10
+ };
11
+ export {};
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.MongoExtension = void 0;
13
+ const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
+ const testcontainers_1 = require("testcontainers");
15
+ function startMongoContainer() {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ global.MONGO_CONTAINER = yield new testcontainers_1.GenericContainer("mongo")
18
+ .withExposedPorts(27017)
19
+ .start();
20
+ });
21
+ }
22
+ function stopMongoContainer() {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ kiwi_js_1.KiwiPreconditions.checkState(global.MONGO_CONTAINER !== undefined, "Mongo container has not been previously started");
25
+ yield global.MONGO_CONTAINER.stop();
26
+ global.MONGO_CONTAINER = undefined;
27
+ });
28
+ }
29
+ function getMongoBaseUrl() {
30
+ kiwi_js_1.KiwiPreconditions.checkState(global.MONGO_CONTAINER !== undefined, "Mongo container has not been previously started");
31
+ const host = global.MONGO_CONTAINER.getHost();
32
+ const port = global.MONGO_CONTAINER.getMappedPort(27017);
33
+ return `mongodb://${host}:${port}/`;
34
+ }
35
+ function getMongoUriWithDb(dbName) {
36
+ return `${getMongoBaseUrl()}${dbName}`;
37
+ }
38
+ exports.MongoExtension = {
39
+ startMongoContainer,
40
+ stopMongoContainer,
41
+ getMongoBaseUrl,
42
+ getMongoUriWithDb,
43
+ };
@@ -0,0 +1,13 @@
1
+ declare function startPostgresContainer(): Promise<void>;
2
+ declare function stopPostgresContainer(): Promise<void>;
3
+ declare function getPostgresBaseUrl(): string;
4
+ declare function getPostgresUriWithDb(dbName: string): string;
5
+ declare function setupNewDatabase(dbName: string): Promise<void>;
6
+ export declare const PostgresExtension: {
7
+ startPostgresContainer: typeof startPostgresContainer;
8
+ stopPostgresContainer: typeof stopPostgresContainer;
9
+ getPostgresBaseUrl: typeof getPostgresBaseUrl;
10
+ getPostgresUriWithDb: typeof getPostgresUriWithDb;
11
+ setupNewDatabase: typeof setupNewDatabase;
12
+ };
13
+ export {};
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PostgresExtension = void 0;
13
+ const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
+ const postgresql_1 = require("@testcontainers/postgresql");
15
+ const pg_1 = require("pg");
16
+ function startPostgresContainer() {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ global.POSTGRES_CONTAINER = yield new postgresql_1.PostgreSqlContainer().start();
19
+ });
20
+ }
21
+ function stopPostgresContainer() {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ kiwi_js_1.KiwiPreconditions.checkState(global.POSTGRES_CONTAINER !== undefined, "Postgres container has not been previously started");
24
+ yield global.POSTGRES_CONTAINER.stop();
25
+ global.POSTGRES_CONTAINER = undefined;
26
+ });
27
+ }
28
+ function getPostgresBaseUrl() {
29
+ kiwi_js_1.KiwiPreconditions.checkState(global.POSTGRES_CONTAINER !== undefined, "Postgres container has not been previously started");
30
+ return global.POSTGRES_CONTAINER.getConnectionUri();
31
+ }
32
+ function getPostgresUriWithDb(dbName) {
33
+ return getPostgresBaseUrl().replace(/\/test$/, `/${dbName}`);
34
+ }
35
+ function setupNewDatabase(dbName) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const dbClient = new pg_1.Client({
38
+ connectionString: getPostgresBaseUrl(),
39
+ });
40
+ yield dbClient.connect();
41
+ yield dbClient.query(`create database ${dbName}`);
42
+ yield dbClient.end();
43
+ });
44
+ }
45
+ exports.PostgresExtension = {
46
+ startPostgresContainer,
47
+ stopPostgresContainer,
48
+ getPostgresBaseUrl,
49
+ getPostgresUriWithDb,
50
+ setupNewDatabase,
51
+ };
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@kiwiproject/kiwi-test-js",
3
+ "version": "0.1.0",
4
+ "description": "kiwi-test-js is a test utility library. Most of these utilities are ports from the Java Kiwi-test library",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "scripts": {
11
+ "format": "node node_modules/.bin/prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
12
+ "lint": "node node_modules/.bin/eslint --ext .js,.ts .",
13
+ "test": "node --experimental-vm-modules node_modules/.bin/jest --coverage --collectCoverageFrom=src/**/*.ts"
14
+ },
15
+ "jest": {
16
+ "testPathIgnorePatterns": [
17
+ "<rootDir>/__tests__/__utils__",
18
+ "<rootDir>/node_modules/"
19
+ ],
20
+ "coveragePathIgnorePatterns": [
21
+ "src/index.ts"
22
+ ]
23
+ },
24
+ "dependencies": {
25
+ "@jest/globals": "29.6.2",
26
+ "@kiwiproject/kiwi-js": "0.8.0",
27
+ "@testcontainers/elasticsearch": "10.2.1",
28
+ "@testcontainers/postgresql": "10.2.1",
29
+ "jest": "29.6.2",
30
+ "mongodb": "5.7.0",
31
+ "pg": "8.11.2",
32
+ "testcontainers": "10.2.1"
33
+ },
34
+ "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",
39
+ "@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"
46
+ }
47
+ }