In the below code my test case was passed as expected but i am using stryker for mutation testing , handleError function is survived in mutation testing , so i want to kill the mutant by testing the handleError function is being called or not. need to help to test the private function.
i tried spyOn but didn't work
const orderBuilderSpy = jest.spyOn(orderBuilder, 'build')
const handleError = jest.fn()
expect(rderBuilderSpy).toHaveBeenCalledWith(handleError)
// code written in nestJS/typescript
export class OrderBuilder {
private amount: number
public withAmount(amount: number): BuyOrderBuilder {
this.amount = amount
return this
}
public build(): TransactionRequest {
this.handleError()
return {
amount: this.amount,
acceptedWarningRules: [
{
ruleNumber: 4464
}
]
}
}
private handleError() {
const errors: string[] = []
const dynamicFields: string[] = [
'amount',
]
dynamicFields.forEach((field: string) => {
if (!this[field]) {
errors.push(field)
}
})
if (errors.length > 0) {
const errorMessage = errors.join()
throw new Error(`missing ${errorMessage} field in order`)
}
}
}
// test
describe('Order Builder', () => {
it('should test the handleError', () => {
const orderBuilder = new OrderBuilder()
const errorMessage = new Error(
`missing amount field in order`
)
try {
orderBuilder.build()
} catch (error) {
expect(error).toEqual(errorMessage)
}
});
});
Via ts-jest ts-jest is a TypeScript preprocessor with source map support for Jest that lets you use Jest to test projects written in TypeScript.
For TypeScript, unit tests are run against the generated JavaScript code. In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug.
The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.
But since you are unit testing an instance of an Angular component, you won't be able to access the private method.
It looks like you are wanting to verify that handleError
gets called when build
runs.
Private methods are compiled to normal JavaScript prototype methods, so you can use the any
type to let the spy creation pass through the TypeScript type checking.
Here is a highly simplified example:
class OrderBuilder {
public build() {
this.handleError()
}
private handleError() {
throw new Error('missing ... field in order')
}
}
describe('Order Builder', () => {
it('should test the handleError', () => {
const handleErrorSpy = jest.spyOn(OrderBuilder.prototype as any, 'handleError');
const orderBuilder = new OrderBuilder()
expect(() => orderBuilder.build()).toThrow('missing ... field in order'); // Success!
expect(handleErrorSpy).toHaveBeenCalled(); // Success!
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With