Skip to content

Instantly share code, notes, and snippets.

@mdubourg001
Last active January 8, 2026 11:16
Show Gist options
  • Select an option

  • Save mdubourg001/aa561344f3f912513863409e7f288ad5 to your computer and use it in GitHub Desktop.

Select an option

Save mdubourg001/aa561344f3f912513863409e7f288ad5 to your computer and use it in GitHub Desktop.
Vitest import time reporter
import { TestModule } from 'vitest/node';
import { DefaultReporter } from 'vitest/reporters';
type ImportTimeConfig = {
limit: 'all' | number;
orderBy: 'avg' | 'total';
};
type TestModuleWithImportTimings = TestModule & {
task: { importDurations: Record<string, { selfTime: number }> };
};
/**
* This reporter collects and reports the time taken to import each module during the test run.
*/
export class ImportTimeReporter extends DefaultReporter {
private config: ImportTimeConfig;
private timings = new Map<string, { avg: number; total: number }>();
constructor(
config: ImportTimingConfig = {
limit: 'all',
orderBy: 'avg',
},
) {
super();
this.config = config;
}
onTestModuleCollected(module: TestModule): void {
try {
for (const [path, meta] of Object.entries(
(module as TestModuleWithImportTimings).task.importDurations,
)) {
const actual = this.timings.get(path);
this.timings.set(
path,
// average the previous timing with the new one to smooth out variations
{
avg: actual
? (actual.avg + meta.selfTime) / 2
: meta.selfTime,
total: (actual ? actual.total : 0) + meta.selfTime,
},
);
}
} catch (e) {
this.error('Error collecting import timings:', e);
}
}
onTestRunEnd(): void {
// log timings by avg ascending order
const sortedTimings = Array.from(this.timings.entries()).sort(
(a, b) =>
this.config.orderBy === 'avg'
? a[1].avg - b[1].avg
: a[1].total - b[1].total,
);
this.log('\nImport Timing Report:');
for (const [path, timings] of sortedTimings.slice(
this.config.limit === 'all'
? 0
: sortedTimings.length - this.config.limit,
sortedTimings.length,
)) {
this.log(
` ${timings.avg.toFixed(2).padStart(8)}ms (total: ${timings.total.toFixed(2).padStart(8)}ms) - ${path}`,
);
}
this.log('');
}
}
import { defineConfig } from 'vitest/config';
import { ImportTimeReporter } from './import-time-reporter';
export default defineConfig({
// ...
test: {
globals: true,
// ...
// default { limit: 'all', orderBy: 'avg' }
reporters: [new ImportTimeReporter({ limit: 10, orderBy: 'total' })],
}
});
@mdubourg001
Copy link
Author

mdubourg001 commented Jan 7, 2026

Imports (and often barrel files) are what is killing Vitest performances the most. This plugin allows listing which module take the most time importing in order to know what to improve (most probably barrel files or big external dependencies).

Example output:

Import Timing Report:
      9.27ms (total:  3941.00ms)  -  /Users/johndoe/foobar/src/constants/reducer-entities.ts
     37.84ms (total:  4996.67ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/@testing-library+user-event@14.6.1_@testing-library+dom@10.4.1/node_modules/@testing-library/user-event/dist/esm/index.js?v=a5b91ab6
     38.51ms (total:  5343.73ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/lucide-react@0.548.0_react@19.2.0/node_modules/lucide-react/dist/cjs/lucide-react.js?v=a5b91ab6
     39.83ms (total:  5838.69ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/msw@2.12.2_@types+node@22.17.0_typescript@5.9.3/node_modules/msw/lib/node/index.mjs?v=a5b91ab6
     33.10ms (total:  6099.67ms)  -  /Users/johndoe/foobar/node_modules/.vite/vitest/db8923fc5c77700780f28c8f39bb60dacec2f56b/deps/date-fns.js?v=a5b91ab6
     67.38ms (total:  9567.98ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/@testing-library+jest-dom@6.8.0/node_modules/@testing-library/jest-dom/dist/vitest.mjs?v=a5b91ab6
     53.34ms (total: 10733.34ms)  -  /Users/johndoe/foobar/src/msw/node/handlers.ts
     82.84ms (total: 12844.72ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/redux-form@8.3.9_react-redux@9.2.0_@types+react@19.2.2_react@19.2.0_redux@5.0.1__react@19.2.0_redux@5.0.1/node_modules/redux-form/lib/index.js?v=a5b91ab6
    109.40ms (total: 16849.66ms)  -  /Users/johndoe/foobar/node_modules/.pnpm/@testing-library+react@16.3.0_@testing-library+dom@10.4.1_@types+react-dom@19.2.2_@type_15d474de7481bb0af842c1b1705139a8/node_modules/@testing-library/react/dist/index.js?v=a5b91ab6
    101.12ms (total: 21016.93ms)  -  /Users/johndoe/foobar/src/redux/store.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment