DEV Community

loading...

Discussion on: Do you unit test private methods?

Collapse
jefftian profile image
Jeff • Edited

You can access private methods using the object["prop"] notation in TypeScript:

class TestObj {
private method() { return "done" }
}

const testObj = new TestObj()

expect(testObj["method"]()).toEqual("done")
Enter fullscreen mode Exit fullscreen mode
Collapse
juliethqp profile image
JuliethQP

Thanks for, helped solve it

Collapse
yashwanth2714 profile image
Yashwanth Kumar

Why it works?

Collapse
jefftian profile image
Jeff

It's JavaScript feature that you can access properties by using [property]. And in the end the TypeScript will be transpiled to pure javascript which has no public, private differences.

Thread Thread
yashwanth2714 profile image
Yashwanth Kumar

Agree! However, I didn't expect that TS allow this because of it's strict Type Checking.