简体   繁体   中英

How to use chakramjs for a typescript project?

I am writing a node application in typescript, and am pretty inexperienced with both nodejs and typescript.

I want to use chakram to test the API endpoints, yet chakram lacks typescript definiton .

The more general question is how to import a library without definitions , yet the way I am supposed to to apply the answers still eludes me. The provided answer are too abstract for my current understanding, so I would like a more concrete example.

Basically, I don't know how to transform the working javascript healthcheck.js :

var chakram = require('chakram'),
    expect = chakram.expect;

describe("Rest API Healthceck", function () {
    it('should respond with HTTP STATUS OK NO CONTENT', function () {
        var response = chakram.get("http://app.local/api/status", {});
        expect(response).to.have.status(204);

        return chakram.wait();
    });
});

into its typescript variant.

I tried to work with any , as I don't want to provide my own typings yet , I just want it to work.

I tried im my healthcheck.ts file with:

let it: any;
let describe: any;
let chakram: any;
chakram = require('chakram');
const expect = chakram.expect;

describe("Rest API Healthceck", function () {
    it('should respond with HTTP STATUS OK NO CONTENT', function () {
        var response = chakram.get("http://app.local/api/status", {});
        expect(response).to.have.status(204);

        return chakram.wait();
    });
});

It does compile yet it throws an error if I try to execute the test with mocha by ./node_modules/mocha/bin/mocha dist/tests/acceptance/healthcheck.js , namely:

TypeError: describe is not a function

Investigating the error further I am also not sure if the issue has to do with mocha and how its types are loaded . I am also using typings and not definilty typed, that may also be another problem.

Where am I going wrong?

To make the test run I had to make typings aware of mocha:

./node_modules/typings/dist/bin.js install env~mocha --global

The problem was due to missing defintion of mocha and unrelated to chakramjs.

In order for it to run then, my typescript testcase looks like:

let chakram: any;
chakram = require('chakram');
const expect = chakram.expect;

describe("Rest API Healthceck", function () {
    it('should respond with HTTP STATUS OK NO CONTENT', function () {
        var response = chakram.get("http://app.local/api/status", {});
        expect(response).to.have.status(204);

        return chakram.wait();
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM