【问题标题】:jasmine-ts ReferenceError: SVGGElement is not definedjasmine-ts ReferenceError:未定义 SVGGElement
【发布时间】:2018-02-19 23:33:12
【问题描述】:

我正在使用 jasmine-ts 编写测试,并且我有许多工作测试不使用表示 dom 元素的类型。

现在我写了一个测试,可以简化为:

describe("A Test",
    () => {
        it("when an element is created then it is defined",
            () => {
                const element = new SVGGElement();
                expect(element).toBeDefined();
            }
        );
    }
);

使用 jasmine-ts 运行测试时,测试失败并出现以下错误:

消息: ReferenceError:未定义 SVGGElement 堆栈: ReferenceError:未定义 SVGGElement 在对象。 (C:\Path\Test.spec.ts:50:37)

我看到 SVGGElement 在两者中都声明了

  • ...\TypeScript\2.4\lib.es6.d.ts,和
  • ...\TypeScript\2.4\lib.dom.d.ts

所有在使用 tsc 时编译,尽管没有明确导入此类型。这可能是因为 tsconfig.json 中的以下内容:

"compilerOptions": {
    "lib": [ "es6", "dom" ]
}

如何配置才能运行此测试?

【问题讨论】:

    标签: typescript jasmine


    【解决方案1】:

    ReferenceError: SVGGElement 未定义

    这是一个运行时错误。不是编译错误。您在不支持 SVGGElement 的环境中运行,例如nodejs

    更多

    设置在浏览器中运行。 Check the docs.

    【讨论】:

    • 感谢您的提示。文档的哪个方面与我的问题相关,并且与我使用 jasmine-ts 运行的测试相关?
    【解决方案2】:

    根据 basarat 提供的答案,我切换到 Jest,因为这是 jsdom 附带的。

    但是运行测试我得到了完全相同的错误。

    似乎不支持尝试以这种方式使用构造函数构造元素。相反,我使用了以下代码:

    const gElement = document.createElementNS("http://www.w3.org/2000/svg", "g") as SVGGElement;
    

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 1970-01-01
      • 2018-10-01
      • 2021-10-08
      • 1970-01-01
      • 2015-09-03
      • 2012-08-24
      • 2019-12-02
      • 2015-10-04
      相关资源
      最近更新 更多