Итак, я хочу протестировать контроллер, но всегда получаю одну и ту же ошибку, и я не очень знаком с Jest. Я работаю с php, и я уже провел некоторые модульные тесты в phpUnit, но с Джестом у меня действительно возникают проблемы с пониманием, как это сделать.
Вот мое сообщение об ошибке
● 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;
}
}
И мой тест
import { Test, TestingModule } from '@nestjs/testing';
import { CustomerController } from './customer.controller';
import {CustomerService} from './customer.service';
import {CreateCustomerDto} from './dto/customer/customer.dto';
import {Customer} from './interfaces/customer.interface';
import {InjectModel} from '@nestjs/mongoose';
jest.mock('./customer.service.ts');
jest.mock('./customer.controller.ts');
jest.mock('./interfaces/customer.interface.ts');
describe('Customer Controller', () => {
let customerController: CustomerController;
let customerService: CustomerService;
beforeAll(async () => {
const module = await Test.createTestingModule({
controllers: [CustomerController],
providers: [CustomerService],
}).compile();
customerController = module.get(CustomerController);
customerService = module.get<CustomerService>(CustomerService);
});
describe('Create Post', () => {
const dto = new CreateCustomerDto();
const res = 200;
const customerModel: Customer;
it('should return an collection of customer when created ', async () => {
const expectedResult = new CustomerService(@InjectModel(customerModel<Customer>));
jest.spyOn(customerService, 'createCustomer').mockResolvedValue(customerModel);
expect(await customerController.createPost(res, dto)).toBe(expectedResult);
});
});
});
**Any thoughts ?**