@vaharoni/devops 1.2.14 → 1.2.15
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.
|
@@ -2,21 +2,35 @@ import { afterEach, beforeEach, vi } from 'vitest';
|
|
|
2
2
|
import { prisma, rollbackTx, startTx } from './db-client-test';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
+
* Vitest setup for Prisma transactional tests.
|
|
6
|
+
*
|
|
7
|
+
* Each test runs inside a transaction that is rolled back after the test completes,
|
|
8
|
+
* ensuring test isolation without persisting data to the database.
|
|
9
|
+
*
|
|
5
10
|
* Usage:
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
* Create a `vitest.config.ts` in the project's folder:
|
|
12
|
+
*
|
|
13
|
+
* import { defineConfig } from 'vitest/config'
|
|
14
|
+
* export default defineConfig({
|
|
15
|
+
* test: {
|
|
16
|
+
* setupFiles: ['../../db/prisma-setup-vitest.ts'] // adjust path as needed
|
|
17
|
+
* },
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* How it works:
|
|
21
|
+
* - db-client-test.ts creates a transactional Prisma client and stores it in globalThis.prismaGlobal
|
|
22
|
+
* - This is the same singleton key used by db/db-client.ts
|
|
23
|
+
* - All code importing from "db" gets the transactional client
|
|
24
|
+
* - beforeEach starts a transaction, afterEach rolls it back
|
|
17
25
|
*/
|
|
18
26
|
|
|
19
|
-
vi.mock('db', () =>
|
|
27
|
+
vi.mock('db', async () => {
|
|
28
|
+
const prismaClient = await import('@prisma/client');
|
|
29
|
+
return {
|
|
30
|
+
...prismaClient,
|
|
31
|
+
prisma,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
20
34
|
|
|
21
35
|
beforeEach(async () => {
|
|
22
36
|
await startTx();
|
package/package.json
CHANGED
|
@@ -2,21 +2,35 @@ import { afterEach, beforeEach, vi } from 'vitest';
|
|
|
2
2
|
import { prisma, rollbackTx, startTx } from './db-client-test';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
+
* Vitest setup for Prisma transactional tests.
|
|
6
|
+
*
|
|
7
|
+
* Each test runs inside a transaction that is rolled back after the test completes,
|
|
8
|
+
* ensuring test isolation without persisting data to the database.
|
|
9
|
+
*
|
|
5
10
|
* Usage:
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
* Create a `vitest.config.ts` in the project's folder:
|
|
12
|
+
*
|
|
13
|
+
* import { defineConfig } from 'vitest/config'
|
|
14
|
+
* export default defineConfig({
|
|
15
|
+
* test: {
|
|
16
|
+
* setupFiles: ['../../db/prisma-setup-vitest.ts'] // adjust path as needed
|
|
17
|
+
* },
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* How it works:
|
|
21
|
+
* - db-client-test.ts creates a transactional Prisma client and stores it in globalThis.prismaGlobal
|
|
22
|
+
* - This is the same singleton key used by db/db-client.ts
|
|
23
|
+
* - All code importing from "db" gets the transactional client
|
|
24
|
+
* - beforeEach starts a transaction, afterEach rolls it back
|
|
17
25
|
*/
|
|
18
26
|
|
|
19
|
-
vi.mock('db', () =>
|
|
27
|
+
vi.mock('db', async () => {
|
|
28
|
+
const prismaClient = await import('@prisma/client');
|
|
29
|
+
return {
|
|
30
|
+
...prismaClient,
|
|
31
|
+
prisma,
|
|
32
|
+
};
|
|
33
|
+
});
|
|
20
34
|
|
|
21
35
|
beforeEach(async () => {
|
|
22
36
|
await startTx();
|