@kiwiproject/kiwi-test-js 0.3.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 +1 -0
- package/dist/index.js +3 -1
- package/dist/redis/redis-extension.d.ts +26 -0
- package/dist/redis/redis-extension.js +68 -0
- package/dist/waitForIt/waitForIt.d.ts +11 -0
- package/dist/waitForIt/waitForIt.js +44 -0
- package/package.json +18 -16
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; } });
|
|
@@ -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.
|
|
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.
|
|
25
|
+
"@elastic/elasticsearch": "8.10.0",
|
|
26
26
|
"@jest/globals": "29.7.0",
|
|
27
27
|
"@kiwiproject/kiwi-js": "0.8.0",
|
|
28
|
-
"@testcontainers/elasticsearch": "10.2
|
|
29
|
-
"@testcontainers/postgresql": "10.2
|
|
28
|
+
"@testcontainers/elasticsearch": "10.3.2",
|
|
29
|
+
"@testcontainers/postgresql": "10.3.2",
|
|
30
|
+
"convert": "4.14.0",
|
|
30
31
|
"jest": "29.7.0",
|
|
31
|
-
"mongodb": "6.
|
|
32
|
+
"mongodb": "6.3.0",
|
|
32
33
|
"pg": "8.11.3",
|
|
33
|
-
"
|
|
34
|
+
"redis": "4.6.11",
|
|
35
|
+
"testcontainers": "10.3.2"
|
|
34
36
|
},
|
|
35
37
|
"devDependencies": {
|
|
36
|
-
"@babel/core": "7.
|
|
37
|
-
"@babel/preset-env": "7.
|
|
38
|
-
"@babel/preset-typescript": "7.
|
|
39
|
-
"@types/node": "20.
|
|
40
|
-
"@types/pg": "8.10.
|
|
41
|
-
"@typescript-eslint/eslint-plugin": "6.
|
|
42
|
-
"@typescript-eslint/parser": "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",
|
|
43
45
|
"babel-jest": "29.7.0",
|
|
44
|
-
"eslint": "8.
|
|
45
|
-
"prettier": "3.0
|
|
46
|
-
"typescript": "5.
|
|
46
|
+
"eslint": "8.54.0",
|
|
47
|
+
"prettier": "3.1.0",
|
|
48
|
+
"typescript": "5.3.2"
|
|
47
49
|
}
|
|
48
50
|
}
|