因此,我想测试一个控制器,但是我总是遇到相同的错误,而且我对Jest并不真正熟悉。 我使用php,并且已经在phpUnit中进行了一些单元测试,但是使用Jest,我确实在理解操作上遇到了一些麻烦。
这是我的错误消息
<div id="stats">
<div class="box">
<i class="fas fa-cloud"></i>
<p>Members</p>
</div>
<div class="box">
<i class="fas fa-cloud"></i>
<p>Awards</p>
</div>
<div class="box">
<i class="fas fa-cloud"></i>
<p>Countries</p>
</div>
</div>
我的控制器
● Test suite failed to run
SyntaxError: /home/webjump-nb103/Projects/development/workspace/stare/customer/src/customer/customer.controller.spec.ts: Unexpected token, expected ";" (13:26)
11 |
12 | describe('Customer Controller', () => {
> 13 | let customerController: CustomerController;
| ^
14 | let customerService: CustomerService;
15 |
16 | beforeAll(async () => {
at Parser.raise (node_modules/@babel/parser/src/parser/location.js:41:63)
at Parser.unexpected (node_modules/@babel/parser/src/parser/util.js:150:16)
at Parser.semicolon (node_modules/@babel/parser/src/parser/util.js:123:40)
at Parser.parseVarStatement (node_modules/@babel/parser/src/parser/statement.js:703:10)
at Parser.parseStatementContent (node_modules/@babel/parser/src/parser/statement.js:216:21)
at Parser.parseStatement (node_modules/@babel/parser/src/parser/statement.js:146:17)
at Parser.parseBlockOrModuleBlockBody (node_modules/@babel/parser/src/parser/statement.js:865:25)
at Parser.parseBlockBody (node_modules/@babel/parser/src/parser/statement.js:841:10)
at Parser.parseBlock (node_modules/@babel/parser/src/parser/statement.js:818:10)
at Parser.parseFunctionBody (node_modules/@babel/parser/src/parser/expression.js:1964:24)
**我的服务**
import {Controller, HttpStatus, Post, Res, Body, ValidationPipe, Put, Param, Get} from '@nestjs/common';
import {CreateCustomerDto} from './dto/customer/customer.dto';
import {CustomerService} from './customer.service';
@Controller('customer')
export class CustomerController {
constructor(private readonly customerService: CustomerService) { }
@Post()
async createPost(@Res() res, @Body(ValidationPipe) createCustomerDto: CreateCustomerDto ) {
const customer = await this.customerService.createCustomer(createCustomerDto);
return res.status(HttpStatus.OK).json({
customer: customer
});
}
@Put('update/:id')
async createUpdate(@Param('id') id: string, @Res() res, @Body(ValidationPipe) createCustomerDto: CreateCustomerDto) {
const customer = await this.customerService.updateCustomer(createCustomerDto, id);
return res.status(HttpStatus.OK).json({
customer: customer
});
}
}
}
还有我的测试
import {Injectable} from '@nestjs/common';
import {Model} from 'mongoose';
import {InjectModel} from '@nestjs/mongoose';
import {Customer} from './interfaces/customer.interface';
import {CreateCustomerDto} from './dto/customer/customer.dto';
@Injectable()
export class CustomerService {
constructor(@InjectModel('customer') private readonly customerModel: Model<Customer>) {}
async createCustomer(createCustomerDto: CreateCustomerDto ): Promise<Customer> {
const customer = new this.customerModel(createCustomerDto);
return customer.save();
}
async updateCustomer(createCustomerDto: CreateCustomerDto, id: string): Promise<Customer> {
const customer = await this.customerModel
.findByIdAndUpdate(id, createCustomerDto, {new: true});
return customer;
}
答案 0 :(得分:0)
正如评论中已经提到的,请确保已正确配置了玩笑。尝试将以下内容添加/修改到您的package.json
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}