I just started learning TypeScript and tried some simple app with jest unit test (using ts-jest):
simple app.ts module:
function greet(person: string): string {
return `Hello, ${person}`;
}
exports.greet = greet;
simple app.spec.ts code:
const greetObject = require('../app');
greetObject.greet(1);
describe('greet function', () => {
it('should return greeting', () => {
expect(greetObject.greet('Dude')).toEqual('Hello, Dude');
});
it('should throw type exception', () => {
const spy = jest.spyOn(greetObject, 'greet');
greetObject.greet(1);
/** @todo what should happen ? */
});
});
and the question is: should I receive type error or not ? In fact, I do not receive any errors here.
But if I call greet with wrong parameter type in app.ts file the whole test suite is failing.
Am I missing something in TypeScript unit testing ?
UPD. Converted require to ES6 import. TypeScript diagnostics are now working, but I still don't know if I can do anything with wrong types and test those situations. Any advice is appreciated.
should I receive type error or not ?
Not here.
What you are testing is the JS output of your TS file.
TS errors are compilation time errors.
I still don't know if I can do anything with wrong types and test those situations.
There is no type checking at runtime with TypeScript, since the runtime is not TypeScript, but JavaScript.
If you want to validate some object received, you should implement actual checking functions.
maybe helpful :