简体   繁体   English

如何使用chakramjs作为打字稿项目?

[英]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. 我正在使用typescript编写节点应用程序,并且对nodejs和typescript都很缺乏经验。

I want to use chakram to test the API endpoints, yet chakram lacks typescript definiton . 我想使用chakram来测试API端点,但chakram缺乏打字稿定义

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 : 基本上,我不知道如何转换工作的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. 我试图与工作any ,因为我不想提供我自己的分型 ,我只是希望它的工作。

I tried im my healthcheck.ts file with: 我试过我的healthcheck.ts文件:

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: 如果我尝试用./node_modules/mocha/bin/mocha dist/tests/acceptance/healthcheck.js mocha执行测试,它会编译但是会引发错误,即:

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 . 进一步调查错误我也不确定该问题是否与mocha及其类型的加载方式有关 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: 为了进行测试,我必须让打字知道mocha:

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

The problem was due to missing defintion of mocha and unrelated to chakramjs. 问题是由于缺少摩卡定义而与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();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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