Skip to content

Build your personal knowledge base with Trilium Notes

test(core): fix initialization issues due to SQL

Elian Doran3a7ce0c284c2
diff --git a/packages/trilium-core/src/migrations/0233__migrate_geo_map_to_collection.spec.ts b/packages/trilium-core/src/migrations/0233__migrate_geo_map_to_collection.spec.ts
index 6dfba236df..3251b9680b 100644
--- a/packages/trilium-core/src/migrations/0233__migrate_geo_map_to_collection.spec.ts
+++ b/packages/trilium-core/src/migrations/0233__migrate_geo_map_to_collection.spec.ts
@@ -19,9 +19,16 @@ import migration from "./0233__migrate_geo_map_to_collection.js";
* test data into the database, then verifies the migration transforms the data correctly.
*/
describe("Migration 0233: Migrate geoMap to collection", () => {
- const sql = getSql();
+ let sql: ReturnType<typeof getSql>;
beforeEach(async () => {
+ // getSql() is resolved here (not at describe-collection time) so that
+ // initializeCore() in the test setup's beforeAll has had a chance to
+ // run first. Capturing it eagerly at the top of describe crashes with
+ // "SQL not initialized" because describe callbacks run before any
+ // beforeAll hooks fire.
+ sql = getSql();
+
// Set up a clean in-memory database for each test
rebuildIntegrationTestDatabase();
diff --git a/packages/trilium-core/src/services/script.spec.ts b/packages/trilium-core/src/services/script.spec.ts
index 11b904d830..3002cda888 100644
--- a/packages/trilium-core/src/services/script.spec.ts
+++ b/packages/trilium-core/src/services/script.spec.ts
@@ -1,5 +1,5 @@
import { trimIndentation } from "@triliumnext/commons";
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import becca from "../becca/becca.js";
import BBranch from "../becca/entities/bbranch.js";
@@ -61,10 +61,18 @@ describe("Script", () => {
});
describe("dayjs in backend scripts", () => {
- const scriptNote = buildNote({
- type: "code",
- mime: "application/javascript;env=backend",
- content: ""
+ // buildNote() is called inside beforeAll (not at describe-collection
+ // time) so that the test setup's initializeCore() has run first.
+ // buildNote generates random IDs via getCrypto(), which crashes with
+ // "Crypto not initialized" if invoked during describe collection.
+ let scriptNote: ReturnType<typeof buildNote>;
+
+ beforeAll(() => {
+ scriptNote = buildNote({
+ type: "code",
+ mime: "application/javascript;env=backend",
+ content: ""
+ });
});
it("dayjs is available", () => {
diff --git a/packages/trilium-core/src/services/search/value_extractor.spec.ts b/packages/trilium-core/src/services/search/value_extractor.spec.ts
index 37f3d41ce3..d9828a86c6 100644
--- a/packages/trilium-core/src/services/search/value_extractor.spec.ts
+++ b/packages/trilium-core/src/services/search/value_extractor.spec.ts
@@ -1,10 +1,18 @@
-import { describe, it, expect, beforeEach } from "vitest";
+import { beforeAll, beforeEach, describe, expect, it } from "vitest";
import ValueExtractor from "./value_extractor.js";
import becca from "../../becca/becca.js";
import SearchContext from "./search_context.js";
import { note } from "../../test/becca_mocking.js";
-const dsc = new SearchContext();
+// SearchContext is constructed inside beforeAll (not at module load) so that
+// the test setup's initializeCore() has run first. Constructing it at module
+// top level crashes with "Context not initialized" because module evaluation
+// happens before any beforeAll hooks fire.
+let dsc: SearchContext;
+
+beforeAll(() => {
+ dsc = new SearchContext();
+});
describe("Value extractor", () => {
beforeEach(() => {