Typescript throw error Property 'to' does not exist on type 'Matchers' when compiling mocha/chai test file

16,527

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 completely even with including all /// <reference path=.... directives.

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

Share:
16,527
swang
Author by

swang

Tech cofounder in training. linkedin profile: http://www.linkedin.com/in/wangshan

Updated on June 09, 2022

Comments

  • swang
    swang almost 2 years

    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.

  • swang
    swang over 7 years
    This resolved the problem, except that @types/mocha will cause a bunch of errors like TS2300 Duplicate identifier 'describe', leave out @types/mocha will work. Also I need to do import {expect} from 'chai' in my spec.ts file
  • martin
    martin over 7 years
    You might be including the d.ts twice somewhere (it's also auto-discovered by tsc) I'm using just @types/mocha and it runs fine without any warnings (also make sure to remove typings directory)
  • swang
    swang over 7 years
    I think it's because I have jasmine installed, it must have come with protractor, but I didn't know why it installed types definition, as I'm not using typescript for e2e test. I have completely uninstalled typings and its directory, but still can't use types@mocha
  • martin
    martin over 7 years
    btw, check what you have in node_modules/@types and also if there're any jasmine leftovers in node_modules
  • swang
    swang over 7 years
    yes there is, if I remove it and install @types/mocha, it will come back again because of the protractor dependency, ├── @types/[email protected] └─┬ [email protected] └── @types/[email protected]
  • martin
    martin over 7 years
    That's strange, is your npm version > 3.0?
  • swang
    swang over 7 years
    @types/jasmine is brought in by protractor after 4.0.4, probably for angular2, after remove jasmine, mocha works fine
  • swang
    swang over 7 years
    yes, it's 3.10.8, there's one thing I don't understand, why I have to import {expect} from 'chai', but not other things like 'describe', 'it' from mocha