Typescript throw error Property 'to' does not exist on type 'Matchers' when compiling mocha/chai test file
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/
swang
Tech cofounder in training. linkedin profile: http://www.linkedin.com/in/wangshan
Updated on June 09, 2022Comments
-
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 over 7 yearsThis 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 over 7 yearsYou might be including the
d.ts
twice somewhere (it's also auto-discovered bytsc
) I'm using just@types/mocha
and it runs fine without any warnings (also make sure to removetypings
directory) -
swang over 7 yearsI 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 over 7 yearsbtw, check what you have in
node_modules/@types
and also if there're any jasmine leftovers innode_modules
-
swang over 7 yearsyes 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 over 7 yearsThat's strange, is your
npm
version >3.0
? -
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 over 7 yearsyes, 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