首页 > 解决方案 > Export Function From Test file and use it in component file?

问题描述

I am new in Angular and NodeJS UTs. I have a function in test file and I have exported that function to use it in another file but once I call it from another file then I am receiving an error of describe block. Here is my code:

abc.controller.ts

export function mockGetAppByName(exists: boolean, error?: boolean) {
// some execution
}

mock.app.ts

    import { mockGetAppByName} from '../../test/api/abc/abc.controller.test';

    //Called function below in some other function
    mockGetAppByName(true);

When I run command npm start It throws below error. If I does not export it then it works fine...When I export and import this function in another file then also I am not receiving any error...BUT...once I call that function in my code then it throws an error. It does not allow my server to start and it keep throwing error.

ERROR

C:\Ambuj Khanna\Project\STA_Console\STAConsole\server\tmp\test\api\action\action.controller.test.js:194 describe('ActionController', () => { ^

ReferenceError: describe is not defined at Object. (C:\Ambuj Khanna\Project\project\server\tmp\test\api\action\abc.controller.test.js:194:1) ←[90m at Module._compile (internal/modules/cjs/loader.js:955:30)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:811:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:723:14)←[39m ←[90m at Module.require (internal/modules/cjs/loader.js:848:19)←[39m ←[90m at require (internal/modules/cjs/helpers.js:74:18)←[39m at Object. (C:\Ambuj Khanna\Project\project\server\tmp\src\mock\mock.application.js:10:34) ←[90m at Module._compile (internal/modules/cjs/loader.js:955:30)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:811:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:723:14)←[39m ←[90m at Module.require (internal/modules/cjs/loader.js:848:19)←[39m ←[90m at require (internal/modules/cjs/helpers.js:74:18)←[39m at Object. (C:\Ambuj
Khanna\Project\STA_Console\STAConsole\server\tmp\src\mock\index.js:5:28) ←[90m at Module._compile (internal/modules/cjs/loader.js:955:30)←[39m ←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)←[39m ←[90m at Module.load (internal/modules/cjs/loader.js:811:32)←[39m ←[90m at Function.Module._load (internal/modules/cjs/loader.js:723:14)←[39m ←[90m at Module.require (internal/modules/cjs/loader.js:848:19)←[39m ←[90m at require (internal/modules/cjs/helpers.js:74:18)←[39m at Object. (C:\Ambuj Khanna\Project\STA_Console\STAConsole\server\tmp\src\app.js:7:14)

标签: node.jsangulartestcase

解决方案


推荐阅读