How to test export function with Karma-Jasmine in Angular4

Viewed 2866

It is my first question here, I hope to do well...

I am a begginer doing unit tests with Karma-Jasmine in Angular and I just found a case that I do not know how to resolve it.

I have a .ts file that looks like this:

example.constants.ts

This file have a function that generate a random ID. Here is my code:

export function generateUid(separator: string) {
...
}

I am trying to do a test of this function because i need cover it. So i decided to create a file example.constants.spec.ts. It looks like this

import { TestBed } from '@angular/core/testing';
import { generateUid } from './example.constants';

describe('ExampleConstants generateUid', () => {

beforeEach(() => {
    TestBed.configureTestingModule({
        providers: []
    });
});

it('should check if Uid is generated',
    () => {
        expect(0).toBe(0);
});

});

The problem is not about how to cover if the function work well. The problem is about this test does not appear when I run ng test --code-coverage. I have been working with components and services unit tests but it is the first time that i want to do a test about an export function. This function not have associated component. It is declare in example.constants.ts like an export function.

Could you help me to do this unit test about an export function?

Regards.

1 Answers

You don't need to configure testing modules for this scenario. You can test it just like normal functions

import { TestBed } from '@angular/core/testing';
import { generateUid } from './example.constants';

describe('ExampleConstants generateUid', () => {

it('should check if Uid is generated',
    () => {
     generateUid('-').toBeDefined();
});

});
Related