test(core): fix initialization issues due to SQL

This commit is contained in:
Elian Doran
2026-04-09 16:55:36 +03:00
parent dc0fcad843
commit 3a7ce0c284
3 changed files with 31 additions and 8 deletions

View File

@@ -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();

View File

@@ -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", () => {

View File

@@ -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(() => {