Skip to content

Commit 5fbee5a

Browse files
new-test-setup-poc
1 parent 2fc58da commit 5fbee5a

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"devDependencies": {
3232
"@babel/core": "7.26.9",
3333
"@babel/preset-env": "7.26.9",
34+
"@mongodb-js/mongodb-downloader": "^0.3.9",
3435
"@typescript-eslint/eslint-plugin": "^8.19.1",
3536
"@typescript-eslint/parser": "^8.19.1",
3637
"acquit": "1.3.0",
@@ -58,6 +59,7 @@
5859
"mocha": "11.1.0",
5960
"moment": "2.30.1",
6061
"mongodb-memory-server": "10.1.4",
62+
"mongodb-runner": "^5.8.2",
6163
"ncp": "^2.0.0",
6264
"nyc": "15.1.0",
6365
"pug": "3.0.3",
@@ -104,7 +106,7 @@
104106
"test-deno": "deno run --allow-env --allow-read --allow-net --allow-run --allow-sys --allow-write ./test/deno.js",
105107
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
106108
"test-tsd": "node ./test/types/check-types-filename && tsd",
107-
"setup-test-encryption": "bash scripts/configure-cluster-with-encryption.sh",
109+
"setup-test-encryption": "node scripts/setup-encryption-tests.js",
108110
"test-encryption": "mocha --exit ./test/encryption/*.test.js",
109111
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
110112
"test-coverage": "nyc --reporter=html --reporter=text npm test",
@@ -146,4 +148,4 @@
146148
"target": "ES2017"
147149
}
148150
}
149-
}
151+
}

test/encryption/encryption.test.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ const fs = require('fs');
99
const mongoose = require('../../lib');
1010
const { Map } = require('../../lib/types');
1111

12+
const { stop, start } = require('../../scripts/setup-encryption-tests');
13+
1214
const LOCAL_KEY = Buffer.from('Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk', 'base64');
1315

16+
const exists = path => {
17+
try {
18+
fs.statSync(path);
19+
return true;
20+
} catch {
21+
return false;
22+
}
23+
};
24+
1425
/**
1526
* @param {object} object
1627
* @param {string} property
@@ -25,19 +36,29 @@ describe('encryption integration tests', () => {
2536
const cachedUri = process.env.MONGOOSE_TEST_URI;
2637
const cachedLib = process.env.CRYPT_SHARED_LIB_PATH;
2738

28-
before(function() {
39+
before(async function() {
40+
this.timeout(0);
2941
const cwd = process.cwd();
30-
const file = fs.readFileSync(cwd + '/data/mo-expansion.yml', { encoding: 'utf-8' }).trim().split('\n');
31-
32-
// matches `key="value"` and extracts key and value.
33-
const regex = /^(?<key>.*): "(?<value>.*)"$/;
34-
const variables = Object.fromEntries(file.map((line) => regex.exec(line.trim()).groups).map(({ key, value }) => [key, value]));
35-
36-
process.env.CRYPT_SHARED_LIB_PATH ??= variables.CRYPT_SHARED_LIB_PATH;
37-
process.env.MONGOOSE_TEST_URI ??= variables.MONGODB_URI;
42+
if (!exists(cwd + '/mo-expansion.yml')) {
43+
const { uri, cryptShared } = await start();
44+
process.env.CRYPT_SHARED_LIB_PATH = cryptShared;
45+
process.env.MONGOOSE_TEST_URI = uri;
46+
} else {
47+
const file = fs.readFileSync(cwd + '/mo-expansion.yml', { encoding: 'utf-8' }).trim().split('\n');
48+
49+
const regex = /^(?<key>.*): "(?<value>.*)"$/;
50+
const variables = Object.fromEntries(file.map((line) => regex.exec(line.trim()).groups).map(({ key, value }) => [key, value]));
51+
52+
process.env.CRYPT_SHARED_LIB_PATH = variables.CRYPT_SHARED_LIB_PATH;
53+
process.env.MONGOOSE_TEST_URI = variables.MONGODB_URI;
54+
}
3855
});
3956

40-
after(function() {
57+
after(async function() {
58+
if (!exists(process.cwd() + '/mo-expansion.yml')) {
59+
await stop();
60+
}
61+
4162
process.env.CRYPT_SHARED_LIB_PATH = cachedLib;
4263
process.env.MONGOOSE_TEST_URI = cachedUri;
4364
});

0 commit comments

Comments
 (0)