Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore(web): Setup in-source testing with Vitest (#13484)

Ben Bachem8ca61cf846a0
diff --git a/packages/config-eslint/base.js b/packages/config-eslint/base.js
index 1214be0d70..453718366f 100644
--- a/packages/config-eslint/base.js
+++ b/packages/config-eslint/base.js
@@ -3,6 +3,7 @@ import globals from "globals";
import tseslint from "typescript-eslint";
import turboConfig from "eslint-config-turbo/flat";
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
+import langfusePlugin from "@repo/eslint-plugin";
import "eslint-plugin-only-warn";
export default tseslint.config(
@@ -88,4 +89,15 @@ export default tseslint.config(
"@typescript-eslint/no-deprecated": "warn",
},
},
+
+ // Vitest in-source testing should only be used while developing, not in committed code.
+ {
+ name: "langfuse/no-in-source-vitest",
+ plugins: {
+ "@repo": langfusePlugin,
+ },
+ rules: {
+ "@repo/no-in-source-vitest": "warn",
+ },
+ },
);
diff --git a/packages/config-eslint/next.js b/packages/config-eslint/next.js
index d112a9db23..761e1e9a03 100644
--- a/packages/config-eslint/next.js
+++ b/packages/config-eslint/next.js
@@ -107,4 +107,15 @@ export default [
"react/no-unused-prop-types": "warn",
},
},
+
+ // Vitest in-source testing should only be used while developing, not in committed code.
+ {
+ name: "langfuse/no-in-source-vitest",
+ plugins: {
+ "@repo": langfusePlugin,
+ },
+ rules: {
+ "@repo/no-in-source-vitest": "warn",
+ },
+ },
];
diff --git a/packages/eslint-plugin/src/index.ts b/packages/eslint-plugin/src/index.ts
index a35dbbe8ab..326a58c011 100644
--- a/packages/eslint-plugin/src/index.ts
+++ b/packages/eslint-plugin/src/index.ts
@@ -1,7 +1,9 @@
import { default as noTailwindOverflowScroll } from "./rules/no-tailwind-overflow-scroll.js";
+import { default as noInSourceVitest } from "./rules/no-in-source-vitest.js";
export const plugin = {
rules: {
+ "no-in-source-vitest": noInSourceVitest,
"no-tailwind-overflow-scroll": noTailwindOverflowScroll,
},
};
diff --git a/packages/eslint-plugin/src/rules/no-in-source-vitest.test.ts b/packages/eslint-plugin/src/rules/no-in-source-vitest.test.ts
new file mode 100644
index 0000000000..6734d2c683
--- /dev/null
+++ b/packages/eslint-plugin/src/rules/no-in-source-vitest.test.ts
@@ -0,0 +1,36 @@
+import { RuleTester } from "@typescript-eslint/rule-tester";
+import * as typescriptEslintParser from "@typescript-eslint/parser";
+import rule from "./no-in-source-vitest.js";
+
+const ruleTester = new RuleTester({
+ languageOptions: {
+ parser: typescriptEslintParser,
+ parserOptions: {
+ ecmaVersion: 2022,
+ sourceType: "module",
+ ecmaFeatures: {
+ jsx: true,
+ },
+ },
+ },
+});
+
+ruleTester.run("no-in-source-vitest", rule, {
+ valid: [
+ {
+ code: `export const value = 42;`,
+ filename: "/repo/web/src/features/example/value.ts",
+ },
+ {
+ code: `if (import.meta.env.DEV) console.log("dev");`,
+ filename: "/repo/web/src/features/example/value.ts",
+ },
+ ],
+ invalid: [
+ {
+ code: `if (import.meta.vitest) {}`,
+ filename: "/repo/web/src/features/example/value.ts",
+ errors: [{ messageId: "unexpected" }],
+ },
+ ],
+});
diff --git a/packages/eslint-plugin/src/rules/no-in-source-vitest.ts b/packages/eslint-plugin/src/rules/no-in-source-vitest.ts
new file mode 100644
index 0000000000..ba7d08c22f
--- /dev/null
+++ b/packages/eslint-plugin/src/rules/no-in-source-vitest.ts
@@ -0,0 +1,29 @@
+import { createRule } from "../util.js";
+
+const rule = createRule({
+ name: "no-in-source-vitest",
+ meta: {
+ type: "suggestion",
+ docs: {
+ description:
+ "Vitest in-source testing should only be used while developing, not in committed code.",
+ },
+ messages: {
+ unexpected:
+ "Vitest in-source testing should only be used while developing, not in committed code.",
+ },
+ schema: [],
+ },
+ defaultOptions: [],
+ create(context) {
+ return {
+ 'MemberExpression[computed=false][property.name="vitest"][object.type="MetaProperty"][object.meta.name="import"][object.property.name="meta"]'(
+ node,
+ ) {
+ context.report({ node, messageId: "unexpected" });
+ },
+ };
+ },
+});
+
+export default rule;
diff --git a/web/AGENTS.md b/web/AGENTS.md
index 491d541c47..e6ca96228c 100644
--- a/web/AGENTS.md
+++ b/web/AGENTS.md
@@ -92,7 +92,9 @@ signoff of user-visible changes.
```tsx
const cardVariants = cva("rounded-md border", {
- variants: { intent: { default: "bg-background", error: "border-destructive" } },
+ variants: {
+ intent: { default: "bg-background", error: "border-destructive" },
+ },
defaultVariants: { intent: "default" },
});
@@ -117,6 +119,12 @@ signoff of user-visible changes.
unique test data over global reset helpers.
- Put pure server unit tests that do not need Postgres bootstrap under
`src/__tests__/server/unit/**` so they skip the shared DB setup hook.
+- For small utility functions, prefer Vitest in-source tests when colocated
+ coverage is the simplest option, especially when the test needs access to
+ private implementation details without widening the module API.
+- Do not extract private utility functions into separate files only to make
+ them testable. Keep them local unless the user explicitly asks for extraction
+ or the utility is meaningfully reused.
## Quick Commands
@@ -124,8 +132,9 @@ signoff of user-visible changes.
- Lint: `pnpm --filter web run lint`
- Lint fix: `pnpm --filter web run lint:fix`
- Typecheck: `pnpm --filter web run typecheck`
-- Server tests: `pnpm --filter web run test -- <pattern>`
-- Client tests: `pnpm --filter web run test-client -- <pattern>`
+- Server tests: `pnpm --filter web run test -- <args>`
+- In-source tests: `pnpm --filter web run test:in-source -- <args>`
+- Client tests: `pnpm --filter web run test-client -- <args>`
- E2E tests: `pnpm --filter web run test:e2e`
- Agent browser install to the default user-level Playwright cache: `pnpm run playwright:install`
- Build: `pnpm --filter web run build`
@@ -170,4 +179,13 @@ signoff of user-visible changes.
- Router style is Pages Router-centric; follow existing routing patterns.
- Keep tests independent; no reliance on test execution order.
- Confirm the target `*.clienttest.*` or `*.servertest.*` file exists before passing a pattern to `vitest run`; source files do not always have a matching colocated test file.
+- When passing a Vitest file or pattern through `pnpm --filter web ...`, make it
+ relative to `web/` because the script runs with `web` as the working
+ directory. Example: use `src/features/widgets/chart-library/BigNumber.tsx`,
+ not `web/src/features/widgets/chart-library/BigNumber.tsx`.
+- Prefer separate test files for components, integration coverage, and broader
+ behaviors; use Vitest in-source tests mainly for small-scoped utilities.
+- Run Vitest in-source utility coverage with `pnpm --filter web run test:in-source`;
+ do not try to target these through `test-client` or by assuming a separate
+ `*.clienttest.*`/`*.servertest.*` file exists.
- Do not hand-edit build artifacts: `.next/*`, `.next-check/*`, `dist/*`.
diff --git a/web/next.config.mjs b/web/next.config.mjs
index aea6cee31f..491aae2761 100644
--- a/web/next.config.mjs
+++ b/web/next.config.mjs
@@ -70,6 +70,11 @@ const nextConfig = {
],
poweredByHeader: false,
basePath: env.NEXT_PUBLIC_BASE_PATH,
+ compiler: {
+ define: {
+ "import.meta.vitest": "undefined",
+ },
+ },
turbopack: {
resolveAlias: {
"@langfuse/shared": "./packages/shared/src",
@@ -194,10 +199,18 @@ const nextConfig = {
];
},
- webpack(config, { isServer }) {
+ webpack(config, { isServer, webpack }) {
// Exclude Datadog packages from webpack bundling to avoid issues
// see: https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/nodejs/#bundling-with-nextjs
config.externals.push("@datadog/pprof", "dd-trace");
+
+ // Setup in-source testing: https://vitest.dev/guide/in-source.html#other-bundlers
+ config.plugins.push(
+ new webpack.DefinePlugin({
+ "import.meta.vitest": "undefined",
+ }),
+ );
+
return config;
},
};
diff --git a/web/package.json b/web/package.json
index b706276072..225973572d 100644
--- a/web/package.json
+++ b/web/package.json
@@ -17,9 +17,10 @@
"analyze": "dotenv -e ../.env -- next experimental-analyze",
"clean": "rm -rf node_modules",
"start": "dotenv -e ../.env -- sh -c 'NEXT_MANUAL_SIG_HANDLE=true next start'",
- "test": "dotenv -e ../.env.test -e ../.env -- vitest run --project server",
+ "test": "dotenv -e ../.env.test -e ../.env -- vitest run --project server --project in-source",
+ "test:in-source": "dotenv -e ../.env.test -e ../.env -- vitest run --project in-source",
"test-client": "dotenv -e ../.env.test -e ../.env -- vitest run --project client",
- "test:watch": "dotenv -e ../.env.test -e ../.env -- vitest --project client --project server",
+ "test:watch": "dotenv -e ../.env.test -e ../.env -- vitest --project client --project server --project in-source",
"test:e2e": "dotenv -e ../.env.test -e ../.env -- playwright test --reporter=line",
"test:e2e:server": "dotenv -e ../.env.test -e ../.env -- vitest run --project e2e-server",
"build-trace": "dotenv -e ../.env -- next internal trace .next/trace-turbopack"
diff --git a/web/tsconfig.json b/web/tsconfig.json
index 6df7e70241..19e590a533 100644
--- a/web/tsconfig.json
+++ b/web/tsconfig.json
@@ -15,7 +15,7 @@
]
},
"strictNullChecks": true,
- "types": ["node", "vitest/globals", "@testing-library/jest-dom"]
+ "types": ["node", "vitest/globals", "vitest/importMeta", "@testing-library/jest-dom"]
},
"exclude": [
"node_modules",
diff --git a/web/vitest.config.mts b/web/vitest.config.mts
index 6279cdd21f..0f15395590 100644
--- a/web/vitest.config.mts
+++ b/web/vitest.config.mts
@@ -3,6 +3,13 @@ import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
import { VitestCiReporter } from "../scripts/vitest/ci-reporter";
+const sharedExclude = [
+ "**/node_modules/**",
+ "**/.next/**",
+ "**/.next-check/**",
+ "**/dist/**",
+];
+
export default defineConfig({
plugins: [tsconfigPaths(), react()],
test: {
@@ -18,11 +25,27 @@ export default defineConfig({
},
},
projects: [
+ {
+ extends: true,
+ test: {
+ name: "in-source",
+ includeSource: ["./src/**/*.{ts,tsx}"],
+ exclude: [
+ ...sharedExclude,
+ "src/**/*.clienttest.{ts,tsx}",
+ "src/**/*.servertest.{ts,tsx}",
+ "src/**/__tests__/**",
+ "src/**/__e2e__/**",
+ ],
+ environment: "node",
+ },
+ },
{
extends: true,
test: {
name: "client",
include: ["src/**/*.clienttest.{ts,tsx}"],
+ exclude: sharedExclude,
environment: "jsdom",
setupFiles: ["@testing-library/jest-dom/vitest"],
},
@@ -32,7 +55,7 @@ export default defineConfig({
test: {
name: "server",
include: ["src/**/server/**/*.servertest.{ts,tsx}"],
- exclude: ["src/__e2e__/**"],
+ exclude: [...sharedExclude, "src/__e2e__/**"],
environment: "node",
setupFiles: ["./src/__tests__/after-teardown.ts"],
globalSetup: ["./src/__tests__/vitest-global-teardown.ts"],
@@ -43,7 +66,7 @@ export default defineConfig({
test: {
name: "e2e-server",
include: ["src/**/*.servertest.{ts,tsx}"],
- exclude: ["src/__tests__/**"],
+ exclude: [...sharedExclude, "src/__tests__/**"],
environment: "node",
setupFiles: ["./src/__tests__/after-teardown.ts"],
globalSetup: ["./src/__tests__/vitest-global-teardown.ts"],