@@ -211,6 +211,8 @@ await createModuleRunnerStub();
211211await createNodeEntry ( ) ;
212212await copyBrowserClientFiles ( ) ;
213213await createBrowserEntryFiles ( ) ;
214+ await patchModuleAugmentations ( ) ;
215+ await patchChaiTypeReference ( ) ;
214216const pluginExports = await createPluginExports ( ) ;
215217await mergePackageJson ( pluginExports ) ;
216218await validateExternalDeps ( ) ;
@@ -1816,6 +1818,94 @@ export * from '../dist/@vitest/browser/context.d.ts'
18161818 console . log ( ' Created browser/context.d.ts' ) ;
18171819}
18181820
1821+ /**
1822+ * Patch module augmentations in global.d.*.d.ts files to use relative paths.
1823+ *
1824+ * The original vitest types use module augmentation like:
1825+ * declare module "@vitest/expect" { interface Assertion<T> { toMatchSnapshot: ... } }
1826+ *
1827+ * Since we bundle @vitest /* packages inside dist/@vitest /*, the bare specifier
1828+ * "@vitest/expect" doesn't exist as a package for consumers. This breaks the
1829+ * module augmentation - TypeScript can't find @vitest/expect to augment.
1830+ *
1831+ * The fix: Change module augmentation to use relative paths that TypeScript CAN resolve:
1832+ * declare module "../@vitest/expect/index.js" { ... }
1833+ *
1834+ * This makes TypeScript augment the same module that our index.d.ts imports from,
1835+ * so the augmented properties (toMatchSnapshot, toMatchInlineSnapshot, etc.)
1836+ * appear on the Assertion type that consumers import.
1837+ */
1838+ async function patchModuleAugmentations ( ) {
1839+ console . log ( '\nPatching module augmentations in global.d.*.d.ts files...' ) ;
1840+
1841+ const chunksDir = join ( distDir , 'chunks' ) ;
1842+ const globalDtsFiles : string [ ] = [ ] ;
1843+
1844+ // Find all global.d.*.d.ts files
1845+ for await ( const file of fsGlob ( join ( chunksDir , 'global.d.*.d.ts' ) ) ) {
1846+ globalDtsFiles . push ( file ) ;
1847+ }
1848+
1849+ if ( globalDtsFiles . length === 0 ) {
1850+ console . log ( ' No global.d.*.d.ts files found' ) ;
1851+ return ;
1852+ }
1853+
1854+ // Module augmentation mappings: bare specifier -> relative path from chunks/
1855+ const augmentationMappings : Record < string , string > = {
1856+ '@vitest/expect' : '../@vitest/expect/index.js' ,
1857+ '@vitest/runner' : '../@vitest/runner/index.js' ,
1858+ } ;
1859+
1860+ for ( const file of globalDtsFiles ) {
1861+ let content = await readFile ( file , 'utf-8' ) ;
1862+ let modified = false ;
1863+
1864+ for ( const [ bareSpecifier , relativePath ] of Object . entries ( augmentationMappings ) ) {
1865+ const oldPattern = `declare module "${ bareSpecifier } "` ;
1866+ const newPattern = `declare module "${ relativePath } "` ;
1867+
1868+ if ( content . includes ( oldPattern ) ) {
1869+ content = content . replaceAll ( oldPattern , newPattern ) ;
1870+ modified = true ;
1871+ console . log ( ` Patched: ${ bareSpecifier } -> ${ relativePath } in ${ basename ( file ) } ` ) ;
1872+ }
1873+ }
1874+
1875+ if ( modified ) {
1876+ await writeFile ( file , content , 'utf-8' ) ;
1877+ }
1878+ }
1879+ }
1880+
1881+ /**
1882+ * Add triple-slash reference to @types/chai in @vitest/expect types.
1883+ *
1884+ * The @vitest/expect types use the Chai namespace (e.g., Chai.Assertion) which
1885+ * is defined in @types/chai. Without a reference directive, TypeScript won't
1886+ * automatically find the Chai types, causing the `not` property and other
1887+ * chai-specific features to be missing from the Assertion interface.
1888+ */
1889+ async function patchChaiTypeReference ( ) {
1890+ console . log ( '\nAdding @types/chai reference to @vitest/expect types...' ) ;
1891+
1892+ const expectIndexDts = join ( distDir , '@vitest/expect/index.d.ts' ) ;
1893+
1894+ let content = await readFile ( expectIndexDts , 'utf-8' ) ;
1895+
1896+ // Check if reference already exists
1897+ if ( content . includes ( '/// <reference types="chai"' ) ) {
1898+ console . log ( ' Reference already exists, skipping' ) ;
1899+ return ;
1900+ }
1901+
1902+ // Add triple-slash reference at the top
1903+ content = `/// <reference types="chai" />\n${ content } ` ;
1904+
1905+ await writeFile ( expectIndexDts , content , 'utf-8' ) ;
1906+ console . log ( ' Added /// <reference types="chai" /> to @vitest/expect/index.d.ts' ) ;
1907+ }
1908+
18191909/**
18201910 * Create /plugins/* exports for all copied @vitest/* packages.
18211911 * This allows pnpm overrides to redirect @vitest/* imports to our copied versions.
0 commit comments