ChatGPT解决这个技术问题 Extra ChatGPT

Unit testing using Jasmine and TypeScript

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 jasmine.d.ts.

/// <reference path="sut.ts" />
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />

describe("Person FullName", function () {
    var person;

    BeforeEach(function () {
        person = new Person();
        person.setFirstName("Joe");
        person.setLastName("Smith");
    });

    It("should concatenate first and last names", function () {
        Expect(person.getFullName()).toBe("Joe, Smith");
    });
});

So I click on the link and end up with the following (actually resharper only prefixed the describe function with "Jasmine.", so I manually prefixed the other Jasmine calls):

/// <reference path="sut.ts" />
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />
import Jasmine = require("../../../Scripts/typings/jasmine/jasmine");

Jasmine.describe("Person FullName", function () {
    var person;

    Jasmine.BeforeEach(function () {
        person = new Person();
        person.setFirstName("Joe");
        person.setLastName("Smith");
    });

    Jasmine.It("should concatenate first and last names", function () {
        Jasmine.Expect(person.getFullName()).toBe("Joe, Smith");
    });
});

However the import statement has a red squiggly line with error message "Unable to resolve external module ../../../scripts/typings/jasmine/jasmine. Module cannot be aliased to a non-module type"

Any idea what is causing this error? I've checked that the "Module System" option is set to AMD in my project build settings. I've also checked that the jasmine module is defined in jasmine.d.ts. I downloaded this file from DefinitelyTyped site.

declare module jasmine {
    ...
}
es6: import Jasmine from 'path/here';. es5: var Jasmine = require('path/here');. Use beforeEach, it and expect instead of BeforeEach, It and Expect.
Thanks for your response. I thought in Typescript the following is valid? import Jasmine = require("..."). Though perhaps not as it's giving so many compilation errors...

C
Carl Walsh

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 jasmine.json change file pattern to *.ts

"spec_files": ["**/*[sS]pec.ts"],

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.


When I try this I get the same problem as when I follow any other tutorial on how to do this: "ReferenceError: define is not defined". I think it's either because I'm using webpack or the fact that it's a vss sdk web extension.
This method is meant for plain ts-node tests (without webpack). If you want webpack, you should probably use karma.
In case you need to provide arguments to jasmine: "test": "ts-node --project ./tsconfig-server.json node_modules/jasmine/bin/jasmine --config=jasmine.json",
For Jasmine newbies, like me, you'll need to change jasmine.json to pick up *.ts files rather than *.js files.
When you use this example and install eslint, none of this code will compile. The Jasmine types won't be available.
J
Jan Van Bruggen

Put this at the top of your typescript spec file:

/// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
let Jasmine = require('jasmine');

You must install the following Jasmine modules for that to work:

$ npm install jasmine-core jasmine @types/jasmine @ert78gb/jasmine-ts --save-dev

Once you do that, the IDE (such as WebStorm) will recognize Jasmine and its functions such as describe(), it(), and expect().. So you don't need to prefix them with "Jasmine." Also, you can run your spec files from the command line using the jasmine-ts module. Install these command line tools globally:

$ npm install -g jasmine @ert78gb/jasmine-ts

Then configure the "jasmine" command line module so that Jasmine can find its configuration file. Then you should be able to run jasmine-ts and your spec file should run fine from the command line:

./node_modules/.bin/jasmine-ts src/something.spec.ts

.. and, you can configure your IDE to run it like that as well, and debug runs that way should also work (works for me).

Writing your tests this way, you can run a Jasmine test spec on the server side without Karma, or run it in a web browser using Karma. Same typescript code.


The problem with jasmine-ts is that it is based on Jasmine 2, so you cannot use the latest and greatest Jasmine 3 features. I hope they get it sorted. There is an open ticket for it on GitHub: github.com/svi3c/jasmine-ts/issues/27
@BennyNeugebauer "they" = us, since it's the open source community. So anyone who needs it can work on improving it, and I encourage you, because yes that would certainly be a helpful improvement!
The original jasmine-ts (npmjs.com/package/jasmine-ts) is not actively maintained, but ert78gb's fork (npmjs.com/package/@ert78gb/jasmine-ts) is. The original jasmine-ts has security vulnerabilities in its dependencies, but ert78gb's fork does not. I recommend installing jasmine-ts like this: npm install -save-dev @ert78gb/jasmine-ts
The original jasmine-ts (npmjs.com/package/jasmine-ts) is now actively maintained, thanks to ert78gb! I now recommend installing jasmine-ts the original way: npm install -save-dev jasmine-ts I currently cannot edit this post to revert my changes, because "Suggested edit queue is full"... :(
S
Sergey P. aka azure

If you have issues with imports, use tsconfig-paths

npm i ts-node tsconfig-paths types/jasmine jasmine --save-dev

Run typescript-enabled jasmine:

ts-node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine.js

Ensure that your jasmine will search .ts files:

"spec_files": [
    "**/*[sS]pec.ts"
],
"helpers": [
    "helpers/**/*.ts"
],

To test your scripts you may also need polyfills if you use them in your project. Create a helper file with required imports, like helpers/global/polifill.ts

import 'core-js';

A
Andrew Newland

For me I did the following:

Install Typings

npm install typings --global

Then add the typings in for jasmine

typings install dt~jasmine --save --global

You really shouldn't be using typings anymore. Much better to use the type definitions in the @types/* repos in NPM.
G
Graeme Wicksted

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


m
marcel

Include this to your jasmine html file,...

<script type="text/javascript" src="jasmine/lib/jasmine-2.0.0/jasmine.js"></script>

...or install the npm jasmine package:

npm install --save-dev jasmine

when you are using the second way (jasmine as module) you have to import it:

var jasmine = require('jasmine');

or

import jasmine from 'jasmine';

then change the other code:

jasmine.describe("Person FullName", function () {
    var person;

    jasmine.beforeEach(function () {
        person = new Person();
        person.setFirstName("Joe");
        person.setLastName("Smith");
    });

    jasmine.it("should concatenate first and last names", function () {
        jasmine.expect(person.getFullName()).toBe("Joe, Smith");
    });
});

Personally i would prefer the first way without using the jasmine npm module. (I didn't test the module yet)


I'm getting "Property 'describe' does not exist on type 'typeof jasmine'." Any ideas as to what is going wrong?
C
Coderer

You didn't ask for this, but for bonus points: once you get AJ's answer up and running (using ts-node to invoke the Jasmine startup script), you can add a new task:

"scripts": {
  "watch": "ts-node-dev --respawn -- ./node_modules/jasmine/bin/jasmine src/**.spec.ts"
}

Of course, you can pass your specs or any other arguments using Jasmine's config file instead, if you like. Now, Jasmine will run all your specs once, then ts-node-dev will sit in the background waiting for your tests or anything they might have require'd to change, at which point jasmine will be run again. I haven't worked out a way to only run the tests that have changed (or tests whose imports have changed) yet -- as far as I can tell, that's not supported anyway;


V
Vladimír Mlázovský

My folder structure

Spec folder is on the root of project

    spec
      \- dist              // compiled tests
      \- helpers           // files modified testing env
         \- ts-console.ts  // pretty prints of results
      \- support
         \- jasmine.json
      \- YourTestHere.spec.ts
      \- tsconfig.json     // tsconfig for your tests

Files content

ts-console.ts

const TSConsoleReporter = require("jasmine-console-reporter");
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new TSConsoleReporter());

jasmine.json

{
  "spec_dir": "spec/dist",
  "spec_files": [
     "**/*[sS]pec.js"
  ],
  "helpers": [
    "spec/helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

With extra script in package.json

"scripts": {
  "test": "rm -rf ./spec/dist && tsc -p ./spec && jasmine"
}

and add line "/spec/dist" to .gitignore

Run your tests!

Run your tests with npm test.

How does it work?

Directory for tests is cleaned. Tests are compiled to spec/dist folder to JS. Tests are runned from this location.

I hope it will help you. Good coding.


As of 2022, jasmine-console-reporter has not been updated in about 6 years. It won't work if your nodejs project is using the newer import syntax (ES6+ modules).
Thanks, do you have any alternative?
I ended up using ts-node to run the TypeScript files directly as described in the other answer.