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
31
votes
2 answers

Post request via Chai

I am trying to make a request to my node JS server which accepts post/put call. The parameters I am trying to send with post call via chai is not visible on server (req.body.myparam). I have tried with below post request but ended with not…
SCJP1.6 PWR
  • 371
  • 1
  • 4
  • 7
31
votes
2 answers

Testing JS exceptions with Mocha/Chai

Trying to test some code that throws an exception with Mocha/Chai, but having no luck, here's the simple code I'm trying to test: class window.VisualizationsManager test: -> throw(new Error 'Oh no') Here is my test: describe…
TheDelChop
  • 7,938
  • 4
  • 48
  • 70
30
votes
2 answers

How to provide chai expect with custom error message for mocha unit test?

I have a mocha test using chai's expect: it("should parse sails out of cache file", async () => { const sailExtractor = new Extractor(); const result = await sailExtractor.extract("test.xml"); try { …
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
30
votes
1 answer

How to test event emitters in node

Lets say I want to write this simple task. But I want to write a test validating that: This task emits object. Object has a property name. I'm testing with mocha and chai expect. Thanks in advance. I've tried every possible variant that came to…
datogio
  • 351
  • 1
  • 3
  • 10
29
votes
2 answers

How does the expect().to.be.true work in Chai?

expect(true).to.be.true; In this code, all the 'to', 'be', 'true' seems to be an attribute of the object response from 'expect(true)'. How can these attributes work so that they can raise an exception?
wangzi6147
  • 293
  • 1
  • 3
  • 5
29
votes
4 answers

How to test react-router with enzyme

I am using enzyme+mocha+chai to test my react-redux project. Enzyme provides shallow to test component behavior. But I didn't find a way to test the router. I am using react-router as below: ...
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
29
votes
1 answer

mocha, chai, Uncaught AssertionError: expected {} to equal {} + expected - actual

it('GET /customers/ with wrong id', (done) => { request .get(`/customers/${wrongId}`) .end((err, res) => { expect(res.body).to.equals({}); expect(res).to.have.status(404); done(); }); }); 1)…
Limarenko Denis
  • 769
  • 1
  • 8
  • 13
29
votes
3 answers

Chai.js compare arrays without considering order

What I'd like to do is compare 2 arrays of primitives using chai.js, but without considering the order of elements - like if they were 2 sets. Now obviously I can do something like this: const actual = ['a', 'b', 'c']; const expected = ['b', 'c',…
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
28
votes
1 answer

Add custom http headers to chai requests

I am building an app using node.js and testing with mocha + chai. Is there a way I can add custom headers to my GET and POST chai requests? For example, I want something like (semi-pseudocode): chai.request(server) .get('/api/car/' + data.car_id) …
Trung Tran
  • 13,141
  • 42
  • 113
  • 200
27
votes
4 answers

TypeError: expect(...).to.startsWith is not a function - chai and chakram

I started writing some automation tests(API) Now I tried to do to this endpoint: https://dog.ceo/api/breeds/image/random so I added into my function expect(response.body.message).to.startsWith('https://images.dog.ceo/breeds/'); and at the…
Justyna J
  • 303
  • 1
  • 3
  • 8
26
votes
1 answer

Asserting object equality in Javascript test (chai)

I need to assert equality between two points in my JavaScript unit tests: var pnt1 = {x: 2, y: 3}; and var pnt2 = {x: 2, y: 3}; When I do assert.equal(pnt1, pnt2); It says the points are different. Can I exclude from the check the fact that the…
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
25
votes
1 answer

Chai: assert array includes all items

Is it possible to assert that an array includes multiple specific items using chai? For example, I would expect this to work: ['foo', 'bar'].should.include(['foo', 'bar']) Instead chai throws: "expected [ 'foo', 'bar' ] to include [ 'foo', 'bar'…
mockaroodev
  • 2,031
  • 1
  • 20
  • 24
25
votes
2 answers

How do I test if a function calls a specific method/function?

Is there a way in Mocha to test if a function calls a specific method or external function? I am using Mocha with Chai, but am open to any other assertion libraries. Ok, so testing whether a methid is being called is pretty easy using sinon. I'm…
hal
  • 4,845
  • 7
  • 34
  • 57
25
votes
5 answers

Chai.js: Object contains/includes

Chai has an include method. I want to test to see if an object contains another object. For example: var origin = { name: "John", otherObj: { title: "Example" } } I want to use Chai to test if this object contains the following (which it…
user1082754
24
votes
1 answer

TypeError: Cannot read property 'equal' of undefined

I am trying to use enzyme to assert DOM nodes. My Component looks like import React, {Component} from 'react'; import TransactionListRow from './TransactionListRow'; import {Table, TableBody, TableHeader, TableHeaderColumn, TableRow} from…
daydreamer
  • 87,243
  • 191
  • 450
  • 722