0
votes

I am writing tests using jasmine-ts, and I have a number of working tests that do not use types representing dom elements.

Now I have written a test that can be simplified to the following:

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

When running the test using jasmine-ts, the test fails with the following error:

Message: ReferenceError: SVGGElement is not defined Stack: ReferenceError: SVGGElement is not defined at Object. (C:\Path\Test.spec.ts:50:37)

I see that SVGGElement is declared in both

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

All compiles when using tsc, though there is no explicit import for this type. Presumably this works because of the following in tsconfig.json:

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

How do I configure things so that it is possible to run this test?

2

2 Answers

0
votes

ReferenceError: SVGGElement is not defined

This is a runtime error. Not a compile error. You are running in an enviroment that doesn't support SVGGElement e.g. nodejs

More

Setup to run in browser. Check the docs.

0
votes

Armed with the answer provided by basarat, I switched to Jest as this ships with jsdom.

However running the test I got exactly the same error.

It appears that attempting to construct an element using a constructor in this way is not supported. Instead I used the following code:

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