diff --git a/packages/@liexp/backend/src/test/vitest.base-config.ts b/packages/@liexp/backend/src/test/vitest.base-config.ts index 5e1666dfd..88059098c 100644 --- a/packages/@liexp/backend/src/test/vitest.base-config.ts +++ b/packages/@liexp/backend/src/test/vitest.base-config.ts @@ -1,31 +1,50 @@ import { URL } from "url"; import viteTsconfigPaths from "vite-tsconfig-paths"; -import { defineConfig } from "vitest/config"; +import { defineConfig, type ViteUserConfig } from "vitest/config"; + +type ToAlias = (mockPath: string) => string; export const PathnameAlias = - (url: string) => - (mockPath: string): string => { - return new URL(mockPath, url).pathname; + (url: string): ToAlias => + (mockPath) => { + // console.log("mockPath", mockPath, url); + const pathname = new URL(mockPath, url).pathname; + // console.log("pathname", pathname); + return pathname; }; -const toAlias = PathnameAlias(import.meta.url); +export const extendBaseConfig = ( + root: string, + configFn: (toAlias: ToAlias) => ViteUserConfig, +) => { + const toBackendAlias = PathnameAlias(import.meta.url); + const toAlias = PathnameAlias(root); + const config = configFn(toAlias); -export const baseConfig = defineConfig({ - root: toAlias("./"), - test: { - environment: "node", - watch: false, - alias: { - sharp: toAlias("mocks/sharp.mock.js"), - canvas: toAlias("mocks/canvas.mock.js"), - "pdfjs-dist/legacy/build/pdf.js": toAlias("mocks/pdfjs.mock.js"), - "@blocknote/core": toAlias("mocks/blocknote-core.mock.js"), - "@blocknote/react/**": toAlias("mocks/blocknote-react.mock.js"), + return defineConfig({ + root: toAlias("./"), + test: { + environment: "node", + watch: false, + alias: { + sharp: toBackendAlias("lib/test/mocks/sharp.mock.js"), + canvas: toBackendAlias("lib/test/mocks/canvas.mock.js"), + "pdfjs-dist/legacy/build/pdf.js": toBackendAlias( + "lib/test/mocks/pdfjs.mock.js", + ), + "@blocknote/core": toBackendAlias( + "lib/test/mocks/blocknote-core.mock.js", + ), + "@blocknote/react/**": toBackendAlias( + "lib/test/mocks/blocknote-react.mock.js", + ), + }, + ...config.test, }, - }, - plugins: [ - viteTsconfigPaths({ - root: toAlias("./"), - }), - ], -}); + plugins: [ + viteTsconfigPaths({ + root: toAlias("./"), + }) as any, + ].concat(config.plugins ?? []), + }); +}; diff --git a/packages/@liexp/backend/vitest.config.ts b/packages/@liexp/backend/vitest.config.ts index 508519694..75bfe769f 100644 --- a/packages/@liexp/backend/vitest.config.ts +++ b/packages/@liexp/backend/vitest.config.ts @@ -1,13 +1,11 @@ import viteTsconfigPaths from "vite-tsconfig-paths"; -import { defineConfig, mergeConfig } from "vitest/config"; -import { baseConfig } from "./src/test/vitest.base-config"; +import { extendBaseConfig } from "./src/test/vitest.base-config"; -export default mergeConfig(baseConfig, defineConfig({ +export default extendBaseConfig(import.meta.url, (toAlias) => ({ test: { name: "@liexp/backend", - root: __dirname, globals: true, - include: [__dirname + "/src/**/*.spec.ts"], + include: [toAlias("src/**/*.spec.ts")], watch: false, coverage: { thresholds: { @@ -17,6 +15,6 @@ export default mergeConfig(baseConfig, defineConfig({ }, }, }, - plugins: [viteTsconfigPaths({ root: __dirname })], - root: __dirname, + plugins: [viteTsconfigPaths({ root: toAlias("./") })], + root: toAlias("./"), })); diff --git a/services/api/test/vitest.config.e2e.ts b/services/api/test/vitest.config.e2e.ts index 7a066476a..7031e3aa6 100644 --- a/services/api/test/vitest.config.e2e.ts +++ b/services/api/test/vitest.config.e2e.ts @@ -1,31 +1,22 @@ -import { defineProject, mergeConfig } from "vitest/config"; -import { - PathnameAlias, - baseConfig, -} from "@liexp/backend/lib/test/vitest.base-config.js"; +import { extendBaseConfig } from "@liexp/backend/lib/test/vitest.base-config.js"; -const toAlias = PathnameAlias(import.meta.url); - -const config = mergeConfig( - baseConfig, - defineProject({ - test: { - name: "api-e2e", - root: toAlias("../"), - globals: true, - include: [toAlias(`../src/**/*.e2e.ts`)], - setupFiles: [toAlias(`testSetup.ts`)], - globalSetup: [toAlias(`globalSetup.ts`)], - exclude: ["**/build", "**/src/migrations", "**/src/scripts"], - pool: "forks", - poolOptions: { - forks: { - singleFork: process.env.CI === "true" ? true : false, - isolate: false, - }, +const config = extendBaseConfig(import.meta.url, (toAlias) => ({ + test: { + name: "api-e2e", + root: toAlias("../"), + globals: true, + include: [toAlias(`../src/**/*.e2e.ts`)], + setupFiles: [toAlias(`testSetup.ts`)], + globalSetup: [toAlias(`globalSetup.ts`)], + exclude: ["**/build", "**/src/migrations", "**/src/scripts"], + pool: "forks", + poolOptions: { + forks: { + singleFork: process.env.CI === "true" ? true : false, + isolate: false, }, }, - }), -); + }, +})); export default config; diff --git a/services/api/test/vitest.config.spec.ts b/services/api/test/vitest.config.spec.ts index 579379143..4911c8934 100644 --- a/services/api/test/vitest.config.spec.ts +++ b/services/api/test/vitest.config.spec.ts @@ -1,17 +1,11 @@ -import { defineProject, mergeConfig } from "vitest/config"; -import { PathnameAlias, baseConfig } from "@liexp/backend/lib/test/vitest.base-config.js"; +import { extendBaseConfig } from "@liexp/backend/lib/test/vitest.base-config.js"; -const toAlias = PathnameAlias(import.meta.url); - -export default mergeConfig( - baseConfig, - defineProject({ - test: { - name: "api-spec", - root: toAlias('../'), - globals: true, - include: [ toAlias('../src/**/*.spec.ts')], - exclude: ["**/build", "**/src/migrations", "**/src/scripts"], - }, - }), -); +export default extendBaseConfig(import.meta.url, (toAlias) => ({ + test: { + name: "api-spec", + root: toAlias("../"), + globals: true, + include: [toAlias("../src/**/*.spec.ts")], + exclude: ["**/build", "**/src/migrations", "**/src/scripts"], + }, +}));