8. In-source testing
export function add(a: number, b: number) { return a + b }
if (import.meta.vitest) { const { it, expect } = import.meta.vitest it('add', () => { expect(add(1, 1)).toBe(2) })}- Gated entirely by the
if (import.meta.vitest)guard;test/it/expect/etc. are pulled offimport.meta.vitestitself (an injected object), not from a'vitest'import specifier, the extractor’s “how did this binding get here” resolution needs a special case: aMemberExpressiondestructure offimport.meta.vitestis a Vitest-global-equivalent binding, regardless of the file’sglobalssetting. - Governed by
includeSourceconfig:string[]glob list, resolved relative toroot, default[](feature is opt-in, off unless a project lists source globs, e.g.includeSource: ['src/**/*.{js,ts}']). A file only contributes in-source tests if it matchesincludeSource(in addition to actually containingimport.meta.vitestblocks). Note this is independent of, and additive to, the regularincludetest-file glob, a file can be scanned for in-source tests without matchingincludeat all, since it’s not a “test file” in the ordinary sense (it’s application source with embedded tests). - Production exclusion is a build-time concern (Vite
define: { 'import.meta.vitest': 'undefined' }enabling dead-code elimination), irrelevant to the static extractor except as confirmation thatimport.meta.vitestguards are the sole and idiomatic gate; the extractor doesn’t need to model bundler DCE, just recognize the guard shape.
Back to the Vitest surface index.