Files
nest/vitest.config.mts
T

66 lines
2.1 KiB
TypeScript

import { existsSync } from 'fs';
import type { Plugin } from 'vite';
import { defineConfig } from 'vitest/config';
/**
* Resolves `.js` imports to `.ts` source files when both exist side-by-side.
*
* The NestJS monorepo keeps compiled `.js` output next to the `.ts` sources.
* Without this plugin, Vite resolves `import '…/foo.js'` to the compiled JS
* instead of the original TypeScript source.
*/
function resolveTypescriptSource(): Plugin {
return {
name: 'resolve-ts-source',
enforce: 'pre',
async resolveId(source, importer, options) {
if (!importer) return null;
const resolved = await this.resolve(source, importer, {
...options,
skipSelf: true,
});
if (resolved && !resolved.external && resolved.id.endsWith('.js')) {
const tsPath = resolved.id.replace(/\.js$/, '.ts');
if (existsSync(tsPath)) return tsPath;
}
return null;
},
};
}
export default defineConfig({
// Spec files are excluded from every tsconfig in the repo, so Oxc cannot pick
// up `experimentalDecorators`/`emitDecoratorMetadata` from them and would emit
// TC39 decorators (which reject parameter decorators). Configure them here.
oxc: {
decorator: {
legacy: true,
emitDecoratorMetadata: true,
},
assumptions: {
setPublicClassFields: true,
},
typescript: {
removeClassFieldsWithoutInitializer: true,
},
},
test: {
globals: true,
root: './',
include: ['packages/**/*.spec.ts'],
alias: {
'@nestjs/common': './packages/common',
'@nestjs/core': './packages/core',
'@nestjs/microservices': './packages/microservices',
'@nestjs/websockets': './packages/websockets',
'@nestjs/testing': './packages/testing',
'@nestjs/platform-express': './packages/platform-express',
'@nestjs/platform-ws': './packages/platform-ws',
'@nestjs/platform-fastify': './packages/platform-fastify',
'@nestjs/platform-socket.io': './packages/platform-socket.io',
},
setupFiles: ['reflect-metadata'],
},
plugins: [resolveTypescriptSource()],
});