【问题标题】:Typescript Jasmine Spec compilation removes import statement - Visual StudioTypescript Jasmine Spec 编译删除导入语句 - Visual Studio
【发布时间】:2016-12-06 19:56:28
【问题描述】:

没有太多运气找到解决这个问题的方法,并且已经解决了几个小时。

所以我有几个文件夹,其中包含 Angular 2 组件/模块/服务。我正在为 Jasmine 测试规范添加一个新文件夹来测试生成的 JS 代码。

在 Visual Studio 中,我将 Jasmine 作为参考 (nu-get) 导入,并通过打字将其拉入,并使用 Chutzpah 扩展使 spec.js 文件显示在 Visual Studio 测试资源管理器中。这一切都很好,最基本的测试(true == true 和 true == false)都返回预期的输出。

当我尝试将 Angular 2 应用程序中的服务添加到规范中以对其进行测试时,就会出现此问题。这是代码...

import { TestingService } from "../Services/testing.service";

describe("TestingService", function () {
    let TestingService: TestingService;
    TestingService.Testing("TEST"); 

    it("testing is working", function () {
        expect(true).toBe(true);
    });
});

代码编译正确,从TS到JS的typescript编译没有报错。当我尝试运行测试时出现问题,生成的 JS 代码如下所示...

"use strict";

describe("TestingService", function () {
    var TestingService;
    TestingService.Testing("TEST");
    it("testing is working", function () {
        expect(true).toBe(true);
    });
});

正如您所见,从 TS 代码导入的测试服务已被编译器删除,并且测试用例错误表明 TestService.Testing 未定义。我真的不明白为什么这里删除了导入,因为我所有其他 angular 2 组件/服务/模型/等都是使用完全相同的 gulp 脚本编译的,并且导入会继续。

这是我尝试过的其他一些事情,但没有成功。

现在打字稿中似乎已弃用 -

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

不起作用,编译错误-

import foo = require("../Services/testing.service"); var bar = foo;

只是重申我的环境-

VS - Visual Studio

VS Chutzpah 搜索 .spec.js 测试用例

项目中的VS Jasmine参考

打字茉莉花

Gulp 将 TS 编译为 JS - 工作

错误 -

Gulp 将测试规范编译为 JS 并移除导入

感谢您的宝贵时间。

【问题讨论】:

    标签: visual-studio angular typescript jasmine chutzpah


    【解决方案1】:

    您有 chutzpah.json 文件设置吗?您是否看过 Chutzpah Angular2 示例。这可能有助于引导您并帮助您入门:https://github.com/mmanela/chutzpah/tree/master/Samples/Angular2/Basic

    【讨论】:

    • 感谢您的帮助,我确实设置了 chutzpah.json 文件。 Angular2 示例的问题是它现在已经过时了。我尝试将来自 Karma 的确切导入语句复制到 Chutzpah json 中,但没有成功。最后,我最终决定使用 Karma 并使用 Visual Studio Node.js 工具从交互式命令行运行测试。我可以看到为什么测试会因 Karma 失败,因为我没有愚蠢地创建一个新对象,而 testservice 给出了一个未定义的错误。
    猜你喜欢
    • 2013-02-22
    • 2016-09-12
    • 2013-02-22
    • 2016-11-11
    • 2015-09-06
    • 2012-09-25
    • 2017-08-28
    • 2014-04-09
    • 2020-06-06
    相关资源
    最近更新 更多