Skip to content

Commit

Permalink
fix(backend): make vitest base config extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
ascariandrea committed Jan 23, 2025
1 parent 43aef56 commit 90b2c1f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
65 changes: 42 additions & 23 deletions packages/@liexp/backend/src/test/vitest.base-config.ts
Original file line number Diff line number Diff line change
@@ -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 ?? []),
});
};
12 changes: 5 additions & 7 deletions packages/@liexp/backend/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -17,6 +15,6 @@ export default mergeConfig(baseConfig, defineConfig({
},
},
},
plugins: [viteTsconfigPaths({ root: __dirname })],
root: __dirname,
plugins: [viteTsconfigPaths({ root: toAlias("./") })],
root: toAlias("./"),
}));
43 changes: 17 additions & 26 deletions services/api/test/vitest.config.e2e.ts
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 10 additions & 16 deletions services/api/test/vitest.config.spec.ts
Original file line number Diff line number Diff line change
@@ -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"],
},
}));

0 comments on commit 90b2c1f

Please sign in to comment.