Unit testing using Jasmine and TypeScript

后端 未结 7 2013
一生所求
一生所求 2021-01-31 01:26

I am trying to get a unit test written in Typescript using Jasmine to compile. With the following in my unit-test file, Resharper prompts me with a link to import types from jas

7条回答
  •  余生分开走
    2021-01-31 02:08

    You could try a side-effect only import which brings in the @types/jasmine declaration and places the jasmine functions into the global scope so you don't need to prefix each call with jasmine. allowing a quick port from existing unit tests and still plays nice with webpack.

    // tslint:disable-next-line:no-import-side-effect
    import "jasmine";
    
    describe("My Unit Test", () => { /* ... */ } );
    

    Of course you still need to install jasmine and the typings:

    $ npm i jasmine @types/jasmine --save-dev
    

    But no need for specialized jasmine loaders for ts or node. Just run jasmine against the compiled js files:

    $ node ./node_modules/jasmine/bin/jasmine.js --config=test/support/jasmine.json
    

    Assuming your typescript files are within a "test" subdirectory compiling to bin/test and you have a test/support/jasmine.json with something like this:

    {
        "spec_dir": "bin/test",
        "spec_files": [
          "**/*[sS]pec.js"
        ],
        "stopSpecOnExpectationFailure": false,
        "random": false
    }
    

    P.S. all of the above works on Windows too

提交回复
热议问题