export interface User {
name: string;
}
How can I unit test the above interface, so Karma could show it in the code coverage report?
I already tried creating the object and assert some properties, but didn't work. The test passes but karma doesn't consider it in the code coverage report.
import { User } from "./user";
describe('User', () => {
it('test', () => {
const obj: User = {
name: "xxx",
}
expect(obj.name).toEqual("xxx");
});
});
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.
To test an interface with common tests regardless of implementation, you can use an abstract test case, and then create concrete instances of the test case for each implementation of the interface.
Another argument in favor of writing your unit tests in TypeScript is that unit tests serve as documentation. If you have unit tests code that exercises production code with the same type system, developers are more likely to understand the mechanisms of it all when, for example, attempting a refactor.
We would follow the conventions: Place Source JS/TS files in src folder and tests typescript files in tests folder.
You can't. There is no code to cover here: nothing is executable.
And interfaces only exist at compile-time. They don't exist at runtime.
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