[engsys] Allow min/max tests to use vitest config of the target package (#31343)
### Packages impacted by this PR ### Issues associated with this PR Resolves #30578 ### Describe the problem that is addressed by this PR While simple settings like `test-timeout` and `hook-timeout` are often defined in package.json scripts, there are other settings that are specified in the vitest.config.ts of the target package. Our existing setup uses the default vitest template which does not work for libraries that use customized config. This PR improves that experience by creating a 3-way-merge of: 1. The base config 2. The template config (which specifies the test patterns) 3. The package's config Only done for node right now. If you need it for browser, you now have a path forward ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? There are some gotchas here: 1. testTimeout is defined in package.json which takes precedence over any config files I believe - it may be surprising that your testTimeout does not take (note that this is somewhat the case today) 2. The package's config adds files under `test/**/*.spec.ts` to the include pattern. This is fine, because the min/max tests are placed under `test/public` so that pattern will not match anything I really want to clean up min/max - but this is not the time
This commit is contained in:
Родитель
09df4d579c
Коммит
2e53d49699
|
@ -4,7 +4,34 @@
|
|||
import { defineConfig, mergeConfig } from "vitest/config";
|
||||
import viteConfig from "../../../../../vitest.shared.config.ts";
|
||||
|
||||
export default mergeConfig(
|
||||
// The goal of this file is to create a unified test configuration for Vitest
|
||||
// by combining a base configuration shared across packages with package-specific settings.
|
||||
// This ensures that we maintain consistent test behavior when running min/max tests,
|
||||
// while allowing for individual package overrides where necessary.
|
||||
|
||||
// 1. Try to load the package-specific vitest config, if available.
|
||||
// This allows each package to have its own tailored test configuration,
|
||||
// but it's optional. If the package config isn't present, we proceed with a default setup.
|
||||
let packageConfig = {};
|
||||
try {
|
||||
packageConfig = (await import("../../vitest.config.ts")).default;
|
||||
} catch (e: any) {
|
||||
if (e.code === "ERR_MODULE_NOT_FOUND") {
|
||||
// If no specific config is found, we log this fact.
|
||||
// This ensures developers know when package-specific settings are missing,
|
||||
// but it's not an error — we simply default to the shared config.
|
||||
console.warn(
|
||||
`vitest.config.ts not found in the expected location (sdk/<service-directory>/<package-directory>/vitest.config.ts) - package's vitest config will not be included`,
|
||||
);
|
||||
} else {
|
||||
// Any other error here indicates a real issue, so we rethrow it.
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Merge the shared base config with some standard test settings for min/max tests.
|
||||
// These settings apply to all packages unless specifically overridden,
|
||||
const baseConfig = mergeConfig(
|
||||
viteConfig,
|
||||
defineConfig({
|
||||
test: {
|
||||
|
@ -13,3 +40,13 @@ export default mergeConfig(
|
|||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// 3. Merge the package-specific config with the base config to produce the final result.
|
||||
// This allows for package-level customizations while still adhering to the shared setup,
|
||||
// ensuring consistency across the entire codebase.
|
||||
const finalViteConfig = mergeConfig(baseConfig, packageConfig);
|
||||
|
||||
console.log("Using the following Vitest configuration:");
|
||||
console.log(JSON.stringify(finalViteConfig, null, 2));
|
||||
|
||||
export default finalViteConfig;
|
||||
|
|
Загрузка…
Ссылка в новой задаче