test(repos): service 测试用例,工作目录改为从配置文件中读取。

This commit is contained in:
Ivan 2021-02-24 11:11:47 +08:00
parent 1e7c594e72
commit 625ed18ae9
2 changed files with 20 additions and 20 deletions

View File

@ -16,13 +16,13 @@ import configuration from './commons/config/configuration';
}), }),
TypeOrmModule.forRootAsync({ TypeOrmModule.forRootAsync({
imports: [ConfigModule], imports: [ConfigModule],
useFactory: (cnfigService: ConfigService) => ({ useFactory: (configService: ConfigService) => ({
type: 'postgres', type: 'postgres',
host: cnfigService.get<string>('db.postgres.host'), host: configService.get<string>('db.postgres.host'),
port: cnfigService.get<number>('db.postgres.port'), port: configService.get<number>('db.postgres.port'),
username: cnfigService.get<string>('db.postgres.username'), username: configService.get<string>('db.postgres.username'),
password: cnfigService.get<string>('db.postgres.password'), password: configService.get<string>('db.postgres.password'),
database: cnfigService.get<string>('db.postgres.database'), database: configService.get<string>('db.postgres.database'),
synchronize: true, synchronize: true,
autoLoadEntities: true, autoLoadEntities: true,
}), }),

View File

@ -2,11 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm'; import { getRepositoryToken } from '@nestjs/typeorm';
import { Project } from '../projects/project.entity'; import { Project } from '../projects/project.entity';
import { ReposService } from './repos.service'; import { ReposService } from './repos.service';
import { ConfigService } from '@nestjs/config'; import { ConfigService, ConfigModule } from '@nestjs/config';
import { rm } from 'fs/promises'; import { rm } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
import configuration from '../commons/config/configuration';
const workspacesRoot = 'E:\\Projects\\demos\\workspaces';
describe('ReposService', () => { describe('ReposService', () => {
let service: ReposService; let service: ReposService;
@ -21,34 +20,35 @@ describe('ReposService', () => {
}), }),
), ),
})); }));
let workspacesRoot: string;
afterEach(async () => { afterEach(async () => {
await rm(join(workspacesRoot, 'test1'), { await rm(join(workspacesRoot, 'test1'), {
recursive: true, recursive: true,
}).catch(() => undefined); }).catch(() => undefined);
}); });
beforeEach(async () => { beforeEach(async () => {
await rm(join(workspacesRoot, 'test1'), {
recursive: true,
}).catch(() => undefined);
const module: TestingModule = await Test.createTestingModule({ const module: TestingModule = await Test.createTestingModule({
imports: [
ConfigModule.forRoot({
load: [configuration],
}),
],
providers: [ providers: [
ReposService, ReposService,
{ {
provide: getRepositoryToken(Project), provide: getRepositoryToken(Project),
useFactory: repositoryMockFactory, useFactory: repositoryMockFactory,
}, },
{
provide: ConfigService,
useValue: {
get() {
return workspacesRoot;
},
},
},
], ],
}).compile(); }).compile();
service = module.get<ReposService>(ReposService); service = module.get<ReposService>(ReposService);
const configServer = module.get<ConfigService>(ConfigService);
workspacesRoot = configServer.get<string>('workspaces.root');
await rm(join(workspacesRoot, 'test1'), {
recursive: true,
}).catch(() => undefined);
}); });
it('should be defined', () => { it('should be defined', () => {