2

I'm working on setting up unit tests for my express app. When I run my test it fails with this error ``

import * as timestamp from './timestamp'
import chai, { expect } from 'chai'
import sinonChai from 'sinon-chai'
import { mockReq, mockRes } from 'sinon-express-mock'
//chai.use(sinonChai); <-- `I removed this because it was creating this error:: TypeError: Cannot read property 'use' of undefined`


describe('hello world', () => {

    it('should behave...', () => {
        const request = {
            body: {
              foo: 'bar',
            },
          }
          const req = mockReq(request)
          const res = mockRes()

          timestamp.timestamp(req, res)


          expect(res.json).to.have.been.calledWith({})

    });
});
Justin Young
  • 2,393
  • 3
  • 36
  • 62
  • 1
    `calledWith` does not exist in chai, so you need the `chai.use(sinonChai);` to add it. Your problem isn't the Invalid Chai property error, so much as the TypeError you're getting in that line. You might want to rephrase this question accordingly. – sripberger Feb 09 '19 at 20:37
  • As for why that line isn't working, it will require more knowledge of the surrounding environment. I see the TypeScript tag, so I assume you're using TypeScript to support the ES6 module syntax. Most of the time, when module references come back undefined, it's usually the result of a circular dependency. Though of course, I can't say for sure without seeing what's in your ./timestamp module and its own dependencies. – sripberger Feb 09 '19 at 20:41
  • Also, it might help to see what the TS is actually transpiling to before you run it. – sripberger Feb 09 '19 at 20:42

2 Answers2

0

I think it's your module system or bundler (webpack?) messing it up. This works fine in my example: https://runkit.com/fatso83/chai-spy-example

I saw this myself with WebKit the other day, and I fixed it by splitting the import over two lines. Try replacing

import chai, { expect } from 'chai';

with

import chai, from 'chai';
import { expect } from 'chai';
oligofren
  • 20,744
  • 16
  • 93
  • 180
-1

I also encountered the same issue and the following worked for me

expect(spyedMethod.calledWith('param1, param2')).to.equal(true);
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30968572) – kmp Feb 07 '22 at 23:57