【问题标题】:Testing Typescript classes within namespace using Jest使用 Jest 在命名空间中测试 Typescript 类
【发布时间】:2019-05-20 12:25:06
【问题描述】:

我在命名空间中有以下方法:

// main.ts
namespace testControl {

    export const isInternalLink = (link: string) => {
        return true;
    }
}

并遵循笑话规范:

// main.spec.ts

test('should return false given external link', () => {

 // How to use testControl.isInternalLink here ?

});

尝试添加

/// <reference path="./main.ts"/> 

尝试将测试包装在同一个命名空间中

【问题讨论】:

    标签: typescript jestjs


    【解决方案1】:

    尝试导出命名空间本身:

    // main.ts
    export namespace testControl {
    
        export const isInternalLink = (link: string) => {
            return true;
        }
    }
    

    然后在您的规范中导入命名空间:

    // main.spec.ts
    import { testControl } from './main'
    
    test('should return false given external link', () => {
    
      // How to use testControl.isInternalLink here ?
      expect(testControl.isExternalLink('')).toBe(false)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2020-04-26
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多