auto generate code templete for unit testing
quick switch to ut code file
Usage
- in workspace right click a file choose command
create/switch to UT file
- in editor use shortcut
ctl + alt + t to switch/create ut file
Features
- support nest framework ut creation,auto import Test Target Class
auto import & mock dependency which is injected by construstor
- if test root dir exist
base.mock.ts then it will auto added to test file
- auto update test
mock dependency when source class add new dependencies
Prerequisite
- file must use sigle quote, formatted
- using nest test framework && jest
Example
- Target Nest Class:
business.service.ts
import { Injectable } from '@nestjs/common';
import { HttpUtil } from './util/httpUtil';
@Injectable()
export class BusinessService {
constructor(private readonly httpUtil: HttpUtil) {
}
async invoke(req): Promise<any> {
return await this.httpUtil.call(req);
}
}
- genereated Test class:
business.service.test.ts
require('../base.mock');
import { Test } from '@nestjs/testing';
import { BusinessService } from '../../src/business.service';
import { HttpUtil } from '../../src/util/httpUtil';
describe('test BusinessService', () => {
let businessService: BusinessService;
class HttpUtilMock {
// todo: to mock functions
}
beforeEach(async () => {
const HttpUtilProvider = {
provide: HttpUtil,
useClass: HttpUtilMock
};
const moduleRef = await Test.createTestingModule({
imports: [],
controllers: [],
providers: [BusinessService, HttpUtilProvider],
}).compile();
businessService = moduleRef.get<BusinessService>(BusinessService);
});
test('BusinessService business', async () => {
// todo mock && call && assert
});
});
ChangeLog
- 0.0.11 fix auto import package path error
- 0.0.12 add quick switch to src file function
- 0.0.15 fix import third package path error
- 0.0.16 fix import
Inject('Interface1Impl') decorated dependency
- 0.0.18 fix
.ts replace logic, regex match .ts to .ts$
- 0.0.19 reconstruct project, use typescript ast generate ut code
- 0.1.0 fix bugs, add features: when an existing test add extra inject dependency,then auto update test files
- 0.1.6 fix udapte ut with allyName cases
| |