Reputation: 1
I am currently working on an Angular4 Web application and I am trying to write a unit test for a class and pipes that have only public static methods. By now I have not found any solution to do that. Anything that works for components, service etc. does not work here.
Example: Class
export class StringExtensions {
public static firstToLowerCase = ( argument: string ): string => {
return `${ argument.substr( 0, 1 ).toLowerCase() }${ argument.substr( 1 ) }`;
}
}
Example: not working Unittest
import {StringExtensions} from './string-extensions';
describe('StringExtensions', () => {
let classStringExtensions: StringExtensions;
const argument: string = 'ArgumenTas';
beforeEach(() => {
classStringExtensions = new StringExtensions();
});
afterEach(() => {
classStringExtensions = null;
});
it('should ...', () => {
expect(classStringExtensions.firstToLowerCase(argument)).toBe('argumenTas');
});
});
ErrorMessage:
ERROR in .../string-extensions.spec.ts (17,38):
Property 'firstToLowerCase' does not exist on type 'StringExtensions'.
Upvotes: 0
Views: 674
Reputation: 62298
It is because you calling the static method on an instance, you need to call it on the type as static members are accessed at the type level.
expect(StringExtensions.firstToLowerCase(argument)).toBe('argumenTas');
The current code inside of the beforeEach
and afterEach
are also not necessary.
This question is general to typescript and not specific to any framework.
Upvotes: 1