@terreno/test 57.1.0 → 57.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.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @terreno/test
2
+
3
+ Shared Bun test helpers, MongoDB preload utilities, and HTTP fixtures for Terreno packages.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ bun add -d @terreno/test mongoose
9
+ ```
10
+
11
+ `mongoose` is a peer dependency (`>= 8.0.0`).
12
+
13
+ ## Quick start
14
+
15
+ In `bunfig.toml`:
16
+
17
+ ```toml
18
+ [test]
19
+ preload = ["./src/tests/bunSetup.ts"]
20
+ root = "./src"
21
+ ```
22
+
23
+ In `src/tests/bunSetup.ts`:
24
+
25
+ ```typescript
26
+ import {registerSimpleMongoPreload} from "@terreno/test";
27
+
28
+ process.env.TERRENO_TEST_USE_MEMORY_MONGO = "true";
29
+
30
+ registerSimpleMongoPreload({
31
+ testEnv: {
32
+ tokenIssuer: "my-package.test",
33
+ },
34
+ });
35
+ ```
36
+
37
+ Then run `bun test` in the package. The preload starts `mongodb-memory-server` when `TERRENO_TEST_MONGODB_URI` is unset. Set `TERRENO_TEST_MONGODB_URI` to use an external MongoDB. Set `BUN_TEST_DISABLE_DB=true` to skip database hooks.
38
+
39
+ ## What's included
40
+
41
+ - `registerSimpleMongoPreload` — connect-once in-memory or external Mongo
42
+ - `registerBackendPreload` — full lifecycle with optional transactions and fixture cache
43
+ - `setTerrenoTestEnv` — canonical auth secrets for tests
44
+ - `getBaseServer` / `authAsUser` — Express + supertest helpers
45
+ - Log silencing and Sentry mocks for quiet CI
46
+
47
+ ## Documentation
48
+
49
+ Full API reference: [docs/reference/test.md](https://github.com/flourishhealth/terreno/blob/master/docs/reference/test.md)
50
+
51
+ ## License and Contributing
52
+
53
+ Licensed under the [MIT License](https://github.com/flourishhealth/terreno/blob/master/LICENSE). See [CONTRIBUTING.md](https://github.com/flourishhealth/terreno/blob/master/CONTRIBUTING.md) for contribution guidelines.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var bun_test_1 = require("bun:test");
4
+ var setTerrenoTestEnv_1 = require("./setTerrenoTestEnv");
5
+ (0, bun_test_1.afterEach)(function () {
6
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)();
7
+ });
8
+ (0, bun_test_1.describe)("setTerrenoTestEnv", function () {
9
+ (0, bun_test_1.it)("applies extra keys and deletes undefined extra values", function () {
10
+ process.env.FEATURE_FLAGS_DEBUG = "true";
11
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)({
12
+ extra: { FEATURE_FLAGS_DEBUG: undefined, TRACE: "1" },
13
+ });
14
+ (0, bun_test_1.expect)(process.env.TRACE).toBe("1");
15
+ (0, bun_test_1.expect)(process.env.FEATURE_FLAGS_DEBUG).toBeUndefined();
16
+ });
17
+ (0, bun_test_1.it)("deletes listed keys after applying defaults", function () {
18
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)({
19
+ deleteKeys: ["TRACE"],
20
+ extra: { TRACE: "1" },
21
+ });
22
+ (0, bun_test_1.expect)(process.env.TRACE).toBeUndefined();
23
+ });
24
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var bun_test_1 = require("bun:test");
4
+ var setTerrenoTestEnv_1 = require("../env/setTerrenoTestEnv");
5
+ var setupEnvironment_1 = require("./setupEnvironment");
6
+ (0, bun_test_1.afterEach)(function () {
7
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)();
8
+ });
9
+ (0, bun_test_1.describe)("setupTestEnvironment", function () {
10
+ (0, bun_test_1.it)("throws when required auth secrets are missing", function () {
11
+ Reflect.deleteProperty(process.env, "TOKEN_ISSUER");
12
+ (0, bun_test_1.expect)(function () { return (0, setupEnvironment_1.setupTestEnvironment)(); }).toThrow(/TOKEN_ISSUER/);
13
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)();
14
+ Reflect.deleteProperty(process.env, "TOKEN_SECRET");
15
+ (0, bun_test_1.expect)(function () { return (0, setupEnvironment_1.setupTestEnvironment)(); }).toThrow(/TOKEN_SECRET/);
16
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)();
17
+ Reflect.deleteProperty(process.env, "REFRESH_TOKEN_SECRET");
18
+ (0, bun_test_1.expect)(function () { return (0, setupEnvironment_1.setupTestEnvironment)(); }).toThrow(/REFRESH_TOKEN_SECRET/);
19
+ (0, setTerrenoTestEnv_1.setTerrenoTestEnv)();
20
+ Reflect.deleteProperty(process.env, "SESSION_SECRET");
21
+ (0, bun_test_1.expect)(function () { return (0, setupEnvironment_1.setupTestEnvironment)(); }).toThrow(/SESSION_SECRET/);
22
+ });
23
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var bun_test_1 = require("bun:test");
4
+ var testLogger_1 = require("./testLogger");
5
+ var originalDebug = process.env.DEBUG_MONGO_PRELOAD;
6
+ var originalShow = process.env.SHOW_ALL_TEST_LOGS;
7
+ (0, bun_test_1.afterEach)(function () {
8
+ if (originalDebug === undefined) {
9
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
10
+ }
11
+ else {
12
+ process.env.DEBUG_MONGO_PRELOAD = originalDebug;
13
+ }
14
+ if (originalShow === undefined) {
15
+ Reflect.deleteProperty(process.env, "SHOW_ALL_TEST_LOGS");
16
+ }
17
+ else {
18
+ process.env.SHOW_ALL_TEST_LOGS = originalShow;
19
+ }
20
+ });
21
+ (0, bun_test_1.describe)("testLogger", function () {
22
+ (0, bun_test_1.it)("always logs info and warn", function () {
23
+ var info = (0, bun_test_1.spyOn)(console, "info").mockImplementation(function () { });
24
+ var warn = (0, bun_test_1.spyOn)(console, "warn").mockImplementation(function () { });
25
+ testLogger_1.testLogger.info("hello");
26
+ testLogger_1.testLogger.warn("careful");
27
+ (0, bun_test_1.expect)(info.mock.calls.length).toBeGreaterThan(0);
28
+ (0, bun_test_1.expect)(warn.mock.calls.length).toBeGreaterThan(0);
29
+ info.mockRestore();
30
+ warn.mockRestore();
31
+ });
32
+ (0, bun_test_1.it)("debugs only when DEBUG_MONGO_PRELOAD is true", function () {
33
+ var debug = (0, bun_test_1.spyOn)(console, "debug").mockImplementation(function () { });
34
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
35
+ testLogger_1.testLogger.debug("quiet");
36
+ (0, bun_test_1.expect)(debug.mock.calls.length).toBe(0);
37
+ process.env.DEBUG_MONGO_PRELOAD = "true";
38
+ testLogger_1.testLogger.debug("loud");
39
+ (0, bun_test_1.expect)(debug.mock.calls.length).toBe(1);
40
+ debug.mockRestore();
41
+ });
42
+ (0, bun_test_1.it)("catches errors when debug or show-all logs are enabled", function () {
43
+ var error = (0, bun_test_1.spyOn)(console, "error").mockImplementation(function () { });
44
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
45
+ Reflect.deleteProperty(process.env, "SHOW_ALL_TEST_LOGS");
46
+ testLogger_1.testLogger.catch(new Error("silent"));
47
+ (0, bun_test_1.expect)(error.mock.calls.length).toBe(0);
48
+ process.env.SHOW_ALL_TEST_LOGS = "true";
49
+ testLogger_1.testLogger.catch(new Error("shown"));
50
+ (0, bun_test_1.expect)(error.mock.calls.length).toBe(1);
51
+ process.env.DEBUG_MONGO_PRELOAD = "true";
52
+ testLogger_1.testLogger.catch("also");
53
+ (0, bun_test_1.expect)(error.mock.calls.length).toBe(2);
54
+ error.mockRestore();
55
+ });
56
+ });
package/package.json CHANGED
@@ -10,10 +10,10 @@
10
10
  "qs": "^6.15.0",
11
11
  "winston": "^3.18.3"
12
12
  },
13
- "description": "Shared Bun test helpers, MongoDB preload utilities, and HTTP fixtures for Terreno packages",
13
+ "description": "Shared Bun test helpers, MongoDB preload utilities, and HTTP fixtures — part of Terreno, the batteries-included TypeScript framework for universal apps.",
14
14
  "devDependencies": {
15
15
  "@biomejs/biome": "^2.3.6",
16
- "@types/bun": "^1.3.8",
16
+ "@types/bun": "^1.4.0",
17
17
  "@types/express": "^5.0.6",
18
18
  "@types/luxon": "^3.7.1",
19
19
  "@types/supertest": "^7.2.0",
@@ -64,5 +64,5 @@
64
64
  "test:ci": "bun test"
65
65
  },
66
66
  "types": "dist/index.d.ts",
67
- "version": "57.1.0"
67
+ "version": "57.3.0"
68
68
  }
@@ -0,0 +1,26 @@
1
+ import {afterEach, describe, expect, it} from "bun:test";
2
+
3
+ import {setTerrenoTestEnv} from "./setTerrenoTestEnv";
4
+
5
+ afterEach(() => {
6
+ setTerrenoTestEnv();
7
+ });
8
+
9
+ describe("setTerrenoTestEnv", () => {
10
+ it("applies extra keys and deletes undefined extra values", () => {
11
+ process.env.FEATURE_FLAGS_DEBUG = "true";
12
+ setTerrenoTestEnv({
13
+ extra: {FEATURE_FLAGS_DEBUG: undefined, TRACE: "1"},
14
+ });
15
+ expect(process.env.TRACE).toBe("1");
16
+ expect(process.env.FEATURE_FLAGS_DEBUG).toBeUndefined();
17
+ });
18
+
19
+ it("deletes listed keys after applying defaults", () => {
20
+ setTerrenoTestEnv({
21
+ deleteKeys: ["TRACE"],
22
+ extra: {TRACE: "1"},
23
+ });
24
+ expect(process.env.TRACE).toBeUndefined();
25
+ });
26
+ });
@@ -0,0 +1,24 @@
1
+ import {afterEach, describe, expect, it} from "bun:test";
2
+
3
+ import {setTerrenoTestEnv} from "../env/setTerrenoTestEnv";
4
+ import {setupTestEnvironment} from "./setupEnvironment";
5
+
6
+ afterEach(() => {
7
+ setTerrenoTestEnv();
8
+ });
9
+
10
+ describe("setupTestEnvironment", () => {
11
+ it("throws when required auth secrets are missing", () => {
12
+ Reflect.deleteProperty(process.env, "TOKEN_ISSUER");
13
+ expect(() => setupTestEnvironment()).toThrow(/TOKEN_ISSUER/);
14
+ setTerrenoTestEnv();
15
+ Reflect.deleteProperty(process.env, "TOKEN_SECRET");
16
+ expect(() => setupTestEnvironment()).toThrow(/TOKEN_SECRET/);
17
+ setTerrenoTestEnv();
18
+ Reflect.deleteProperty(process.env, "REFRESH_TOKEN_SECRET");
19
+ expect(() => setupTestEnvironment()).toThrow(/REFRESH_TOKEN_SECRET/);
20
+ setTerrenoTestEnv();
21
+ Reflect.deleteProperty(process.env, "SESSION_SECRET");
22
+ expect(() => setupTestEnvironment()).toThrow(/SESSION_SECRET/);
23
+ });
24
+ });
@@ -0,0 +1,58 @@
1
+ import {afterEach, describe, expect, it, spyOn} from "bun:test";
2
+
3
+ import {testLogger} from "./testLogger";
4
+
5
+ const originalDebug = process.env.DEBUG_MONGO_PRELOAD;
6
+ const originalShow = process.env.SHOW_ALL_TEST_LOGS;
7
+
8
+ afterEach(() => {
9
+ if (originalDebug === undefined) {
10
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
11
+ } else {
12
+ process.env.DEBUG_MONGO_PRELOAD = originalDebug;
13
+ }
14
+ if (originalShow === undefined) {
15
+ Reflect.deleteProperty(process.env, "SHOW_ALL_TEST_LOGS");
16
+ } else {
17
+ process.env.SHOW_ALL_TEST_LOGS = originalShow;
18
+ }
19
+ });
20
+
21
+ describe("testLogger", () => {
22
+ it("always logs info and warn", () => {
23
+ const info = spyOn(console, "info").mockImplementation(() => {});
24
+ const warn = spyOn(console, "warn").mockImplementation(() => {});
25
+ testLogger.info("hello");
26
+ testLogger.warn("careful");
27
+ expect(info.mock.calls.length).toBeGreaterThan(0);
28
+ expect(warn.mock.calls.length).toBeGreaterThan(0);
29
+ info.mockRestore();
30
+ warn.mockRestore();
31
+ });
32
+
33
+ it("debugs only when DEBUG_MONGO_PRELOAD is true", () => {
34
+ const debug = spyOn(console, "debug").mockImplementation(() => {});
35
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
36
+ testLogger.debug("quiet");
37
+ expect(debug.mock.calls.length).toBe(0);
38
+ process.env.DEBUG_MONGO_PRELOAD = "true";
39
+ testLogger.debug("loud");
40
+ expect(debug.mock.calls.length).toBe(1);
41
+ debug.mockRestore();
42
+ });
43
+
44
+ it("catches errors when debug or show-all logs are enabled", () => {
45
+ const error = spyOn(console, "error").mockImplementation(() => {});
46
+ Reflect.deleteProperty(process.env, "DEBUG_MONGO_PRELOAD");
47
+ Reflect.deleteProperty(process.env, "SHOW_ALL_TEST_LOGS");
48
+ testLogger.catch(new Error("silent"));
49
+ expect(error.mock.calls.length).toBe(0);
50
+ process.env.SHOW_ALL_TEST_LOGS = "true";
51
+ testLogger.catch(new Error("shown"));
52
+ expect(error.mock.calls.length).toBe(1);
53
+ process.env.DEBUG_MONGO_PRELOAD = "true";
54
+ testLogger.catch("also");
55
+ expect(error.mock.calls.length).toBe(2);
56
+ error.mockRestore();
57
+ });
58
+ });