mocha

Handling dependencies not installed with npm, in Mocha?

£可爱£侵袭症+ 提交于 2019-12-22 11:03:30
问题 I have a working Node application and I'm trying to add Mocha tests, but getting some odd import errors. This is my file structure: package.json index.js src/ chart.js test/ test_chart.js This is what my chart.js file looks like: global.jQuery = require('jquery'); global.$ = global.jQuery; require('typeahead'); require('bloodhound'); var bootstrap = require('bootstrap'); var Handlebars = require('handlebars'); var Highcharts = require('highcharts-browserify'); var parse = require('csv-parse')

How to require modules for testing NodeJS with Intern JS?

喜欢而已 提交于 2019-12-22 09:47:54
问题 I'm getting the error Attempt to require unloaded module xxxxxx with the following intern.js test (myfile.js below does a require('xxxxxx') ) for testing NodeJS. define(function(require) { var bdd = require('intern!bdd'); var myfile = require('../myfile.js'); bdd.describe('the thing being tested', function() { bdd.it('do the test', function() { ... The directory structure is intern.js myfile.js test |-- test.js How do I properly require a file? There's no examples on how to do it with the BDD

How to setup unit test in Docker for nodejs application?

时间秒杀一切 提交于 2019-12-22 08:55:59
问题 I am trying to run mocha unit test for my node application. The application is built by a docker image. Docker image: FROM node:6.10.0-alpine RUN mkdir -p /app WORKDIR /app COPY package.json /app RUN npm install COPY . /app EXPOSE 3000 CMD ["npm", "start"] Docker compose: version: "3" services: web: #### nodejs image build: . volumes: - ./app/ ports: - "3000:3000" depends_on: - db db: build: ##### postgres db image context: . dockerfile: dbDockerfile ports: - 5432:5432 The setup can be built

sails.js + mocha + supertest + sinon: how to stub sails.js controller function

痞子三分冷 提交于 2019-12-22 08:35:08
问题 I am trying to stub a sails controller function, but I don't know which object to stub. using sinon.stub(object,'funcname', function()... This is probably related to the way sails bind controller functions... Here is some code to give example Controller file api/controllers/PersonController.js var fs = require('fs'); // // I want to stub retrieveData function when testing // function retreiveData(cb) { fs.readFile('./filedata', function (err, data) { if (err) throw err; cb(data.toString()); }

Mocha JS: How to reuse assertions within a spec?

折月煮酒 提交于 2019-12-22 08:28:01
问题 I'm using Mocha bdd for unit testing. Within my specifications, multiple test cases make use of the same assertions. I would like to pull these shared assertions out into a reusable block. How can I do this? 回答1: Here is an example of parameterized tests: 'use strict'; var assert = require('chai').assert; var pascalize = require('inflection/pascalize'); var providers = [ { name: 'Single Word', input: 'commons', output: 'Commons' }, { name: 'Single Space', input: 'creative commons', output:

How to run Node.js and Ruby tests within one project on Travis CI

隐身守侯 提交于 2019-12-22 06:50:07
问题 I have a repo that contains multiple components, most of them in JavaScript (Node.js) and one written in Ruby (Ruby on Rails). I'd like to have one .travis.yml file that triggers one build that runs all the tests for each of the components. According to this Travis CI Google Group thread, there is no official support for this for now. My directory structure looks like this: . ├── buildserver ├── core ├── extensions ├── webapp ├── Vagrantfile ├── package.json ├── .travis.yml └── Makefile I

mocha --watch and mongoose models

浪尽此生 提交于 2019-12-22 06:45:41
问题 If I leave mocha watching for changes, every time I save a file mongoose throws the following error: OverwriteModelError: Cannot overwrite Client model once compiled I know that mongoose won't allow to define a model twice, but I don't know how to make it work with mocha --watch . // client.js var mongoose = require('mongoose'); var Schema = mongoose.Schema; var clientSchema = new Schema({ secret: { type: String, required: true, unique: true }, name: String, description: String, grant_types:

Mocking/Stubbing `super` calls

こ雲淡風輕ζ 提交于 2019-12-22 06:38:04
问题 I would like to mock out super calls, especially constructors in some ES6 classes. For example import Bar from 'bar'; class Foo extends Bar { constructor(opts) { ... super(opts); } someFunc() { super.someFunc('asdf'); } } And then in my test, I would like to do something like import Foo from '../lib/foo'; import Bar from 'bar'; describe('constructor', function() { it('should call super', function() { let opts = Symbol('opts'); let constructorStub = sinon.stub(Bar, 'constructor'); new Foo(opts

correct usage of sinon's fake XMLHttpRequest

女生的网名这么多〃 提交于 2019-12-22 05:58:09
问题 I am creating XMLHttpRequest javascript module to get JSON data from server. Here is the code: (function() { var makeRequest = function(url,callback,opt) { var xhr; if (XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (ActiveXObject) { // IE try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xhr) { callback.call(this, 'Giving up :( Cannot create an XMLHTTP instance', null);

correct usage of sinon's fake XMLHttpRequest

∥☆過路亽.° 提交于 2019-12-22 05:58:01
问题 I am creating XMLHttpRequest javascript module to get JSON data from server. Here is the code: (function() { var makeRequest = function(url,callback,opt) { var xhr; if (XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (ActiveXObject) { // IE try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!xhr) { callback.call(this, 'Giving up :( Cannot create an XMLHTTP instance', null);