我正在测试一个打字稿控制器公共方法,它调用控制器的私有方法,但是当我看到覆盖范围时,我发现私有方法没有被覆盖。下面是示例代码。我正在调用内部调用私有方法 transformUserActivity 来转换响应数据的控制器的 readActivity。当我看到代码覆盖率时,它说 transformUserActivity 没有被覆盖。
测试代码 audtiController.test.ts
import { AuditController } from '../src/auditController'
import { MxGroup } from "isdk"
let auditControllerInstance: AuditController
describe('Audit Test Suit', () => {
test('Response Test', () => {
mxGroup.readUserActivites = jest.fn(() => {
return new Promise((resolve, reject) => {
resolve(require('./__mockdata__/activityAuditData'))
})
})
auditControllerInstance = AuditController.getInstance()
let activityTypes = []
let users = [{ "id": "dadfq1daff12" }]
let fromTime = 1575138600000
let toTime = 1576002599999
auditControllerInstance.readActivity(users, fromTime, toTime, activityTypes).then(data => {
expect(data).toMatchSnapshot()
})
})
})
控制器代码 auditController.ts
@transformError()
readActivity (users: UserIdentity[], fromTime: number, toTime: number, type?: string[]) {
const request = this.mxGroup.readUserActivites(users, fromTime, toTime, type)
this.currentRequest = request
return new Promise((resolve, reject) => {
request.then((activities: UserActivityLog[]) => {
resolve(this.transformUserActivity(activities))
}).catch(reject)
})
}
private transformUserActivity(activities: UserActivityLog[]) {]
activities.forEach((activity: UserActivityLog) => {
if (activity.activities) {
activity.activities.forEach((act: CUserActivityLogEntry) => {
let activity_detail: object = null
try {
activity_detail = this.transformClientPostLogAction(act.detail)
} catch (e) {
console.log(e)
}
act.activity_detail = activity_detail
this.transformCommonInfo(act.detail, activity_detail)
});
}
})
return activities
}