@kiwiproject/kiwi-test-js 0.2.0 → 0.4.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/dist/index.d.ts CHANGED
@@ -2,3 +2,4 @@ export { MongoExtension } from "./mongo/mongo-extension";
2
2
  export { PostgresExtension } from "./postgres/postgres-extension";
3
3
  export { ElasticSearchExtension } from "./elasticsearch/elastic-search-extension";
4
4
  export { MinioExtension } from "./minio/minio-extension";
5
+ export { wait } from "./waitForIt/waitForIt";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MinioExtension = exports.ElasticSearchExtension = exports.PostgresExtension = exports.MongoExtension = void 0;
3
+ exports.wait = exports.MinioExtension = exports.ElasticSearchExtension = exports.PostgresExtension = exports.MongoExtension = void 0;
4
4
  var mongo_extension_1 = require("./mongo/mongo-extension");
5
5
  Object.defineProperty(exports, "MongoExtension", { enumerable: true, get: function () { return mongo_extension_1.MongoExtension; } });
6
6
  var postgres_extension_1 = require("./postgres/postgres-extension");
@@ -9,3 +9,5 @@ var elastic_search_extension_1 = require("./elasticsearch/elastic-search-extensi
9
9
  Object.defineProperty(exports, "ElasticSearchExtension", { enumerable: true, get: function () { return elastic_search_extension_1.ElasticSearchExtension; } });
10
10
  var minio_extension_1 = require("./minio/minio-extension");
11
11
  Object.defineProperty(exports, "MinioExtension", { enumerable: true, get: function () { return minio_extension_1.MinioExtension; } });
12
+ var waitForIt_1 = require("./waitForIt/waitForIt");
13
+ Object.defineProperty(exports, "wait", { enumerable: true, get: function () { return waitForIt_1.wait; } });
@@ -12,15 +12,25 @@ declare function startMinioContainer(accessKey?: string, secretKey?: string, ima
12
12
  */
13
13
  declare function stopMinioContainer(): Promise<void>;
14
14
  declare function setMinioPort(port: number): void;
15
+ declare function setMinioHost(host: string): void;
15
16
  /**
16
17
  * Retrieves the mapped external port of the started Minio container. Error will be thrown if startMinioContainer was
17
18
  * not previously called.
18
19
  */
19
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;
20
28
  export declare const MinioExtension: {
21
29
  startMinioContainer: typeof startMinioContainer;
22
30
  stopMinioContainer: typeof stopMinioContainer;
23
31
  setMinioPort: typeof setMinioPort;
24
32
  getMinioPort: typeof getMinioPort;
33
+ getMinioHost: typeof getMinioHost;
34
+ setMinioHost: typeof setMinioHost;
25
35
  };
26
36
  export {};
@@ -33,6 +33,7 @@ function startMinioContainer(accessKey = "minioadmin", secretKey = "keyboard cat
33
33
  .withCommand(["server", "/data"])
34
34
  .start();
35
35
  setMinioPort(container.getMappedPort(9000));
36
+ setMinioHost(container.getHost());
36
37
  // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
37
38
  global.MINIO_CONTAINER = container;
38
39
  });
@@ -47,11 +48,15 @@ function stopMinioContainer() {
47
48
  yield global.MINIO_CONTAINER.stop();
48
49
  global.MINIO_CONTAINER = undefined;
49
50
  delete process.env.MINIO_EXTENSION_PORT;
51
+ delete process.env.MINIO_EXTENSION_HOST;
50
52
  });
51
53
  }
52
54
  function setMinioPort(port) {
53
55
  process.env.MINIO_EXTENSION_PORT = String(port);
54
56
  }
57
+ function setMinioHost(host) {
58
+ process.env.MINIO_EXTENSION_HOST = host;
59
+ }
55
60
  /**
56
61
  * Retrieves the mapped external port of the started Minio container. Error will be thrown if startMinioContainer was
57
62
  * not previously called.
@@ -60,9 +65,21 @@ function getMinioPort() {
60
65
  kiwi_js_1.KiwiPreconditions.checkState(process.env.MINIO_EXTENSION_PORT !== undefined, "Minio container has not been previously started");
61
66
  return parseInt(process.env.MINIO_EXTENSION_PORT, 10);
62
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;
77
+ }
63
78
  exports.MinioExtension = {
64
79
  startMinioContainer,
65
80
  stopMinioContainer,
66
81
  setMinioPort,
67
82
  getMinioPort,
83
+ getMinioHost,
84
+ setMinioHost,
68
85
  };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Starts a Redis container and stores the container information in global.REDIS_CONTAINER.
3
+ *
4
+ * @param image The image name/version to use for redis. Defaults to redis:7.
5
+ */
6
+ declare function startRedisContainer(image?: string): Promise<void>;
7
+ /**
8
+ * Stops a previously started Redis container. Error will be thrown if startRedisContainer is not
9
+ * previously called.
10
+ */
11
+ declare function stopRedisContainer(): Promise<void>;
12
+ declare function setRedisBaseUrl(host: string, port: number): void;
13
+ /**
14
+ * Retrieves the base connection URL for accessing the running Redis. Error will be thrown if startRedisContainer
15
+ * is not previously called.
16
+ */
17
+ declare function getRedisBaseUrl(): string;
18
+ declare function flushDatabase(): Promise<void>;
19
+ export declare const RedisExtension: {
20
+ startRedisContainer: typeof startRedisContainer;
21
+ stopRedisContainer: typeof stopRedisContainer;
22
+ setRedisBaseUrl: typeof setRedisBaseUrl;
23
+ getRedisBaseUrl: typeof getRedisBaseUrl;
24
+ flushDatabase: typeof flushDatabase;
25
+ };
26
+ export {};
@@ -0,0 +1,68 @@
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.RedisExtension = void 0;
13
+ const kiwi_js_1 = require("@kiwiproject/kiwi-js");
14
+ const testcontainers_1 = require("testcontainers");
15
+ const redis_1 = require("redis");
16
+ /**
17
+ * Starts a Redis container and stores the container information in global.REDIS_CONTAINER.
18
+ *
19
+ * @param image The image name/version to use for redis. Defaults to redis:7.
20
+ */
21
+ function startRedisContainer(image = "redis:7") {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const container = yield new testcontainers_1.GenericContainer(image)
24
+ .withExposedPorts(6379)
25
+ .start();
26
+ setRedisBaseUrl(container.getHost(), container.getMappedPort(6379));
27
+ // NOTE: This will only work if tests are runInBand (i.e. not in parallel)
28
+ global.REDIS_CONTAINER = container;
29
+ });
30
+ }
31
+ /**
32
+ * Stops a previously started Redis container. Error will be thrown if startRedisContainer is not
33
+ * previously called.
34
+ */
35
+ function stopRedisContainer() {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ kiwi_js_1.KiwiPreconditions.checkState(global.REDIS_CONTAINER !== undefined, "Redis container has not been previously started or is not running in band");
38
+ yield global.REDIS_CONTAINER.stop();
39
+ global.REDIS_CONTAINER = undefined;
40
+ delete process.env.REDIS_EXTENSION_BASE_URI;
41
+ });
42
+ }
43
+ function setRedisBaseUrl(host, port) {
44
+ process.env.REDIS_EXTENSION_BASE_URI = `redis://${host}:${port}/`;
45
+ }
46
+ /**
47
+ * Retrieves the base connection URL for accessing the running Redis. Error will be thrown if startRedisContainer
48
+ * is not previously called.
49
+ */
50
+ function getRedisBaseUrl() {
51
+ kiwi_js_1.KiwiPreconditions.checkState(process.env.REDIS_EXTENSION_BASE_URI !== undefined, "Redis container has not been previously started");
52
+ return process.env.REDIS_EXTENSION_BASE_URI;
53
+ }
54
+ function flushDatabase() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const client = (0, redis_1.createClient)({ url: getRedisBaseUrl() }).on("error", (err) => console.log("Redis error", err));
57
+ yield client.connect();
58
+ yield client.flushDb();
59
+ yield client.quit();
60
+ });
61
+ }
62
+ exports.RedisExtension = {
63
+ startRedisContainer,
64
+ stopRedisContainer,
65
+ setRedisBaseUrl,
66
+ getRedisBaseUrl,
67
+ flushDatabase,
68
+ };
@@ -0,0 +1,11 @@
1
+ import { Time } from "convert";
2
+ export declare function wait(): WaitFor;
3
+ declare class WaitFor {
4
+ private timeoutMs;
5
+ private checkIntervalMs;
6
+ constructor();
7
+ atMost(time: number, units: Time): this;
8
+ checkEvery(time: number, units: Time): this;
9
+ until(cb: () => boolean): Promise<string>;
10
+ }
11
+ export {};
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.wait = void 0;
7
+ const convert_1 = __importDefault(require("convert"));
8
+ function wait() {
9
+ return new WaitFor();
10
+ }
11
+ exports.wait = wait;
12
+ class WaitFor {
13
+ constructor() {
14
+ this.timeoutMs = 500;
15
+ this.checkIntervalMs = 100;
16
+ }
17
+ atMost(time, units) {
18
+ this.timeoutMs = (0, convert_1.default)(time, units).to("ms");
19
+ return this;
20
+ }
21
+ checkEvery(time, units) {
22
+ this.checkIntervalMs = (0, convert_1.default)(time, units).to("ms");
23
+ return this;
24
+ }
25
+ until(cb) {
26
+ const totalTries = this.timeoutMs / this.checkIntervalMs;
27
+ return new Promise((resolve, reject) => {
28
+ let tries = 1;
29
+ const loop = () => {
30
+ if (cb.apply(this)) {
31
+ resolve(`Condition met after ${tries} of ${totalTries} tries`);
32
+ }
33
+ else if (tries > totalTries) {
34
+ reject(new Error(`Condition was not met after ${totalTries} tries`));
35
+ }
36
+ else {
37
+ tries += 1;
38
+ setTimeout(loop, this.checkIntervalMs);
39
+ }
40
+ };
41
+ loop();
42
+ });
43
+ }
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiwiproject/kiwi-test-js",
3
- "version": "0.2.0",
3
+ "version": "0.4.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,27 +22,29 @@
22
22
  ]
23
23
  },
24
24
  "dependencies": {
25
- "@elastic/elasticsearch": "8.8.1",
26
- "@jest/globals": "29.6.4",
25
+ "@elastic/elasticsearch": "8.10.0",
26
+ "@jest/globals": "29.7.0",
27
27
  "@kiwiproject/kiwi-js": "0.8.0",
28
- "@testcontainers/elasticsearch": "10.2.1",
29
- "@testcontainers/postgresql": "10.2.1",
30
- "jest": "29.6.4",
31
- "mongodb": "5.8.1",
28
+ "@testcontainers/elasticsearch": "10.3.2",
29
+ "@testcontainers/postgresql": "10.3.2",
30
+ "convert": "4.14.0",
31
+ "jest": "29.7.0",
32
+ "mongodb": "6.3.0",
32
33
  "pg": "8.11.3",
33
- "testcontainers": "10.2.1"
34
+ "redis": "4.6.11",
35
+ "testcontainers": "10.3.2"
34
36
  },
35
37
  "devDependencies": {
36
- "@babel/core": "7.22.10",
37
- "@babel/preset-env": "7.22.10",
38
- "@babel/preset-typescript": "7.22.5",
39
- "@types/node": "20.5.4",
40
- "@types/pg": "8.10.2",
41
- "@typescript-eslint/eslint-plugin": "6.4.1",
42
- "@typescript-eslint/parser": "6.4.1",
43
- "babel-jest": "29.6.3",
44
- "eslint": "8.47.0",
45
- "prettier": "3.0.2",
46
- "typescript": "5.1.6"
38
+ "@babel/core": "7.23.3",
39
+ "@babel/preset-env": "7.23.3",
40
+ "@babel/preset-typescript": "7.23.3",
41
+ "@types/node": "20.10.0",
42
+ "@types/pg": "8.10.9",
43
+ "@typescript-eslint/eslint-plugin": "6.13.1",
44
+ "@typescript-eslint/parser": "6.13.0",
45
+ "babel-jest": "29.7.0",
46
+ "eslint": "8.54.0",
47
+ "prettier": "3.1.0",
48
+ "typescript": "5.3.2"
47
49
  }
48
50
  }