Unit testing using Jasmine and TypeScript

后端 未结 7 2012
一生所求
一生所求 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:04

    Here's (in my opinion) the best way to test a ts-node app as of 2018:

    npm install --save-dev typescript jasmine @types/jasmine ts-node
    

    In package.json:

    {
      "scripts": {
        "test": "ts-node node_modules/jasmine/bin/jasmine"
      }
    }
    

    In your spec files:

    import "jasmine";
    import something from "../src/something";
    
    describe("something", () => {
        it("should work", () => {
            expect(something.works()).toBe(true);
        });
    });
    

    To run the tests:

    npm test
    

    This will use the locally installed versions of ts-node and jasmine. This is better than using globally installed versions, because with local versions, you can be sure that everyone is using the same version.

    Note: if you have a web app instead of a node app, you should probably run your tests using Karma instead of the Jasmine CLI.

提交回复
热议问题