Questions tagged [chai]

Chai is a BDD/TDD assertion library for Node.js and browsers that can be used with any Javascript testing framework.

Chai is a / assertion library for and that can be used with any testing framework.

2953 questions
24
votes
4 answers

How to make Istanbul generate coverage for all of my source code?

Currently Istanbul is only generating coverage for files that are used in my tests, which is okay, but seems to defeat the purpose of having coverage somewhat. I have no Istanbul configuration, and am invoking it via npm test with the following…
Seer
  • 5,226
  • 5
  • 33
  • 55
24
votes
4 answers

Chai: expecting an error or not depending on a parameter

I've been trying to do a text of a function that handles errors in a way that, if it is a valid error, it is thrown, but if it is not, then nothing is thrown. The problem is that i cant seem to set the parameter while…
Apzx
  • 367
  • 1
  • 3
  • 12
22
votes
7 answers

Chai expect: an array to contain an object with at least these properties and values

I'm trying to validate that an array of objects like this: [ { a: 1, b: 2, c: 3 }, { a: 4, b: 5, c: 6 }, ... ] contains at least one object with both { a: 1 } and { c: 3 }: I…
blub
  • 8,757
  • 4
  • 27
  • 38
22
votes
5 answers

Comparing arrays in chai

I'm writing some tests with chai and chai-as-promised (and more frameworks, but it doesn't matter in this case) and I need to check if array I get from a web-page is same as a predefined array. I tried to use…
Alissa
  • 686
  • 1
  • 8
  • 21
22
votes
1 answer

Sinon spy on console.log call not registered

I'm trying to learn about Sinon and want to spy on console.log. The code is simple: function logToConsole() { console.log('Hello World'); } exports.logToConsole = logToConsole; But if I want to test it, it doesn't work because the call to…
user2511874
21
votes
3 answers

Assert that element is not actionable in Cypress

If an element is not actionable on the page (in this case, covered by another element) and you try to click it, Cypress will show an error like this: CypressError: Timed out retrying: cy.click() failed because this element: ... is…
Laura
  • 3,233
  • 3
  • 28
  • 45
21
votes
3 answers

How to unit test file upload with Supertest -and- send a token?

How can I test a file upload with a token being sent? I'm getting back "0" instead of a confirmation of upload. This is a failed test: var chai = require('chai'); var expect = chai.expect; var config = require("../config"); // contains call to…
Christina Mitchell
  • 437
  • 2
  • 8
  • 17
20
votes
4 answers

How to make empty placeholder tests intentionally fail in Mocha?

I'm writing an API in NodeJS and testing using Mocha, Chai and SuperTest. I'm using a typical test-driven approach of writing the tests first then satisfying those tests with working code. However, because of the number of tests for all the…
Soviut
  • 88,194
  • 49
  • 192
  • 260
18
votes
3 answers

How to have mocha show entire object in diff on assertion error?

I have a unit test test case created with mocha and chai's expect where I deeply compare an array of value objects to the parsed content of a JSON file. My record object has about 20 properties, and currently only the price can cause a mismatch. On…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
18
votes
2 answers

Chai.js - Check if string contains substring from a list

I'm using chai.js writing some automation tests. I have a string: url(http://somewhere.com/images/myimage.png) I want to do something like: expect(thatSelectedItem).contains.any('jpg', 'png', 'gif') However can't seem to find anything in chai.js Any…
userMod2
  • 8,312
  • 13
  • 63
  • 115
18
votes
2 answers

simulate for onClick not working in enzyme

This is a cancel button
I need to simulate its click,I tried the following test wrapper.find('.cancelFileBtn').simulate('click'); But the click function is still…
Thomas John
  • 2,138
  • 2
  • 22
  • 38
18
votes
2 answers

Request body undefined in supertest

I am testing an express API with supertest. I am trying to pass in body parameters into the test, as can be seen in the code snippets below, but it appears that the body parameters don't get passed in correctly since I get an error message that the…
dpen82
  • 244
  • 1
  • 2
  • 13
18
votes
1 answer

How to check whether the element exist using chai?

Using Chai, how can I see whether the element For example, a div with the class .avatar exist? I tried to.exist but it's not working.
THpubs
  • 7,804
  • 16
  • 68
  • 143
18
votes
3 answers

Stub moment.js constructor with Sinon

I am not able to stub the moment constructor when calling it with the format function to return a pre-defined string, here's an example spec that I would like to run with mocha: it('should stub moment', sinon.test(function() { console.log('Real…
TPPZ
  • 4,447
  • 10
  • 61
  • 106
18
votes
1 answer

Can Webstorm play nice with Chai language chains?

I am using Mocha+Chai for a current Node.js project. My IDE is Webstorm 11. Unfortunately, it seems Webstorm has no way of figuring out how language chains in Chai's should/expect are supposed to work. The following is commonplace: I have tried…
csvan
  • 8,782
  • 12
  • 48
  • 91