@cedarjs/testing 0.7.2-next.2 → 0.7.2-next.49
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/config/jest/api/package.json +8 -0
- package/config/jest/web/package.json +8 -0
- package/dist/api/mockContext.d.ts +5 -0
- package/dist/api/mockContext.d.ts.map +1 -0
- package/dist/api/mockContext.js +33 -0
- package/dist/api/vitest/CedarApiVitestEnv.d.ts +5 -0
- package/dist/api/vitest/CedarApiVitestEnv.d.ts.map +1 -0
- package/dist/api/vitest/CedarApiVitestEnv.js +41 -0
- package/dist/api/vitest/index.d.ts +4 -0
- package/dist/api/vitest/index.d.ts.map +1 -0
- package/dist/api/vitest/index.js +8 -0
- package/dist/api/vitest/vite-plugin-auto-import.d.ts +2 -0
- package/dist/api/vitest/vite-plugin-auto-import.d.ts.map +1 -0
- package/dist/api/vitest/vite-plugin-auto-import.js +37 -0
- package/dist/api/vitest/vite-plugin-cedar-vitest-api-config.d.ts +3 -0
- package/dist/api/vitest/vite-plugin-cedar-vitest-api-config.d.ts.map +1 -0
- package/dist/api/vitest/vite-plugin-cedar-vitest-api-config.js +34 -0
- package/dist/api/vitest/vite-plugin-track-db-imports.d.ts +3 -0
- package/dist/api/vitest/vite-plugin-track-db-imports.d.ts.map +1 -0
- package/dist/api/vitest/vite-plugin-track-db-imports.js +23 -0
- package/dist/api/vitest/vitest-api.setup.d.ts +9 -0
- package/dist/api/vitest/vitest-api.setup.d.ts.map +1 -0
- package/dist/api/vitest/vitest-api.setup.js +247 -0
- package/dist/cjs/api/mockContext.d.ts +5 -0
- package/dist/cjs/api/mockContext.d.ts.map +1 -0
- package/dist/cjs/api/mockContext.js +58 -0
- package/dist/cjs/api/vitest/CedarApiVitestEnv.js +71 -0
- package/dist/cjs/api/vitest/index.js +34 -0
- package/dist/cjs/api/vitest/vite-plugin-auto-import.js +71 -0
- package/dist/cjs/api/vitest/vite-plugin-cedar-vitest-api-config.js +69 -0
- package/dist/cjs/api/vitest/vite-plugin-track-db-imports.js +47 -0
- package/dist/cjs/api/vitest/vitest-api.setup.js +270 -0
- package/dist/cjs/web/MockProviders.d.ts +0 -4
- package/dist/cjs/web/MockProviders.d.ts.map +1 -1
- package/dist/cjs/web/MockProviders.js +2 -1
- package/dist/cjs/web/MockRouter.d.ts +0 -3
- package/dist/cjs/web/MockRouter.d.ts.map +1 -1
- package/dist/cjs/web/globRoutesImporter.d.ts +13 -0
- package/dist/cjs/web/globRoutesImporter.d.ts.map +1 -0
- package/dist/cjs/web/globRoutesImporter.js +46 -0
- package/dist/cjs/web/vitest/index.d.ts +4 -0
- package/dist/cjs/web/vitest/index.d.ts.map +1 -0
- package/dist/cjs/web/vitest/index.js +34 -0
- package/dist/cjs/web/vitest/vite-plugin-auto-import.d.ts +2 -0
- package/dist/cjs/web/vitest/vite-plugin-auto-import.d.ts.map +1 -0
- package/dist/cjs/web/vitest/vite-plugin-auto-import.js +60 -0
- package/dist/cjs/web/vitest/vite-plugin-cedarjs-router-import-transform.d.ts +7 -0
- package/dist/cjs/web/vitest/vite-plugin-cedarjs-router-import-transform.d.ts.map +1 -0
- package/dist/cjs/web/vitest/vite-plugin-cedarjs-router-import-transform.js +42 -0
- package/dist/cjs/web/vitest/vite-plugin-create-auth-import-transform.d.ts +3 -0
- package/dist/cjs/web/vitest/vite-plugin-create-auth-import-transform.d.ts.map +1 -0
- package/dist/cjs/web/vitest/vite-plugin-create-auth-import-transform.js +43 -0
- package/dist/web/MockProviders.d.ts +0 -4
- package/dist/web/MockProviders.d.ts.map +1 -1
- package/dist/web/MockProviders.js +2 -1
- package/dist/web/MockRouter.d.ts +0 -3
- package/dist/web/MockRouter.d.ts.map +1 -1
- package/dist/web/globRoutesImporter.d.ts +13 -0
- package/dist/web/globRoutesImporter.d.ts.map +1 -0
- package/dist/web/globRoutesImporter.js +21 -0
- package/dist/web/vitest/index.d.ts +4 -0
- package/dist/web/vitest/index.d.ts.map +1 -0
- package/dist/web/vitest/index.js +8 -0
- package/dist/web/vitest/vite-plugin-auto-import.d.ts +2 -0
- package/dist/web/vitest/vite-plugin-auto-import.d.ts.map +1 -0
- package/dist/web/vitest/vite-plugin-auto-import.js +26 -0
- package/dist/web/vitest/vite-plugin-cedarjs-router-import-transform.d.ts +7 -0
- package/dist/web/vitest/vite-plugin-cedarjs-router-import-transform.d.ts.map +1 -0
- package/dist/web/vitest/vite-plugin-cedarjs-router-import-transform.js +18 -0
- package/dist/web/vitest/vite-plugin-create-auth-import-transform.d.ts +3 -0
- package/dist/web/vitest/vite-plugin-create-auth-import-transform.d.ts.map +1 -0
- package/dist/web/vitest/vite-plugin-create-auth-import-transform.js +19 -0
- package/package.json +39 -12
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockContext.d.ts","sourceRoot":"","sources":["../../src/api/mockContext.ts"],"names":[],"mappings":"AAoCA,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEjE,eAAO,MAAM,OAAO,IAAc,CAAA;AAElC,eAAO,MAAM,UAAU,eAAgB,aAAa,KAAG,aAKtD,CAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const mockContextStore = /* @__PURE__ */ new Map();
|
|
2
|
+
const mockContext = new Proxy(
|
|
3
|
+
{},
|
|
4
|
+
{
|
|
5
|
+
get: (_target, prop) => {
|
|
6
|
+
if (prop === "toJSON") {
|
|
7
|
+
return () => mockContextStore.get("context");
|
|
8
|
+
}
|
|
9
|
+
const ctx = mockContextStore.get("context");
|
|
10
|
+
if (!ctx) {
|
|
11
|
+
return void 0;
|
|
12
|
+
}
|
|
13
|
+
return ctx[prop];
|
|
14
|
+
},
|
|
15
|
+
set: (_target, prop, value) => {
|
|
16
|
+
const ctx = mockContextStore.get("context");
|
|
17
|
+
if (!ctx) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
ctx[prop] = value;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
const context = mockContext;
|
|
26
|
+
const setContext = (newContext) => {
|
|
27
|
+
mockContextStore.set("context", newContext);
|
|
28
|
+
return mockContext;
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
context,
|
|
32
|
+
setContext
|
|
33
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CedarApiVitestEnv.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/CedarApiVitestEnv.ts"],"names":[],"mappings":"AACA,OAAO,2BAA2B,CAAA;AAElC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAMtD,QAAA,MAAM,yBAAyB,EAAE,WA+ChC,CAAA;AAED,eAAe,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { getSchema } from "@prisma/internals";
|
|
2
|
+
import "dotenv-defaults/config.js";
|
|
3
|
+
import execa from "execa";
|
|
4
|
+
import { getPaths } from "@cedarjs/project-config";
|
|
5
|
+
import { getDefaultDb, checkAndReplaceDirectUrl } from "../directUrlHelpers.js";
|
|
6
|
+
const CedarApiVitestEnvironment = {
|
|
7
|
+
name: "cedar-api",
|
|
8
|
+
transformMode: "ssr",
|
|
9
|
+
async setup() {
|
|
10
|
+
if (process.env.SKIP_DB_PUSH === "1") {
|
|
11
|
+
return {
|
|
12
|
+
teardown() {
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const cedarPaths = getPaths();
|
|
17
|
+
const defaultDb = getDefaultDb(cedarPaths.base);
|
|
18
|
+
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL || defaultDb;
|
|
19
|
+
const prismaSchema = (await getSchema(cedarPaths.api.dbSchema)).toString();
|
|
20
|
+
const directUrlEnvVar = checkAndReplaceDirectUrl(prismaSchema, defaultDb);
|
|
21
|
+
const command = process.env.TEST_DATABASE_STRATEGY === "reset" ? ["prisma", "migrate", "reset", "--force", "--skip-seed"] : ["prisma", "db", "push", "--force-reset", "--accept-data-loss"];
|
|
22
|
+
const directUrlDefinition = directUrlEnvVar ? { [directUrlEnvVar]: process.env[directUrlEnvVar] } : {};
|
|
23
|
+
execa.sync(`yarn rw`, command, {
|
|
24
|
+
cwd: cedarPaths.api.base,
|
|
25
|
+
stdio: "inherit",
|
|
26
|
+
shell: true,
|
|
27
|
+
env: {
|
|
28
|
+
DATABASE_URL: process.env.DATABASE_URL,
|
|
29
|
+
...directUrlDefinition
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
teardown() {
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var CedarApiVitestEnv_default = CedarApiVitestEnvironment;
|
|
39
|
+
export {
|
|
40
|
+
CedarApiVitestEnv_default as default
|
|
41
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAA;AAChE,OAAO,EAAE,0BAA0B,EAAE,MAAM,0CAA0C,CAAA;AACrF,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { autoImportsPlugin } from "./vite-plugin-auto-import.js";
|
|
2
|
+
import { cedarVitestApiConfigPlugin } from "./vite-plugin-cedar-vitest-api-config.js";
|
|
3
|
+
import { trackDbImportsPlugin } from "./vite-plugin-track-db-imports.js";
|
|
4
|
+
export {
|
|
5
|
+
autoImportsPlugin,
|
|
6
|
+
cedarVitestApiConfigPlugin,
|
|
7
|
+
trackDbImportsPlugin
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-auto-import.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/vite-plugin-auto-import.ts"],"names":[],"mappings":"AAEA,wBAAgB,iBAAiB,8DAiChC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import autoImport from "unplugin-auto-import/vite";
|
|
2
|
+
function autoImportsPlugin() {
|
|
3
|
+
return autoImport({
|
|
4
|
+
// targets to transform
|
|
5
|
+
include: [
|
|
6
|
+
/\.[tj]sx?$/
|
|
7
|
+
// .ts, .tsx, .js, .jsx
|
|
8
|
+
],
|
|
9
|
+
// global imports to register
|
|
10
|
+
imports: [
|
|
11
|
+
// import { mockContext, mockHttpEvent, mockSignedWebhook } from '@cedarjs/testing/api';
|
|
12
|
+
{
|
|
13
|
+
"@cedarjs/testing/api": [
|
|
14
|
+
"mockContext",
|
|
15
|
+
"mockHttpEvent",
|
|
16
|
+
"mockSignedWebhook"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
// import { gql } from 'graphql-tag'
|
|
20
|
+
{
|
|
21
|
+
"graphql-tag": ["gql"]
|
|
22
|
+
},
|
|
23
|
+
// import { context } from '@cedarjs/context'
|
|
24
|
+
{
|
|
25
|
+
"@cedarjs/context": ["context"]
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
// We provide our mocking types elsewhere and so don't need this plugin to
|
|
29
|
+
// generate them.
|
|
30
|
+
// TODO: Maybe we should have it at least generate the types for the gql
|
|
31
|
+
// import? (Or do we already provide that some other way?)
|
|
32
|
+
dts: false
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
autoImportsPlugin
|
|
37
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-cedar-vitest-api-config.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/vite-plugin-cedar-vitest-api-config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAIlC,wBAAgB,0BAA0B,IAAI,MAAM,CA4BnD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { getEnvVarDefinitions, getPaths } from "@cedarjs/project-config";
|
|
3
|
+
function cedarVitestApiConfigPlugin() {
|
|
4
|
+
return {
|
|
5
|
+
name: "cedar-vitest-plugin",
|
|
6
|
+
config: () => {
|
|
7
|
+
return {
|
|
8
|
+
define: getEnvVarDefinitions(),
|
|
9
|
+
ssr: {
|
|
10
|
+
noExternal: ["@cedarjs/testing"]
|
|
11
|
+
},
|
|
12
|
+
resolve: {
|
|
13
|
+
alias: {
|
|
14
|
+
src: getPaths().api.src
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
test: {
|
|
18
|
+
environment: path.join(import.meta.dirname, "CedarApiVitestEnv.js"),
|
|
19
|
+
// fileParallelism: false,
|
|
20
|
+
// fileParallelism doesn't work with vitest projects (which is what
|
|
21
|
+
// we're using in the root vitest.config.ts). As a workaround we set
|
|
22
|
+
// poolOptions instead, which also shouldn't work, but was suggested
|
|
23
|
+
// by Vitest team member AriPerkkio (Hiroshi's answer didn't work).
|
|
24
|
+
// https://github.com/vitest-dev/vitest/discussions/7416
|
|
25
|
+
poolOptions: { forks: { singleFork: true } },
|
|
26
|
+
setupFiles: [path.join(import.meta.dirname, "vitest-api.setup.js")]
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
cedarVitestApiConfigPlugin
|
|
34
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vite-plugin-track-db-imports.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/vite-plugin-track-db-imports.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAElC,wBAAgB,oBAAoB,IAAI,MAAM,CA8B7C"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
function trackDbImportsPlugin() {
|
|
2
|
+
return {
|
|
3
|
+
name: "db-import-tracker",
|
|
4
|
+
transform(code, id) {
|
|
5
|
+
if (id.match(/\/api\/src\/lib\/db\.(js|ts)$/) && code.includes("PrismaClient")) {
|
|
6
|
+
return code + `
|
|
7
|
+
;if (typeof globalThis !== "undefined") {
|
|
8
|
+
globalThis.__cedarjs_db_imported__ = true;
|
|
9
|
+
} else {
|
|
10
|
+
throw new Error(
|
|
11
|
+
"vite-plugin-track-db-imports: globalThis is undefined. " +
|
|
12
|
+
"This is an error with CedarJS"
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
`;
|
|
16
|
+
}
|
|
17
|
+
return code;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
trackDbImportsPlugin
|
|
23
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DefineScenario } from '@cedarjs/testing/api';
|
|
2
|
+
declare global {
|
|
3
|
+
var mockCurrentUser: (currentUser: Record<string, unknown> | null) => void;
|
|
4
|
+
}
|
|
5
|
+
declare global {
|
|
6
|
+
var defineScenario: DefineScenario;
|
|
7
|
+
var __cedarjs_db_imported__: boolean;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=vitest-api.setup.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vitest-api.setup.d.ts","sourceRoot":"","sources":["../../../src/api/vitest/vitest-api.setup.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AA0C1D,OAAO,CAAC,MAAM,CAAC;IAEb,IAAI,eAAe,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,KAAK,IAAI,CAAA;CAC3E;AAUD,OAAO,CAAC,MAAM,CAAC;IAEb,IAAI,cAAc,EAAE,cAAc,CAAA;IAElC,IAAI,uBAAuB,EAAE,OAAO,CAAA;CACrC"}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { afterAll, beforeEach, it, describe, vi, beforeAll } from "vitest";
|
|
4
|
+
import { getPaths } from "@cedarjs/project-config";
|
|
5
|
+
import { defineScenario } from "@cedarjs/testing/api";
|
|
6
|
+
const mockContextStore = vi.hoisted(() => /* @__PURE__ */ new Map());
|
|
7
|
+
const mockContext = vi.hoisted(
|
|
8
|
+
() => new Proxy(
|
|
9
|
+
{},
|
|
10
|
+
{
|
|
11
|
+
get: (_target, prop) => {
|
|
12
|
+
if (prop === "toJSON") {
|
|
13
|
+
return () => mockContextStore.get("context");
|
|
14
|
+
}
|
|
15
|
+
return mockContextStore.get("context")[prop];
|
|
16
|
+
},
|
|
17
|
+
set: (_target, prop, value) => {
|
|
18
|
+
const ctx = mockContextStore.get("context");
|
|
19
|
+
ctx[prop] = value;
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
)
|
|
24
|
+
);
|
|
25
|
+
vi.mock("@cedarjs/context", () => {
|
|
26
|
+
return {
|
|
27
|
+
context: mockContext,
|
|
28
|
+
setContext: (newContext) => {
|
|
29
|
+
mockContextStore.set("context", newContext);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
mockContextStore.set("context", {});
|
|
35
|
+
});
|
|
36
|
+
globalThis.mockCurrentUser = (currentUser) => {
|
|
37
|
+
mockContextStore.set("context", { currentUser });
|
|
38
|
+
};
|
|
39
|
+
globalThis.defineScenario = defineScenario;
|
|
40
|
+
const cedarPaths = getPaths();
|
|
41
|
+
const FOREIGN_KEY_ERRORS = [1451, 1811, 23503];
|
|
42
|
+
const TEARDOWN_CACHE_PATH = path.join(
|
|
43
|
+
cedarPaths.generated.base,
|
|
44
|
+
"scenarioTeardown.json"
|
|
45
|
+
);
|
|
46
|
+
const DEFAULT_SCENARIO = "standard";
|
|
47
|
+
let teardownOrder = [];
|
|
48
|
+
let originalTeardownOrder = [];
|
|
49
|
+
function buildScenario(itFunc) {
|
|
50
|
+
return (...args) => {
|
|
51
|
+
let scenarioName;
|
|
52
|
+
let testName;
|
|
53
|
+
let testFunc;
|
|
54
|
+
if (args.length === 3) {
|
|
55
|
+
;
|
|
56
|
+
[scenarioName, testName, testFunc] = args;
|
|
57
|
+
} else if (args.length === 2) {
|
|
58
|
+
scenarioName = DEFAULT_SCENARIO;
|
|
59
|
+
[testName, testFunc] = args;
|
|
60
|
+
} else {
|
|
61
|
+
throw new Error("scenario() requires 2 or 3 arguments");
|
|
62
|
+
}
|
|
63
|
+
return itFunc(testName, async (ctx) => {
|
|
64
|
+
const testPath = ctx.task.file.filepath;
|
|
65
|
+
const { scenario } = await loadScenarios(testPath, scenarioName);
|
|
66
|
+
const scenarioData = await seedScenario(scenario);
|
|
67
|
+
try {
|
|
68
|
+
const result = await testFunc(scenarioData);
|
|
69
|
+
return result;
|
|
70
|
+
} finally {
|
|
71
|
+
await teardown();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function buildDescribeScenario(describeFunc) {
|
|
77
|
+
return (...args) => {
|
|
78
|
+
let scenarioName;
|
|
79
|
+
let describeBlockName;
|
|
80
|
+
let describeBlock;
|
|
81
|
+
if (args.length === 3) {
|
|
82
|
+
;
|
|
83
|
+
[scenarioName, describeBlockName, describeBlock] = args;
|
|
84
|
+
} else if (args.length === 2) {
|
|
85
|
+
scenarioName = DEFAULT_SCENARIO;
|
|
86
|
+
[describeBlockName, describeBlock] = args;
|
|
87
|
+
} else {
|
|
88
|
+
throw new Error("describeScenario() requires 2 or 3 arguments");
|
|
89
|
+
}
|
|
90
|
+
return describeFunc(describeBlockName, () => {
|
|
91
|
+
let scenarioData;
|
|
92
|
+
beforeAll(async (ctx) => {
|
|
93
|
+
const testPath = ctx.file.filepath;
|
|
94
|
+
const { scenario } = await loadScenarios(testPath, scenarioName);
|
|
95
|
+
scenarioData = await seedScenario(scenario);
|
|
96
|
+
});
|
|
97
|
+
afterAll(async () => {
|
|
98
|
+
await teardown();
|
|
99
|
+
});
|
|
100
|
+
const getScenario = () => scenarioData;
|
|
101
|
+
describeBlock(getScenario);
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async function configureTeardown() {
|
|
106
|
+
if (!wasDbImported()) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const { getDMMF, getSchema } = await import("@prisma/internals");
|
|
110
|
+
const datamodel = await getSchema(cedarPaths.api.dbSchema);
|
|
111
|
+
const schema = await getDMMF({ datamodel });
|
|
112
|
+
const schemaModels = schema.datamodel.models.map((m) => {
|
|
113
|
+
return m.dbName || m.name;
|
|
114
|
+
});
|
|
115
|
+
if (fs.existsSync(TEARDOWN_CACHE_PATH)) {
|
|
116
|
+
teardownOrder = JSON.parse(fs.readFileSync(TEARDOWN_CACHE_PATH).toString());
|
|
117
|
+
}
|
|
118
|
+
if (teardownOrder.length !== schemaModels.length) {
|
|
119
|
+
teardownOrder = schemaModels;
|
|
120
|
+
}
|
|
121
|
+
originalTeardownOrder = deepCopy(teardownOrder);
|
|
122
|
+
}
|
|
123
|
+
beforeAll(async () => {
|
|
124
|
+
await configureTeardown();
|
|
125
|
+
});
|
|
126
|
+
afterAll(() => {
|
|
127
|
+
globalThis.__cedarjs_db_imported__ = false;
|
|
128
|
+
});
|
|
129
|
+
async function teardown() {
|
|
130
|
+
if (!wasDbImported()) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
const quoteStyle2 = await getQuoteStyle();
|
|
134
|
+
const projectDb = await getProjectDb();
|
|
135
|
+
for (const modelName of teardownOrder) {
|
|
136
|
+
try {
|
|
137
|
+
const query = `DELETE FROM ${quoteStyle2}${modelName}${quoteStyle2}`;
|
|
138
|
+
await projectDb.$executeRawUnsafe(query);
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error("teardown error\n", e);
|
|
141
|
+
const match = isErrorWithMessage(e) && e.message.match(/Code: `(\d+)`/);
|
|
142
|
+
if (match && FOREIGN_KEY_ERRORS.includes(parseInt(match[1]))) {
|
|
143
|
+
const index = teardownOrder.indexOf(modelName);
|
|
144
|
+
teardownOrder[index] = null;
|
|
145
|
+
teardownOrder.push(modelName);
|
|
146
|
+
} else {
|
|
147
|
+
throw e;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
teardownOrder = teardownOrder.filter((val) => val);
|
|
152
|
+
if (!isIdenticalArray(teardownOrder, originalTeardownOrder)) {
|
|
153
|
+
originalTeardownOrder = deepCopy(teardownOrder);
|
|
154
|
+
fs.writeFileSync(TEARDOWN_CACHE_PATH, JSON.stringify(teardownOrder));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const seedScenario = async (scenario) => {
|
|
158
|
+
if (scenario) {
|
|
159
|
+
const scenarios = {};
|
|
160
|
+
const projectDb = await getProjectDb();
|
|
161
|
+
for (const [model, namedFixtures] of Object.entries(scenario)) {
|
|
162
|
+
scenarios[model] = {};
|
|
163
|
+
for (const [name, createArgs] of Object.entries(namedFixtures)) {
|
|
164
|
+
if (typeof createArgs === "function") {
|
|
165
|
+
scenarios[model][name] = await projectDb[model].create(
|
|
166
|
+
createArgs(scenarios)
|
|
167
|
+
);
|
|
168
|
+
} else {
|
|
169
|
+
scenarios[model][name] = await projectDb[model].create(createArgs);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return scenarios;
|
|
174
|
+
} else {
|
|
175
|
+
return {};
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
async function loadScenarios(testPath, scenarioName) {
|
|
179
|
+
const testFileDir = path.parse(testPath);
|
|
180
|
+
const testFileNameParts = testFileDir.name.split(".");
|
|
181
|
+
const testFilePath = `${testFileDir.dir}/${testFileNameParts.slice(0, testFileNameParts.length - 1).join(".")}.scenarios`;
|
|
182
|
+
let allScenarios;
|
|
183
|
+
let scenario;
|
|
184
|
+
try {
|
|
185
|
+
allScenarios = await import(testFilePath);
|
|
186
|
+
} catch (e) {
|
|
187
|
+
if (isErrorWithCode(e)) {
|
|
188
|
+
if (e instanceof Error) {
|
|
189
|
+
throw e;
|
|
190
|
+
} else {
|
|
191
|
+
console.error("unexpected error type", e);
|
|
192
|
+
throw e;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (allScenarios) {
|
|
197
|
+
if (allScenarios[scenarioName]) {
|
|
198
|
+
scenario = allScenarios[scenarioName];
|
|
199
|
+
} else {
|
|
200
|
+
throw new Error(
|
|
201
|
+
`UndefinedScenario: There is no scenario named "${scenarioName}" in ${testFilePath}.{js,ts}`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { scenario };
|
|
206
|
+
}
|
|
207
|
+
const wasDbImported = () => {
|
|
208
|
+
return Boolean(globalThis.__cedarjs_db_imported__);
|
|
209
|
+
};
|
|
210
|
+
let quoteStyle;
|
|
211
|
+
async function getQuoteStyle() {
|
|
212
|
+
const { getConfig: getPrismaConfig, getSchema } = await import("@prisma/internals");
|
|
213
|
+
const datamodel = await getSchema(cedarPaths.api.dbSchema);
|
|
214
|
+
if (!quoteStyle) {
|
|
215
|
+
const config = await getPrismaConfig({
|
|
216
|
+
datamodel
|
|
217
|
+
});
|
|
218
|
+
switch (config.datasources?.[0]?.provider) {
|
|
219
|
+
case "mysql":
|
|
220
|
+
quoteStyle = "`";
|
|
221
|
+
break;
|
|
222
|
+
default:
|
|
223
|
+
quoteStyle = '"';
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return quoteStyle;
|
|
227
|
+
}
|
|
228
|
+
async function getProjectDb() {
|
|
229
|
+
const libDb = await import(`${cedarPaths.api.lib}/db`);
|
|
230
|
+
return libDb.db;
|
|
231
|
+
}
|
|
232
|
+
function isIdenticalArray(a, b) {
|
|
233
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
234
|
+
}
|
|
235
|
+
function deepCopy(obj) {
|
|
236
|
+
return JSON.parse(JSON.stringify(obj));
|
|
237
|
+
}
|
|
238
|
+
function isErrorWithMessage(e) {
|
|
239
|
+
return !!e && typeof e === "object" && "message" in e && typeof e.message === "string";
|
|
240
|
+
}
|
|
241
|
+
function isErrorWithCode(e) {
|
|
242
|
+
return !!e && typeof e === "object" && "code" in e && typeof e.code === "string";
|
|
243
|
+
}
|
|
244
|
+
globalThis.scenario = buildScenario(it);
|
|
245
|
+
globalThis.scenario.only = buildScenario(it.only);
|
|
246
|
+
globalThis.describeScenario = buildDescribeScenario(describe);
|
|
247
|
+
globalThis.describeScenario.only = buildDescribeScenario(describe.only);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mockContext.d.ts","sourceRoot":"","sources":["../../../src/api/mockContext.ts"],"names":[],"mappings":"AAoCA,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEjE,eAAO,MAAM,OAAO,IAAc,CAAA;AAElC,eAAO,MAAM,UAAU,eAAgB,aAAa,KAAG,aAKtD,CAAA"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var mockContext_exports = {};
|
|
20
|
+
__export(mockContext_exports, {
|
|
21
|
+
context: () => context,
|
|
22
|
+
setContext: () => setContext
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(mockContext_exports);
|
|
25
|
+
const mockContextStore = /* @__PURE__ */ new Map();
|
|
26
|
+
const mockContext = new Proxy(
|
|
27
|
+
{},
|
|
28
|
+
{
|
|
29
|
+
get: (_target, prop) => {
|
|
30
|
+
if (prop === "toJSON") {
|
|
31
|
+
return () => mockContextStore.get("context");
|
|
32
|
+
}
|
|
33
|
+
const ctx = mockContextStore.get("context");
|
|
34
|
+
if (!ctx) {
|
|
35
|
+
return void 0;
|
|
36
|
+
}
|
|
37
|
+
return ctx[prop];
|
|
38
|
+
},
|
|
39
|
+
set: (_target, prop, value) => {
|
|
40
|
+
const ctx = mockContextStore.get("context");
|
|
41
|
+
if (!ctx) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
ctx[prop] = value;
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
const context = mockContext;
|
|
50
|
+
const setContext = (newContext) => {
|
|
51
|
+
mockContextStore.set("context", newContext);
|
|
52
|
+
return mockContext;
|
|
53
|
+
};
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
context,
|
|
57
|
+
setContext
|
|
58
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var CedarApiVitestEnv_exports = {};
|
|
30
|
+
__export(CedarApiVitestEnv_exports, {
|
|
31
|
+
default: () => CedarApiVitestEnv_default
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(CedarApiVitestEnv_exports);
|
|
34
|
+
var import_internals = require("@prisma/internals");
|
|
35
|
+
var import_config = require("dotenv-defaults/config.js");
|
|
36
|
+
var import_execa = __toESM(require("execa"), 1);
|
|
37
|
+
var import_project_config = require("@cedarjs/project-config");
|
|
38
|
+
var import_directUrlHelpers = require("../directUrlHelpers.js");
|
|
39
|
+
const CedarApiVitestEnvironment = {
|
|
40
|
+
name: "cedar-api",
|
|
41
|
+
transformMode: "ssr",
|
|
42
|
+
async setup() {
|
|
43
|
+
if (process.env.SKIP_DB_PUSH === "1") {
|
|
44
|
+
return {
|
|
45
|
+
teardown() {
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const cedarPaths = (0, import_project_config.getPaths)();
|
|
50
|
+
const defaultDb = (0, import_directUrlHelpers.getDefaultDb)(cedarPaths.base);
|
|
51
|
+
process.env.DATABASE_URL = process.env.TEST_DATABASE_URL || defaultDb;
|
|
52
|
+
const prismaSchema = (await (0, import_internals.getSchema)(cedarPaths.api.dbSchema)).toString();
|
|
53
|
+
const directUrlEnvVar = (0, import_directUrlHelpers.checkAndReplaceDirectUrl)(prismaSchema, defaultDb);
|
|
54
|
+
const command = process.env.TEST_DATABASE_STRATEGY === "reset" ? ["prisma", "migrate", "reset", "--force", "--skip-seed"] : ["prisma", "db", "push", "--force-reset", "--accept-data-loss"];
|
|
55
|
+
const directUrlDefinition = directUrlEnvVar ? { [directUrlEnvVar]: process.env[directUrlEnvVar] } : {};
|
|
56
|
+
import_execa.default.sync(`yarn rw`, command, {
|
|
57
|
+
cwd: cedarPaths.api.base,
|
|
58
|
+
stdio: "inherit",
|
|
59
|
+
shell: true,
|
|
60
|
+
env: {
|
|
61
|
+
DATABASE_URL: process.env.DATABASE_URL,
|
|
62
|
+
...directUrlDefinition
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
return {
|
|
66
|
+
teardown() {
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var CedarApiVitestEnv_default = CedarApiVitestEnvironment;
|