Skip to content

Commit

Permalink
fix(coverage): expensive regexp hangs v8 report generation (#5259)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Feb 22, 2024
1 parent ec6f56a commit d68a739
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 20 deletions.
22 changes: 22 additions & 0 deletions examples/nestjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@vitest/example-nestjs",
"type": "module",
"private": true,
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:run": "vitest run"
},
"devDependencies": {
"@nestjs/common": "^10.3.3",
"@nestjs/testing": "^10.3.3",
"@vitest/coverage-v8": "^1.3.1",
"unplugin-swc": "^1.4.4",
"vitest": "1.3.1"
},
"stackblitz": {
"startCommand": "npm run test:ui"
}
}
17 changes: 17 additions & 0 deletions examples/nestjs/src/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Body, Controller, Get, Post } from '@nestjs/common'
import type { Cat, CatsService } from './cats.service'

@Controller('cats')
export class CatsController {
constructor(private catsService: CatsService) {}

@Post()
async create(@Body() createCatDto: Cat) {
this.catsService.create(createCatDto)
}

@Get()
async findAll(): Promise<Cat[]> {
return this.catsService.findAll()
}
}
20 changes: 20 additions & 0 deletions examples/nestjs/src/cats.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common'

export interface Cat {
name: string
age: number
breed: string
}

@Injectable()
export class CatsService {
private readonly cats: Cat[] = []

create(cat: Cat) {
this.cats.push(cat)
}

findAll(): Cat[] {
return this.cats
}
}
23 changes: 23 additions & 0 deletions examples/nestjs/test/nestjs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { CatsController } from '../src/cats.controller'
import type { Cat } from '../src/cats.service'
import { CatsService } from '../src/cats.service'

describe('CatsController', () => {
let catsController: CatsController
let catsService: CatsService

beforeEach(() => {
catsService = new CatsService()
catsController = new CatsController(catsService)
})

describe('findAll', () => {
it('should return an array of cats', async () => {
const result = ['test'] as unknown as Cat[]
vi.spyOn(catsService, 'findAll').mockImplementation(() => result)

expect(await catsController.findAll()).toBe(result)
})
})
})
6 changes: 6 additions & 0 deletions examples/nestjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"experimentalDecorators": true
}
}
32 changes: 32 additions & 0 deletions examples/nestjs/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import swc from 'unplugin-swc'
import { defineConfig } from 'vitest/config'

export default defineConfig({
plugins: [
swc.vite({
jsc: {
target: 'esnext',
parser: {
syntax: 'typescript',
decorators: true,
},
transform: {
legacyDecorator: true,
decoratorMetadata: true,
},
},
}),
],
test: {
coverage: {
enabled: true,
provider: 'v8',
thresholds: {
branches: 100,
functions: 57.14,
lines: 81.08,
statements: 81.08,
},
},
},
})
2 changes: 1 addition & 1 deletion packages/coverage-v8/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const WRAPPER_LENGTH = 185

// Note that this needs to match the line ending as well
const VITE_EXPORTS_LINE_PATTERN = /Object\.defineProperty\(__vite_ssr_exports__.*\n/g
const DECORATOR_METADATA_PATTERN = /_ts_metadata\("design:paramtypes"(\s|.)+?]\),/g
const DECORATOR_METADATA_PATTERN = /_ts_metadata\("design:paramtypes", \[[^\]]*?\]\),*/g
const DEFAULT_PROJECT = Symbol.for('default-project')

const debug = createDebug('vitest:coverage')
Expand Down
Loading

0 comments on commit d68a739

Please sign in to comment.