add test case

This commit is contained in:
liuyi 2025-05-22 23:12:26 +08:00
parent db465b70c8
commit 9ab2cd121f
2 changed files with 74 additions and 2 deletions

View File

@ -74,8 +74,10 @@
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"moduleNameMapper": {
"^@/(.*)$": "<rootDir>/src/$1"
},
"testMatch": ["<rootDir>/test/**/*.test.ts"],
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},

70
test/category.test.ts Normal file
View File

@ -0,0 +1,70 @@
import { describe } from 'node:test';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { Test, TestingModule } from '@nestjs/testing';
import { DataSource } from 'typeorm';
import { database } from '@/config';
import { ContentModule } from '@/modules/content/content.module';
import { CategoryController } from '@/modules/content/controllers';
import { CategoryRepository } from '@/modules/content/repositories';
import { DatabaseModule } from '@/modules/database/database.module';
describe('category test', () => {
let controller: CategoryController;
let datasource: DataSource;
let app: NestFastifyApplication;
let repository: CategoryRepository;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [ContentModule, DatabaseModule.forRoot(database)],
}).compile();
app = module.createNestApplication<NestFastifyApplication>(new FastifyAdapter());
await app.init();
await app.getHttpAdapter().getInstance().ready();
controller = module.get<CategoryController>(CategoryController);
repository = module.get<CategoryRepository>(CategoryRepository);
datasource = module.get<DataSource>(DataSource);
repository.clear();
});
it('check datasource', () => {
expect(datasource).toBeDefined();
});
it('create new category', () => {
expect(controller).toBeDefined();
});
it('/category', () => {
return app
.inject({
method: 'GET',
url: '/category',
})
.then((result) => {
console.log(result.json());
expect(result.statusCode).toEqual(200);
expect(result.json().items).toEqual([]);
});
});
it('/category/create', () => {
return app
.inject({
method: 'POST',
url: '/category',
body: { name: 'hhhhh' },
})
.then((result) => {
console.log(result.json());
expect(result.statusCode).toEqual(201);
});
});
afterAll(async () => {
await datasource.destroy(); // 关闭数据库连接
await app.close();
});
});