mocha

Gitlab CI job succeeds before mocha tests are run

天大地大妈咪最大 提交于 2019-12-24 08:18:15
问题 I found that my Gitlab jobs are succeeding and ending before the actual test is run. First, I installing the required dependencies for my app, then I call the mocha commands, but the job succeeds before there is any output from them. My .gitlab-ci.yml: image: node:lts-alpine stages: - test test: stage: test services: - mongo:latest script: - cd server/ - apk add --update git - apk --no-cache add g++ gcc libgcc libstdc++ linux-headers make python - npm install --quiet node-gyp -g - npm install

Comparing the function output type in assertion

拈花ヽ惹草 提交于 2019-12-24 05:09:08
问题 I'm struggling with writing a test assertion using chai, mocha, and JS-DOM. I have a simple function like: function HtmlElement(el) { this.element = (el instanceof HTMLElement) ? el :document.createElement(el); } in the tests I have: it('should have addClass method', function () { const ul = (new HtmlElement('ul')); ul.element.should.be.equals('<ul></ul>'); // Outputs the actual UL }); but the error: AssertionError: expected <ul></ul> to equal '<ul></ul>' is the one I can't understand - what

Spying on Date constructor with sinon

狂风中的少年 提交于 2019-12-24 04:29:29
问题 I have a method that sets expiration date of a token: var jwt = require('jwt-simple'); module.exports = { setExpirationDate: function(numDays) { var dateObj = new Date(); console.log(dateObj); } } I want to write an assert on "new Date" statement: var jwtHelper = require('../../../helpers/jwtToken'); describe('setExpirationDate method', function() { it('should create date object', function() { var Date = sinon.spy(Date); jwtHelper.setExpirationDate(global.TOKEN_EXPIRE_DAYS); expect(Date).to

How to stub an object that still has to be loaded from the database in RSpec?

柔情痞子 提交于 2019-12-24 03:50:31
问题 I would like to check whether an object's association receives a certain method call. Problem is that I can stub the object's association, but when later on, the association is loaded from the database, the loaded object is not the same "fysical" object as my stubbed object. It has the same ID, but it is not the same "fysical" object, so the stub doesn't work anymore. What I currently have is (and this works!) it "should register the payment of the invoice if everything ok" do invoice =

Suppress console log of successful mocha tests

流过昼夜 提交于 2019-12-24 03:33:20
问题 When running mocha test suite, my console output is polluted with application logs. Is there an easy way to suppress these logs from successful tests? I'm able to suppress all logs for testing environment, however I'd like to see logs from failed tests. I'm using Winston as logging library. 回答1: You could set up a separate Winston transport when running tests that will write logs to a logfile, and show the contents of that file if the test failed: afterEach(function() { if (this.currentTest

ReferenceError: can't find variable x when using grunt test in yeoman

狂风中的少年 提交于 2019-12-24 01:56:07
问题 I'm using yeoman, grunt, mocha. I want to do BDD so I make basic unit test and run grunt test in console, which gives me ReferenceError: can't find variable: Creature opening test/index.html in browser works as it should. This is my creature.js file in app/scripts: 'use strict'; var Creature = (function () { function Creature(name) { this.name = name; } Creature.prototype.sayHello = function (message) { return this.name + ' ' + message; }; Creature.prototype.eat = function (item){ return this

How to skip to next next describe on error in Mocha?

微笑、不失礼 提交于 2019-12-24 00:59:40
问题 I have a bunch of describe s that test different parts of an API. In one section, all the tests are dependent on one test succeeding. I want to make Mocha run the first test, and if it fails, skip all following tests and run the next test suite for the next section of the API. mocha --bail would stop testing altogether after the first fail, and won't continue to the next section. mocha-steps is a viable solution, but I prefer not to use any external libraries. In addition, it doesn't skip

How to manually open socket and use it with http.request

落爺英雄遲暮 提交于 2019-12-23 22:40:05
问题 Im writing some integration tests for a node project. We need to get reliable metrics about server performance. For that, we want to open a socket between the server and the client before any test is performance, this way, we can eliminate the time needed to create a socket between client and server. I have created a small test case like this: 'use strict'; var express = require( 'express' ), net = require('net'), http = require('http'); var host = 'localhost', port = 8000; describe( 'dummy

unit testing express route with async callback

被刻印的时光 ゝ 提交于 2019-12-23 20:17:03
问题 I'm writing an app in node.js using express. I seperated the route definition from express so I will be able to unit test it without mocking out express. What I did is: file: usersRoutes.js var routes = { getAll: function(req,res) { var db = req.db; // req.db is initialized through a dedicated middleware function var usersCollection = db.get('users'); usersCollection.find({},{limit:10},function(e,results){ res.send(results); }); } }; module.exports = routes; file: users.js var express =

In debug mode, mocha doesn't stop on debugger statements in a spec file when using node-inspector

跟風遠走 提交于 2019-12-23 19:39:25
问题 When I run mocha with --debug-brk and open chrome dev tools with node-inspector, the debugger skips over any debugger statements that I put in my specfile. I can get debugger statements to work in module files provided I follow this trick of placing a breakpoint at the bottom of the mocha lib. Has anybody else seen this problem? 回答1: It seems that everybody should be seeing the same problem with node-inspector version 0.2.0beta4. The problem is in the way how breakpoints are managed: The