chai

How to unit test express Router routes

六月ゝ 毕业季﹏ 提交于 2019-12-02 16:41:47
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 router = express.Router(); router.get('/', controller.index); module.exports = router; app/api/controllers

How to unit test a method which connects to mongo, without actually connecting to mongo?

谁都会走 提交于 2019-12-02 14:45:21
I'm trying to write a test to test a method that connects to mongo, but I don't actually want to have to have mongo running and actually make a connection to it to have my tests pass successfully. Here's my current test which is successful when my mongo daemon is running. describe('with a valid mongo string parameter', function() { it('should return a rejected promise', function(done) { var con = mongoFactory.getConnection('mongodb://localhost:27017'); expect(con).to.be.fulfilled; done(); }); }); mongoFactory.getConnection code: getConnection: function getConnection(connectionString) { // do

Mocha Chai test case for angular configuration file

与世无争的帅哥 提交于 2019-12-02 10:59:41
I am getting a hard time to resolve mocha chai test case for angular js configuration file . angular.module('myApp.myModule', []).config(function ($stateProvider, $urlRouterProvider) { $stateProvider.state('myModule', { url: '/myModule', templateUrl: function(){ return 'scripts/my-module/views/my-module.html'; }, controller: 'myModuleCtrl', controllerAs: 'myCtrl', css: 'styles/lazy-load/my-module.css' }); $urlRouterProvider.otherwise('/'); }) I need to cover mocha chai test case for return function of "templateUrl" which is returning a url ('scripts/my-module/views/my-module.html'). What you

chai: Cannot read property 'not' of undefined

浪子不回头ぞ 提交于 2019-12-02 09:46:33
I am new to chai and mocha and I use the sample code for my first test case. Here is my code. var chai = require("chai"); var mocha = require("mocha"); var expect = chai.expect; describe("Test", function() { it("Test", function() { expect([1, 2]).to.be.an('array').that.does.not.include(3); }); }); I run mocha test.js The result is: TypeError: Cannot read property 'not' of undefined What is wrong with me? It seems .does return undefined. I remove .does and it works correctly. What is the correct usage? The following code works. expect([1, 2]).to.be.an('array').that.not.include(3); If I run your

Chai should is undefined

試著忘記壹切 提交于 2019-12-02 04:43:15
问题 I have installed Mocha as well as Chai. In my unit test: import {expect, should} from "chai"; describe("array", function () { it("has length of 1", function (done) { var arr = ["B"]; expect(arr).have.lengthOf(1); arr.should.have.lengthOf(1); }); }); expect works as expected, but should is undefined. Why? 回答1: You should call chai.should() at the begin of the file to use should style. It will extends each object with a should property to start your assertions chain. You can find more usage

Assert arrays using Chai (or Should) in Javascript ES6 (using Babel)

谁说胖子不能爱 提交于 2019-12-02 02:46:48
问题 I'm trying to test an array such as: let projects = [ { "id": "55a75be01fa2c7ff76a2ce7a", "title: "Only-Ben", "other_keys": "that can contain objects or arrays" }, { "id": "55a75be01fa2c7ff76a2ce7d", "title: "Only-Thomas" }, { "id": "55a75be01fa2c7ff76a2ce7c", "title: "Other-Project" } ]; The goal is to test that the array Contains an element which has a key {title: 'Only Ben'} Do not contain an element which has a key {title: 'Only Thomas'} I'm currently using chai.js and chai things with

Assert arrays using Chai (or Should) in Javascript ES6 (using Babel)

£可爱£侵袭症+ 提交于 2019-12-02 00:16:08
I'm trying to test an array such as: let projects = [ { "id": "55a75be01fa2c7ff76a2ce7a", "title: "Only-Ben", "other_keys": "that can contain objects or arrays" }, { "id": "55a75be01fa2c7ff76a2ce7d", "title: "Only-Thomas" }, { "id": "55a75be01fa2c7ff76a2ce7c", "title: "Other-Project" } ]; The goal is to test that the array Contains an element which has a key {title: 'Only Ben'} Do not contain an element which has a key {title: 'Only Thomas'} I'm currently using chai.js and chai things with this test: projects.should.include.something.that.deep.have.property('title', 'Only Thomas'); This is my

Mocha change timeout for afterEach

非 Y 不嫁゛ 提交于 2019-12-01 22:19:02
问题 I am writing a node application with mocha and chai. Some of the tests call an external API for integration tests, which might take up to 90sec to perform the action. In order to cleanup properly, I defined an afterEach() -block, which will delete any generated remote resources, in case an expect fails and some resources weren't deleted. The tests themselves have an increased timeout, while the rest of the tests should retain their default and small timeout: it('should create remote resource'

Mocha change timeout for afterEach

会有一股神秘感。 提交于 2019-12-01 22:14:40
I am writing a node application with mocha and chai. Some of the tests call an external API for integration tests, which might take up to 90sec to perform the action. In order to cleanup properly, I defined an afterEach() -block, which will delete any generated remote resources, in case an expect fails and some resources weren't deleted. The tests themselves have an increased timeout, while the rest of the tests should retain their default and small timeout: it('should create remote resource', () => {...}).timeout(120000) However, I can't do the same with afterEach().timeout(120000) , because

chai js expect property value empty array

China☆狼群 提交于 2019-12-01 17:09:36
I'm trying to write a unit test using chai js assertion, and was wondering how to expect arrays with zero length as values. My Test function expect statement: return expect(functionRetuningPromise()).to eventually.have.property("key1", []); Console Output on running mocha: AssertionError: expected { otherkey: otherVal, key1: [] } to have a property 'key1' of [], but got [] I have tried deep.property , key1:"[]" with no success I ignored there's a section of change in assertion for properties. So, what made it work for me was : return expect(functionRetuningPromise()).to.eventually.have