admin管理员组

文章数量:1336434

I'm trying to port my javascript unit tests to typescript, however, a simply test with build in matcher fails:

describe('test getBranches', function() {
        it('returns an array of branches', function() {
            branchService.getBranches(owner, name)
            .then(function(res) {
                expect(res).to.exist;//<-- this is where the error is from
            })
        })
    })

The error says:

error TS2339: Property 'to' does not exist on type 'Matchers'.

I'm new to typescript but I guess I'm missing some type files for mocha or chai? I have since installed typings and did the following:

typings install dt~mocha --save --global
typings install dt~chai --save --global
typings install dt~chai-as-promised --save --global

But it doesn't make a difference at all.

Note that the generated js file is fine, the test passed. I just want to know why typescript is giving this error and how can I make it disappear.

I'm trying to port my javascript unit tests to typescript, however, a simply test with build in matcher fails:

describe('test getBranches', function() {
        it('returns an array of branches', function() {
            branchService.getBranches(owner, name)
            .then(function(res) {
                expect(res).to.exist;//<-- this is where the error is from
            })
        })
    })

The error says:

error TS2339: Property 'to' does not exist on type 'Matchers'.

I'm new to typescript but I guess I'm missing some type files for mocha or chai? I have since installed typings and did the following:

typings install dt~mocha --save --global
typings install dt~chai --save --global
typings install dt~chai-as-promised --save --global

But it doesn't make a difference at all.

Note that the generated js file is fine, the test passed. I just want to know why typescript is giving this error and how can I make it disappear.

Share Improve this question edited Nov 3, 2016 at 16:49 swang asked Nov 3, 2016 at 16:29 swangswang 5,2595 gold badges36 silver badges58 bronze badges 5
  • Where does Matchers e from? I doesn't look like it a part of chai. Also how do you import expect? – martin Commented Nov 3, 2016 at 16:47
  • @Martin, the error is from line expect(res).to, so I assume expect returns a Matcher type? – swang Commented Nov 3, 2016 at 16:49
  • 1 Yes, I'm asking where does Matchers class e from because it doesn't exist in neither mocha github./mochajs/mocha/search?utf8=%E2%9C%93&q=Matchers nor chai github./chaijs/chai/search?utf8=%E2%9C%93&q=Matchers so it looks like you have something wrong with your imports. – martin Commented Nov 3, 2016 at 16:56
  • 1 @Martin, you are right, I found Matchers from jasmine-expect, does it mean I'm using jasmine rather than chai? How do i make typescript pick chai's expect? I'm not importing anything in my ts files – swang Commented Nov 3, 2016 at 17:09
  • Have a look in your typings.d.ts maybe you left something there. – martin Commented Nov 3, 2016 at 17:12
Add a ment  | 

1 Answer 1

Reset to default 3

Ideally install both mocha and chai via @types scope as npm package:

  • npm install --save @types/mocha
  • npm install --save @types/chai
  • npm install --save @types/chai-as-promised

This way you can abandon typings tool pletely even with including all /// <reference path=.... directives.

For more info see: https://blogs.msdn.microsoft./typescript/2016/06/15/the-future-of-declaration-files/

本文标签: