-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): make vitest base config extensible
- Loading branch information
1 parent
43aef56
commit 90b2c1f
Showing
4 changed files
with
74 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? []), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}, | ||
})); |