chai

How to unit test express Router routes

雨燕双飞 提交于 2019-12-20 08:40:37
问题 I'm new to Node and Express and I'm trying to unit test my routes/controllers. I've separated my routes from my controllers. How do I go about testing my routes? config/express.js var app = express(); // middleware, etc var router = require('../app/router')(app); app/router/index.js module.exports = function(app) { app.use('/api/books', require('./routes/books')); }; app/router/routes/books.js var controller = require('../../api/controllers/books'); var express = require('express'); var

What is the difference between assert, expect and should in Chai?

…衆ロ難τιáo~ 提交于 2019-12-20 08:01:55
问题 What is the difference between assert , expect and should , and when to use what? assert.equal(3, '3', '== coerces values to strings'); var foo = 'bar'; expect(foo).to.equal('bar'); foo.should.equal('bar'); 回答1: The differences are documented there. The three interfaces present different styles of performing assertions. Ultimately, they perform the same task. Some users prefer one style over the other. This being said, there are also a couple technical considerations worth highlighting: The

Babel / Karma / Chai gives TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions

寵の児 提交于 2019-12-19 16:37:27
问题 I having trouble figuring out why this test is not passing. var expect = require('chai').expect; describe('HelloComponent', function() { it('passes a quite simple test', function() { expect(1 + 4).to.equal(5); }); }); produces this error: DEBUG [web-server]: serving: /Users/ivan/dev/react-starter/node_modules/karma/static/context.html DEBUG [web-server]: serving (cached): /Users/ivan/dev/react-starter/node_modules/mocha/mocha.js DEBUG [web-server]: serving (cached): /Users/ivan/dev/react

Match partial objects in Chai assertions?

99封情书 提交于 2019-12-19 05:07:02
问题 I am looking for the best way to match the following: expect([ { C1: 'xxx', C0: 'this causes it not to match.' } ]).to.deep.include.members([ { C1: 'xxx' } ]); The above doesn't work because C0 exists in the actual, but not the expected. In short, I want this expect to PASS, but I'm not sure how to do it without writing a bunch of custom code... 回答1: chai-subset or chai-fuzzy might also perform what you're looking for. Chai-subset should work like this: expect([ { C1: 'xxx', C0: 'this causes

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

筅森魡賤 提交于 2019-12-18 19:21:28
问题 This question already has answers here : chai test array equality doesn't work as expected (3 answers) Closed 3 years ago . 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) Customers CRUD GET /customers/ with wrong id: Uncaught AssertionError: expected {} to equal {} + expected - actual 回答1: You want to use deep if you're trying to compare objects:

Chai response.body is always empty {}

Deadly 提交于 2019-12-18 07:39:10
问题 No matter what my server actually returns, Chai always gives me this exception when I assert response.body: Uncaught AssertionError: expected {} to deeply equal 'test' Even though the actual server response is 'test', not {}: Here is my test: const chai = require('chai'); const chaiHttp = require('chai-http'); const server = require('./test-server'); const should = chai.should(); chai.use(chaiHttp); describe('GET /test', () => { it('it should give test result', (done) => { chai.request(server

Do I really need to return a promise in test when using Chai as Promised?

冷暖自知 提交于 2019-12-18 07:06:56
问题 Chai as Promised documentation states as follows: Notice : either return or notify(done) must be used with promise assertions. And the examples on the site are as follows: return doSomethingAsync().should.eventually.equal("foo"); doSomethingAsync().should.eventually.equal("foo").notify(done); The thing is; I actually wrote a test using chai as promised without returning the promise. Like so: it('should resolve user', function () { $state.get(state).resolve.user(dataservice, { userId: testUser

chai test array equality doesn't work as expected

寵の児 提交于 2019-12-17 03:34:12
问题 Why does the following fail? expect([0,0]).to.equal([0,0]); and what is the right way to test that? 回答1: For expect , .equal will compare objects rather than their data, and in your case it is two different arrays. Use .eql in order to deeply compare values. Check out this link. Or you could use .deep.equal in order to simulate same as .eql . Or in your case you might want to check .members . For asserts you can use .deepEqual , link. 回答2: Try to use deep Equal. It will compare nested arrays

How do I properly test promises with mocha and chai?

删除回忆录丶 提交于 2019-12-17 00:47:25
问题 The following test is behaving oddly: it('Should return the exchange rates for btc_ltc', function(done) { var pair = 'btc_ltc'; shapeshift.getRate(pair) .then(function(data){ expect(data.pair).to.equal(pair); expect(data.rate).to.have.length(400); done(); }) .catch(function(err){ //this should really be `.catch` for a failed request, but //instead it looks like chai is picking this up when a test fails done(err); }) }); How should I properly handle a rejected promise (and test it)? How should

Unit test with mocha + chai always passed

倖福魔咒の 提交于 2019-12-14 03:02:45
问题 I try to use 'mocha' and 'chai' for my unit test but I have a problem with the test result, it always passed. Please take a look. UnitTest.spec.ts import PostgresService from "../src/Services/PostgresService" import {expect} from "chai" import 'mocha' describe('Postgres Override Test function', () => { it('should return any number but not zero', async () => { let client = new PostgresService(); let result = await client.getLatestCMD("Servo"); try{ console.log("Type : " + typeof(result));