@fragno-dev/test 0.1.1

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.
@@ -0,0 +1,15 @@
1
+
2
+ > @fragno-dev/test@0.1.1 build /home/runner/work/fragno/fragno/packages/fragno-test
3
+ > tsdown
4
+
5
+ ℹ tsdown v0.15.10 powered by rolldown v1.0.0-beta.44
6
+ ℹ Using tsdown config: /home/runner/work/fragno/fragno/packages/fragno-test/tsdown.config.ts
7
+ ℹ entry: src/index.ts
8
+ ℹ tsconfig: tsconfig.json
9
+ ℹ Build start
10
+ ℹ dist/index.js 1.64 kB │ gzip: 0.67 kB
11
+ ℹ dist/index.js.map 5.90 kB │ gzip: 1.93 kB
12
+ ℹ dist/index.d.ts.map 1.21 kB │ gzip: 0.57 kB
13
+ ℹ dist/index.d.ts 2.29 kB │ gzip: 0.68 kB
14
+ ℹ 4 files, total: 11.04 kB
15
+ ✔ Build complete in 11624ms
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @fragno-dev/test
2
+
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 711226d: feat(testing): add `createDatabaseFragmentForTest` in new test package that automatically
8
+ sets up a Fragment's database and makes it ready for testing
9
+ - Updated dependencies [8b2859c]
10
+ - Updated dependencies [bef9f6c]
11
+ - Updated dependencies [711226d]
12
+ - @fragno-dev/db@0.1.5
13
+ - @fragno-dev/core@0.1.3
package/LICENSE.md ADDED
@@ -0,0 +1,16 @@
1
+ Copyright 2025 - present "ReJot Nederland B.V.", and individual contributors.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
4
+ associated documentation files (the “Software”), to deal in the Software without restriction,
5
+ including without limitation the rights to use, copy, modify, merge, publish, distribute,
6
+ sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7
+ furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial
10
+ portions of the Software.
11
+
12
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
13
+ NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
15
+ OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ import { Kysely } from "kysely";
2
+ import { CreateFragmentForTestOptions, CreateFragmentForTestOptions as CreateFragmentForTestOptions$1, FragmentForTest, FragmentForTest as FragmentForTest$1, InitRoutesOverrides, RouteHandlerInputOptions, TestResponse, createFragmentForTest } from "@fragno-dev/core/test";
3
+ import { AnySchema } from "@fragno-dev/db/schema";
4
+ import { DatabaseAdapter } from "@fragno-dev/db/adapters";
5
+ import { FragnoPublicConfig } from "@fragno-dev/core/api/fragment-instantiation";
6
+ import { FragmentDefinition } from "@fragno-dev/core/api/fragment-builder";
7
+
8
+ //#region src/index.d.ts
9
+
10
+ /**
11
+ * Options for creating a database fragment for testing
12
+ */
13
+ interface CreateDatabaseFragmentForTestOptions<TConfig, TDeps, TServices, TAdditionalContext extends Record<string, unknown>, TOptions extends FragnoPublicConfig> extends Omit<CreateFragmentForTestOptions$1<TConfig, TDeps, TServices, TAdditionalContext, TOptions>, "config"> {
14
+ databasePath?: string;
15
+ migrateToVersion?: number;
16
+ config?: TConfig;
17
+ }
18
+ /**
19
+ * Extended fragment test instance with database adapter and Kysely instance
20
+ */
21
+ interface DatabaseFragmentForTest<TConfig, TDeps, TServices, TAdditionalContext extends Record<string, unknown>, TOptions extends FragnoPublicConfig> extends FragmentForTest$1<TConfig, TDeps, TServices, TAdditionalContext, TOptions> {
22
+ kysely: Kysely<any>;
23
+ adapter: DatabaseAdapter<any>;
24
+ }
25
+ declare function createDatabaseFragmentForTest<const TConfig, const TDeps, const TServices extends Record<string, unknown>, const TAdditionalContext extends Record<string, unknown>, const TOptions extends FragnoPublicConfig, const TSchema extends AnySchema>(fragmentBuilder: {
26
+ definition: FragmentDefinition<TConfig, TDeps, TServices, TAdditionalContext>;
27
+ $requiredOptions: TOptions;
28
+ }, options?: CreateDatabaseFragmentForTestOptions<TConfig, TDeps, TServices, TAdditionalContext, TOptions>): Promise<DatabaseFragmentForTest<TConfig, TDeps, TServices, TAdditionalContext, TOptions>>;
29
+ //#endregion
30
+ export { CreateDatabaseFragmentForTestOptions, type CreateFragmentForTestOptions, DatabaseFragmentForTest, type FragmentForTest, type InitRoutesOverrides, type RouteHandlerInputOptions, type TestResponse, createDatabaseFragmentForTest, createFragmentForTest };
31
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;AA0BA;;AAKmB,UALF,oCAKE,CAAA,OAAA,EAAA,KAAA,EAAA,SAAA,EAAA,2BADU,MACV,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,iBAAA,kBAAA,CAAA,SACT,IADS,CAEf,8BAFe,CAEc,OAFd,EAEuB,KAFvB,EAE8B,SAF9B,EAEyC,kBAFzC,EAE6D,QAF7D,CAAA,EAAA,QAAA,CAAA,CAAA;EAEc,YAAA,CAAA,EAAA,MAAA;EAAS,gBAAA,CAAA,EAAA,MAAA;EAAO,MAAA,CAAA,EAKtC,OALsC;;;;;AADvC,UAYO,uBAZP,CAAA,OAAA,EAAA,KAAA,EAAA,SAAA,EAAA,2BAgBmB,MAhBnB,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,iBAiBS,kBAjBT,CAAA,SAkBA,iBAlBA,CAkBgB,OAlBhB,EAkByB,KAlBzB,EAkBgC,SAlBhC,EAkB2C,kBAlB3C,EAkB+D,QAlB/D,CAAA,CAAA;EAAI,MAAA,EAoBJ,MApBI,CAAA,GAAA,CAAA;EAYG,OAAA,EAUN,eAVM,CAAA,GAAuB,CAAA;;AAKrB,iBAQG,6BARH,CAAA,aAAA,EAAA,WAAA,EAAA,wBAWO,MAXP,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,iCAYgB,MAZhB,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,uBAaM,kBAbN,EAAA,sBAcK,SAdL,CAAA,CAAA,eAAA,EAAA;EACO,UAAA,EAgBV,kBAhBU,CAgBS,OAhBT,EAgBkB,KAhBlB,EAgByB,SAhBzB,EAgBoC,kBAhBpC,CAAA;EAAS,gBAAA,EAiBb,QAjBa;CAAO,EAAA,OAAA,CAAA,EAmB9B,oCAnB8B,CAoBtC,OApBsC,EAqBtC,KArBsC,EAsBtC,SAtBsC,EAuBtC,kBAvBsC,EAwBtC,QAxBsC,CAAA,CAAA,EA0BvC,OA1BuC,CA0B/B,uBA1B+B,CA0BP,OA1BO,EA0BE,KA1BF,EA0BS,SA1BT,EA0BoB,kBA1BpB,EA0BwC,QA1BxC,CAAA,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import { Kysely } from "kysely";
2
+ import { SQLocalKysely } from "sqlocal/kysely";
3
+ import { KyselyAdapter } from "@fragno-dev/db/adapters/kysely";
4
+ import { createFragmentForTest, createFragmentForTest as createFragmentForTest$1 } from "@fragno-dev/core/test";
5
+
6
+ //#region src/index.ts
7
+ async function createDatabaseFragmentForTest(fragmentBuilder, options) {
8
+ const { databasePath = ":memory:", migrateToVersion, config, options: fragmentOptions, deps, services, additionalContext } = options ?? {};
9
+ const { dialect } = new SQLocalKysely(databasePath);
10
+ const kysely = new Kysely({ dialect });
11
+ const adapter = new KyselyAdapter({
12
+ db: kysely,
13
+ provider: "sqlite"
14
+ });
15
+ const fragmentAdditionalContext = fragmentBuilder.definition.additionalContext;
16
+ const schema = fragmentAdditionalContext?.databaseSchema;
17
+ const namespace = fragmentAdditionalContext?.databaseNamespace ?? "";
18
+ if (!schema) throw new Error(`Fragment '${fragmentBuilder.definition.name}' does not have a database schema. Make sure you're using defineFragmentWithDatabase().withDatabase(schema).`);
19
+ const migrator = adapter.createMigrationEngine(schema, namespace);
20
+ await (migrateToVersion ? await migrator.prepareMigrationTo(migrateToVersion, { updateSettings: false }) : await migrator.prepareMigration({ updateSettings: false })).execute();
21
+ return {
22
+ ...createFragmentForTest$1(fragmentBuilder, {
23
+ config,
24
+ options: {
25
+ ...fragmentOptions,
26
+ databaseAdapter: adapter
27
+ },
28
+ deps,
29
+ services,
30
+ additionalContext
31
+ }),
32
+ kysely,
33
+ adapter
34
+ };
35
+ }
36
+
37
+ //#endregion
38
+ export { createDatabaseFragmentForTest, createFragmentForTest };
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["createFragmentForTest"],"sources":["../src/index.ts"],"sourcesContent":["import { Kysely } from \"kysely\";\nimport { SQLocalKysely } from \"sqlocal/kysely\";\nimport { KyselyAdapter } from \"@fragno-dev/db/adapters/kysely\";\nimport type { AnySchema } from \"@fragno-dev/db/schema\";\nimport type { DatabaseAdapter } from \"@fragno-dev/db/adapters\";\nimport {\n createFragmentForTest,\n type FragmentForTest,\n type CreateFragmentForTestOptions,\n} from \"@fragno-dev/core/test\";\nimport type { FragnoPublicConfig } from \"@fragno-dev/core/api/fragment-instantiation\";\nimport type { FragmentDefinition } from \"@fragno-dev/core/api/fragment-builder\";\n\n// Re-export utilities from @fragno-dev/core/test\nexport {\n createFragmentForTest,\n type TestResponse,\n type CreateFragmentForTestOptions,\n type RouteHandlerInputOptions,\n type FragmentForTest,\n type InitRoutesOverrides,\n} from \"@fragno-dev/core/test\";\n\n/**\n * Options for creating a database fragment for testing\n */\nexport interface CreateDatabaseFragmentForTestOptions<\n TConfig,\n TDeps,\n TServices,\n TAdditionalContext extends Record<string, unknown>,\n TOptions extends FragnoPublicConfig,\n> extends Omit<\n CreateFragmentForTestOptions<TConfig, TDeps, TServices, TAdditionalContext, TOptions>,\n \"config\"\n > {\n databasePath?: string;\n migrateToVersion?: number;\n config?: TConfig;\n}\n\n/**\n * Extended fragment test instance with database adapter and Kysely instance\n */\nexport interface DatabaseFragmentForTest<\n TConfig,\n TDeps,\n TServices,\n TAdditionalContext extends Record<string, unknown>,\n TOptions extends FragnoPublicConfig,\n> extends FragmentForTest<TConfig, TDeps, TServices, TAdditionalContext, TOptions> {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n kysely: Kysely<any>;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n adapter: DatabaseAdapter<any>;\n}\n\nexport async function createDatabaseFragmentForTest<\n const TConfig,\n const TDeps,\n const TServices extends Record<string, unknown>,\n const TAdditionalContext extends Record<string, unknown>,\n const TOptions extends FragnoPublicConfig,\n const TSchema extends AnySchema,\n>(\n fragmentBuilder: {\n definition: FragmentDefinition<TConfig, TDeps, TServices, TAdditionalContext>;\n $requiredOptions: TOptions;\n },\n options?: CreateDatabaseFragmentForTestOptions<\n TConfig,\n TDeps,\n TServices,\n TAdditionalContext,\n TOptions\n >,\n): Promise<DatabaseFragmentForTest<TConfig, TDeps, TServices, TAdditionalContext, TOptions>> {\n const {\n databasePath = \":memory:\",\n migrateToVersion,\n config,\n options: fragmentOptions,\n deps,\n services,\n additionalContext,\n } = options ?? {};\n\n // Create SQLocalKysely instance\n const { dialect } = new SQLocalKysely(databasePath);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const kysely = new Kysely<any>({\n dialect,\n });\n\n // Create KyselyAdapter\n const adapter = new KyselyAdapter({\n db: kysely,\n provider: \"sqlite\",\n });\n\n // Get schema and namespace from fragment definition's additionalContext\n // Safe cast: DatabaseFragmentBuilder adds databaseSchema and databaseNamespace to additionalContext\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const fragmentAdditionalContext = fragmentBuilder.definition.additionalContext as any;\n const schema = fragmentAdditionalContext?.databaseSchema as TSchema | undefined;\n const namespace = (fragmentAdditionalContext?.databaseNamespace as string | undefined) ?? \"\";\n\n if (!schema) {\n throw new Error(\n `Fragment '${fragmentBuilder.definition.name}' does not have a database schema. ` +\n `Make sure you're using defineFragmentWithDatabase().withDatabase(schema).`,\n );\n }\n\n // Run migrations automatically\n const migrator = adapter.createMigrationEngine(schema, namespace);\n const preparedMigration = migrateToVersion\n ? await migrator.prepareMigrationTo(migrateToVersion, {\n updateSettings: false,\n })\n : await migrator.prepareMigration({\n updateSettings: false,\n });\n await preparedMigration.execute();\n\n // Create fragment with database adapter in options\n // Safe cast: We're merging the user's options with the databaseAdapter, which is required by TOptions\n // The user's TOptions is constrained to FragnoPublicConfig (or a subtype), which we extend with databaseAdapter\n const mergedOptions = {\n ...fragmentOptions,\n databaseAdapter: adapter,\n } as unknown as TOptions;\n\n // Safe cast: If config is not provided, we pass undefined as TConfig.\n // The base createFragmentForTest expects config: TConfig, but if TConfig allows undefined\n // or if the fragment doesn't use config in its dependencies function, this will work correctly.\n const fragment = createFragmentForTest(fragmentBuilder, {\n config: config as TConfig,\n options: mergedOptions,\n deps,\n services,\n additionalContext,\n });\n\n return {\n ...fragment,\n kysely,\n adapter,\n };\n}\n"],"mappings":";;;;;;AAyDA,eAAsB,8BAQpB,iBAIA,SAO2F;CAC3F,MAAM,EACJ,eAAe,YACf,kBACA,QACA,SAAS,iBACT,MACA,UACA,sBACE,WAAW,EAAE;CAGjB,MAAM,EAAE,YAAY,IAAI,cAAc,aAAa;CAEnD,MAAM,SAAS,IAAI,OAAY,EAC7B,SACD,CAAC;CAGF,MAAM,UAAU,IAAI,cAAc;EAChC,IAAI;EACJ,UAAU;EACX,CAAC;CAKF,MAAM,4BAA4B,gBAAgB,WAAW;CAC7D,MAAM,SAAS,2BAA2B;CAC1C,MAAM,YAAa,2BAA2B,qBAA4C;AAE1F,KAAI,CAAC,OACH,OAAM,IAAI,MACR,aAAa,gBAAgB,WAAW,KAAK,8GAE9C;CAIH,MAAM,WAAW,QAAQ,sBAAsB,QAAQ,UAAU;AAQjE,QAP0B,mBACtB,MAAM,SAAS,mBAAmB,kBAAkB,EAClD,gBAAgB,OACjB,CAAC,GACF,MAAM,SAAS,iBAAiB,EAC9B,gBAAgB,OACjB,CAAC,EACkB,SAAS;AAqBjC,QAAO;EACL,GATeA,wBAAsB,iBAAiB;GAC9C;GACR,SAVoB;IACpB,GAAG;IACH,iBAAiB;IAClB;GAQC;GACA;GACA;GACD,CAAC;EAIA;EACA;EACD"}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@fragno-dev/test",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": {
7
+ "development": "./src/index.ts",
8
+ "types": "./dist/index.d.ts",
9
+ "default": "./dist/index.js"
10
+ }
11
+ },
12
+ "main": "./dist/index.js",
13
+ "module": "./dist/index.js",
14
+ "types": "./dist/index.d.ts",
15
+ "dependencies": {
16
+ "kysely": "^0.28.7",
17
+ "sqlocal": "^0.15.2",
18
+ "@fragno-dev/core": "0.1.3",
19
+ "@fragno-dev/db": "0.1.5"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^22",
23
+ "@vitest/coverage-istanbul": "^3.2.4",
24
+ "vitest": "^3.2.4",
25
+ "zod": "^4.1.12",
26
+ "@fragno-private/vitest-config": "0.0.0",
27
+ "@fragno-private/typescript-config": "0.0.1"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/rejot-dev/fragno.git",
32
+ "directory": "packages/fragno-test"
33
+ },
34
+ "homepage": "https://fragno.dev",
35
+ "license": "MIT",
36
+ "scripts": {
37
+ "build": "tsdown",
38
+ "build:watch": "tsdown --watch",
39
+ "types:check": "tsc --noEmit",
40
+ "test": "vitest run",
41
+ "test:watch": "vitest --watch"
42
+ }
43
+ }
@@ -0,0 +1,263 @@
1
+ import { describe, expect, it, afterEach } from "vitest";
2
+ import { column, idColumn, schema } from "@fragno-dev/db/schema";
3
+ import { defineFragmentWithDatabase } from "@fragno-dev/db/fragment";
4
+ import { createDatabaseFragmentForTest } from "./index";
5
+ import { unlinkSync, existsSync } from "node:fs";
6
+
7
+ // Test schema with multiple versions
8
+ const testSchema = schema((s) => {
9
+ return s
10
+ .addTable("users", (t) => {
11
+ return t
12
+ .addColumn("id", idColumn())
13
+ .addColumn("name", column("string"))
14
+ .addColumn("email", column("string"))
15
+ .createIndex("idx_users_all", ["id"]); // Index for querying
16
+ })
17
+ .alterTable("users", (t) => {
18
+ return t.addColumn("age", column("integer").nullable());
19
+ });
20
+ });
21
+
22
+ // Test fragment definition
23
+ const testFragmentDef = defineFragmentWithDatabase<{}>("test-fragment")
24
+ .withDatabase(testSchema)
25
+ .withServices(({ orm }) => {
26
+ return {
27
+ createUser: async (data: { name: string; email: string; age?: number | null }) => {
28
+ const id = await orm.create("users", data);
29
+ return { ...data, id: id.valueOf() };
30
+ },
31
+ getUsers: async () => {
32
+ const users = await orm.find("users", (b) =>
33
+ b.whereIndex("idx_users_all", (eb) => eb("id", "!=", "")),
34
+ );
35
+ return users.map((u) => ({ ...u, id: u.id.valueOf() }));
36
+ },
37
+ };
38
+ });
39
+
40
+ describe("createDatabaseFragmentForTest", () => {
41
+ describe("databasePath option", () => {
42
+ const testDbPath = "./test-fragno.pglite";
43
+
44
+ afterEach(() => {
45
+ // Clean up test database files
46
+ if (existsSync(testDbPath)) {
47
+ try {
48
+ unlinkSync(testDbPath);
49
+ } catch {
50
+ // Ignore cleanup errors
51
+ }
52
+ }
53
+ });
54
+
55
+ it("should use in-memory database by default", async () => {
56
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef);
57
+
58
+ // Should be able to create and query users
59
+ const user = await fragment.services.createUser({
60
+ name: "Test User",
61
+ email: "test@example.com",
62
+ age: 25,
63
+ });
64
+
65
+ expect(user).toMatchObject({
66
+ id: expect.any(String),
67
+ name: "Test User",
68
+ email: "test@example.com",
69
+ age: 25,
70
+ });
71
+
72
+ const users = await fragment.services.getUsers();
73
+ expect(users).toHaveLength(1);
74
+ expect(users[0]).toMatchObject(user);
75
+ });
76
+
77
+ it("should create database at specified path", async () => {
78
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef, {
79
+ databasePath: testDbPath,
80
+ });
81
+
82
+ // Create a user
83
+ await fragment.services.createUser({
84
+ name: "Persisted User",
85
+ email: "persisted@example.com",
86
+ age: 30,
87
+ });
88
+
89
+ // Verify data exists in this instance
90
+ const users = await fragment.services.getUsers();
91
+ expect(users).toHaveLength(1);
92
+ expect(users[0]).toMatchObject({
93
+ name: "Persisted User",
94
+ email: "persisted@example.com",
95
+ age: 30,
96
+ });
97
+ });
98
+ });
99
+
100
+ describe("migrateToVersion option", () => {
101
+ it("should migrate to latest version by default", async () => {
102
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef);
103
+
104
+ // Should have the 'age' column from version 2
105
+ const user = await fragment.services.createUser({
106
+ name: "Test User",
107
+ email: "test@example.com",
108
+ age: 25,
109
+ });
110
+
111
+ expect(user).toMatchObject({
112
+ id: expect.any(String),
113
+ name: "Test User",
114
+ email: "test@example.com",
115
+ age: 25,
116
+ });
117
+ });
118
+
119
+ it("should migrate to specific version when specified", async () => {
120
+ // Migrate to version 1 (before 'age' column was added)
121
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef, {
122
+ migrateToVersion: 1,
123
+ });
124
+
125
+ // Query the database directly to check schema
126
+ // In version 1, we should be able to insert without the age column
127
+ const tableName = "users_test-fragment-db";
128
+ await fragment.kysely
129
+ .insertInto(tableName)
130
+ .values({
131
+ id: "test-id-1",
132
+ name: "V1 User",
133
+ email: "v1@example.com",
134
+ })
135
+ .execute();
136
+
137
+ const result = await fragment.kysely.selectFrom(tableName).selectAll().execute();
138
+
139
+ expect(result).toHaveLength(1);
140
+ expect(result[0]).toMatchObject({
141
+ id: "test-id-1",
142
+ name: "V1 User",
143
+ email: "v1@example.com",
144
+ });
145
+ // In version 1, the age column should not exist
146
+ expect(result[0]).not.toHaveProperty("age");
147
+ });
148
+
149
+ it("should allow creating user with age when migrated to version 2", async () => {
150
+ // Explicitly migrate to version 2
151
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef, {
152
+ migrateToVersion: 2,
153
+ });
154
+
155
+ // Should be able to use age column
156
+ const user = await fragment.services.createUser({
157
+ name: "V2 User",
158
+ email: "v2@example.com",
159
+ age: 35,
160
+ });
161
+
162
+ expect(user).toMatchObject({
163
+ id: expect.any(String),
164
+ name: "V2 User",
165
+ email: "v2@example.com",
166
+ age: 35,
167
+ });
168
+
169
+ const tableName = "users_test-fragment-db";
170
+ const result = await fragment.kysely.selectFrom(tableName).selectAll().execute();
171
+
172
+ expect(result).toHaveLength(1);
173
+ expect(result[0]).toMatchObject({
174
+ name: "V2 User",
175
+ email: "v2@example.com",
176
+ age: 35,
177
+ });
178
+ });
179
+ });
180
+
181
+ describe("combined options", () => {
182
+ const testDbPath = "./test-combined.pglite";
183
+
184
+ afterEach(() => {
185
+ if (existsSync(testDbPath)) {
186
+ try {
187
+ unlinkSync(testDbPath);
188
+ } catch {
189
+ // Ignore cleanup errors
190
+ }
191
+ }
192
+ });
193
+
194
+ it("should work with both databasePath and migrateToVersion", async () => {
195
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef, {
196
+ databasePath: testDbPath,
197
+ migrateToVersion: 2,
198
+ });
199
+
200
+ // Create user at version 2 (with age support)
201
+ const user = await fragment.services.createUser({
202
+ name: "Combined Test",
203
+ email: "combined@example.com",
204
+ age: 40,
205
+ });
206
+
207
+ expect(user).toMatchObject({
208
+ id: expect.any(String),
209
+ name: "Combined Test",
210
+ email: "combined@example.com",
211
+ age: 40,
212
+ });
213
+
214
+ const users = await fragment.services.getUsers();
215
+ expect(users).toHaveLength(1);
216
+ expect(users[0]).toMatchObject({
217
+ name: "Combined Test",
218
+ email: "combined@example.com",
219
+ age: 40,
220
+ });
221
+ });
222
+ });
223
+
224
+ describe("fragment initialization", () => {
225
+ it("should provide kysely instance", async () => {
226
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef);
227
+
228
+ expect(fragment.kysely).toBeDefined();
229
+ expect(typeof fragment.kysely.selectFrom).toBe("function");
230
+ });
231
+
232
+ it("should provide adapter instance", async () => {
233
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef);
234
+
235
+ expect(fragment.adapter).toBeDefined();
236
+ expect(typeof fragment.adapter.createMigrationEngine).toBe("function");
237
+ });
238
+
239
+ it("should have all standard fragment test properties", async () => {
240
+ const fragment = await createDatabaseFragmentForTest(testFragmentDef);
241
+
242
+ expect(fragment.services).toBeDefined();
243
+ expect(fragment.initRoutes).toBeDefined();
244
+ expect(fragment.handler).toBeDefined();
245
+ });
246
+
247
+ it("should throw error for non-database fragment", async () => {
248
+ // Create a fragment without database
249
+ const nonDbFragment = {
250
+ definition: {
251
+ name: "non-db-fragment",
252
+ additionalContext: {},
253
+ },
254
+ $requiredOptions: {},
255
+ };
256
+
257
+ await expect(
258
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
259
+ createDatabaseFragmentForTest(nonDbFragment as any),
260
+ ).rejects.toThrow("Fragment 'non-db-fragment' does not have a database schema");
261
+ });
262
+ });
263
+ });
package/src/index.ts ADDED
@@ -0,0 +1,150 @@
1
+ import { Kysely } from "kysely";
2
+ import { SQLocalKysely } from "sqlocal/kysely";
3
+ import { KyselyAdapter } from "@fragno-dev/db/adapters/kysely";
4
+ import type { AnySchema } from "@fragno-dev/db/schema";
5
+ import type { DatabaseAdapter } from "@fragno-dev/db/adapters";
6
+ import {
7
+ createFragmentForTest,
8
+ type FragmentForTest,
9
+ type CreateFragmentForTestOptions,
10
+ } from "@fragno-dev/core/test";
11
+ import type { FragnoPublicConfig } from "@fragno-dev/core/api/fragment-instantiation";
12
+ import type { FragmentDefinition } from "@fragno-dev/core/api/fragment-builder";
13
+
14
+ // Re-export utilities from @fragno-dev/core/test
15
+ export {
16
+ createFragmentForTest,
17
+ type TestResponse,
18
+ type CreateFragmentForTestOptions,
19
+ type RouteHandlerInputOptions,
20
+ type FragmentForTest,
21
+ type InitRoutesOverrides,
22
+ } from "@fragno-dev/core/test";
23
+
24
+ /**
25
+ * Options for creating a database fragment for testing
26
+ */
27
+ export interface CreateDatabaseFragmentForTestOptions<
28
+ TConfig,
29
+ TDeps,
30
+ TServices,
31
+ TAdditionalContext extends Record<string, unknown>,
32
+ TOptions extends FragnoPublicConfig,
33
+ > extends Omit<
34
+ CreateFragmentForTestOptions<TConfig, TDeps, TServices, TAdditionalContext, TOptions>,
35
+ "config"
36
+ > {
37
+ databasePath?: string;
38
+ migrateToVersion?: number;
39
+ config?: TConfig;
40
+ }
41
+
42
+ /**
43
+ * Extended fragment test instance with database adapter and Kysely instance
44
+ */
45
+ export interface DatabaseFragmentForTest<
46
+ TConfig,
47
+ TDeps,
48
+ TServices,
49
+ TAdditionalContext extends Record<string, unknown>,
50
+ TOptions extends FragnoPublicConfig,
51
+ > extends FragmentForTest<TConfig, TDeps, TServices, TAdditionalContext, TOptions> {
52
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
+ kysely: Kysely<any>;
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ adapter: DatabaseAdapter<any>;
56
+ }
57
+
58
+ export async function createDatabaseFragmentForTest<
59
+ const TConfig,
60
+ const TDeps,
61
+ const TServices extends Record<string, unknown>,
62
+ const TAdditionalContext extends Record<string, unknown>,
63
+ const TOptions extends FragnoPublicConfig,
64
+ const TSchema extends AnySchema,
65
+ >(
66
+ fragmentBuilder: {
67
+ definition: FragmentDefinition<TConfig, TDeps, TServices, TAdditionalContext>;
68
+ $requiredOptions: TOptions;
69
+ },
70
+ options?: CreateDatabaseFragmentForTestOptions<
71
+ TConfig,
72
+ TDeps,
73
+ TServices,
74
+ TAdditionalContext,
75
+ TOptions
76
+ >,
77
+ ): Promise<DatabaseFragmentForTest<TConfig, TDeps, TServices, TAdditionalContext, TOptions>> {
78
+ const {
79
+ databasePath = ":memory:",
80
+ migrateToVersion,
81
+ config,
82
+ options: fragmentOptions,
83
+ deps,
84
+ services,
85
+ additionalContext,
86
+ } = options ?? {};
87
+
88
+ // Create SQLocalKysely instance
89
+ const { dialect } = new SQLocalKysely(databasePath);
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ const kysely = new Kysely<any>({
92
+ dialect,
93
+ });
94
+
95
+ // Create KyselyAdapter
96
+ const adapter = new KyselyAdapter({
97
+ db: kysely,
98
+ provider: "sqlite",
99
+ });
100
+
101
+ // Get schema and namespace from fragment definition's additionalContext
102
+ // Safe cast: DatabaseFragmentBuilder adds databaseSchema and databaseNamespace to additionalContext
103
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
104
+ const fragmentAdditionalContext = fragmentBuilder.definition.additionalContext as any;
105
+ const schema = fragmentAdditionalContext?.databaseSchema as TSchema | undefined;
106
+ const namespace = (fragmentAdditionalContext?.databaseNamespace as string | undefined) ?? "";
107
+
108
+ if (!schema) {
109
+ throw new Error(
110
+ `Fragment '${fragmentBuilder.definition.name}' does not have a database schema. ` +
111
+ `Make sure you're using defineFragmentWithDatabase().withDatabase(schema).`,
112
+ );
113
+ }
114
+
115
+ // Run migrations automatically
116
+ const migrator = adapter.createMigrationEngine(schema, namespace);
117
+ const preparedMigration = migrateToVersion
118
+ ? await migrator.prepareMigrationTo(migrateToVersion, {
119
+ updateSettings: false,
120
+ })
121
+ : await migrator.prepareMigration({
122
+ updateSettings: false,
123
+ });
124
+ await preparedMigration.execute();
125
+
126
+ // Create fragment with database adapter in options
127
+ // Safe cast: We're merging the user's options with the databaseAdapter, which is required by TOptions
128
+ // The user's TOptions is constrained to FragnoPublicConfig (or a subtype), which we extend with databaseAdapter
129
+ const mergedOptions = {
130
+ ...fragmentOptions,
131
+ databaseAdapter: adapter,
132
+ } as unknown as TOptions;
133
+
134
+ // Safe cast: If config is not provided, we pass undefined as TConfig.
135
+ // The base createFragmentForTest expects config: TConfig, but if TConfig allows undefined
136
+ // or if the fragment doesn't use config in its dependencies function, this will work correctly.
137
+ const fragment = createFragmentForTest(fragmentBuilder, {
138
+ config: config as TConfig,
139
+ options: mergedOptions,
140
+ deps,
141
+ services,
142
+ additionalContext,
143
+ });
144
+
145
+ return {
146
+ ...fragment,
147
+ kysely,
148
+ adapter,
149
+ };
150
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "@fragno-private/typescript-config/tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": ".",
6
+ "composite": true
7
+ },
8
+ "include": ["src"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from "tsdown";
2
+
3
+ export default defineConfig({
4
+ entry: ["./src/index.ts"],
5
+ dts: true,
6
+ unbundle: true,
7
+ });
@@ -0,0 +1,4 @@
1
+ import { defineConfig, mergeConfig } from "vitest/config";
2
+ import { baseConfig } from "@fragno-private/vitest-config";
3
+
4
+ export default defineConfig(mergeConfig(baseConfig, {}));