简体   繁体   中英

How to test export function with Karma-Jasmine in Angular4

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.

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();
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM