sinon

Testing Mouseover event in D3 with Sinon

别来无恙 提交于 2019-12-08 17:59:50
问题 I'm having trouble trying to get my test to pass. I would like to be able to use a spy to check whether or not the mouseover event was correctly called. Currently I am getting the following error, "Error: expected on to have been called at least once but was never called". Part of the confusion on my part relates to the differences between d3 and jQuery selectors, I'm more than happy to use the latter, but I'm not sure how to correctly use the former in a test to get the result I want. My

JavaScript / Mocha - How to test if function call was awaited

送分小仙女□ 提交于 2019-12-08 17:38:18
问题 I would like to write a test that check if my function calls other functions using the await keyword. I'd like my test to fail : async methodA() { this.methodB(); return true; }, I'd like my test to succeed : async methodA() { await this.methodB(); return true; }, I'd like my test to succeed too: methodA() { return this.methodB() .then(() => true); }, I have a solution by stubbing the method and force it to return fake promise inside it using process.nextTick , but it seems to be ugly, and I

How to make Istanbul generate coverage for all of my source code?

坚强是说给别人听的谎言 提交于 2019-12-08 14:37:50
问题 Currently Istanbul is only generating coverage for files that are used in my tests, which is okay, but seems to defeat the purpose of having coverage somewhat. I have no Istanbul configuration, and am invoking it via npm test with the following script string: $ istanbul cover _mocha -- -R dot --check-leaks --recursive test/ Is there a way to generate coverage for all of my source code? 回答1: Found the answer, I think I'm partly lucky that the directory structure I have chosen allows me to use

Sinon Fake XML Not Capturing Requests

拥有回忆 提交于 2019-12-08 07:33:07
问题 I'm trying to write some tests using Lab and Sinon for various HTTP requests that are called in a file of mine. I followed the Fake XMLHttpRequest example at http://sinonjs.org/ but when I run my tests it appears to not actually capture any requests. Here is the (relevant) testing code: context('when provided a valid payload', function () { let xhr; let requests; before(function (done) { xhr = sinon.useFakeXMLHttpRequest(); requests = []; xhr.onCreate = function (req) { requests.push(req); };

When using rewire and sinon fakeTimer, order matters?

ε祈祈猫儿з 提交于 2019-12-08 02:28:49
问题 While testing a setup with a timed interval, I came across this problem. First of all, I am using sinon's fakeTimers to create the right timed environment. rewire is used as dependency injection library. The problem is, that sometimes applying the fake timer seems to fail when rewire is involved while in some other instances it's perfectly working. Please check out this setup: test.js 'use strict'; require('should'); var sinon = require('sinon'); var rewire = require('rewire'); // this sample

Testing Ember Simple Auth in Ember App Kit with sinon

天涯浪子 提交于 2019-12-07 20:00:35
问题 I would like to mock server login responses when testing Ember Simple Auth in my Ember App Kit application. However, with the following code I get an opaque error 'Unexpected end of input' when the click action is called on the visit function : var App; module('Acceptances - SignIn', { setup: function(){ App = startApp(); this.xhr = sinon.useFakeXMLHttpRequest(); this.server = sinon.fakeServer.create(); this.server.autoRespond = true; sinon.spy(Ember.$, 'ajax'); this.server.respondWith('POST'

Sinon.js- Trying to spy on console.log but it is already wrapped

限于喜欢 提交于 2019-12-07 17:56:59
问题 I am new to writing node tests.... this is a my first unit test for a gulp plugin I am trying to write: var chai = require('chai'); var spy = require('chai-spies'); var es = require('event-stream'); var File = require('vinyl'); var mock = require('mock-fs'); var sinon = require('sinon'); var sinonChai = require("sinon-chai"); //var rewire = require("rewire"); //var myModule = rewire("./test.js"); var es = require('event-stream'); chai.should(); chai.use(sinonChai); describe('gulp-newy',

How to sinon spy module export utility functions

与世无争的帅哥 提交于 2019-12-07 15:06:53
问题 In javascript (ES6), I have a utility module which just contains some functions, and then in the end of the file, I export them like so: module.exports = { someFunction1, someFunction2, someFunction3, } Then I want to write unit tests for those functions. Some of the functions depend on each other; they call each other in a way, that for example, someFunction1 might call someFunction2. There's no circular problems. Everything works well, until I need to spy that one of the functions is called

Asserting function calls inside a promise

无人久伴 提交于 2019-12-07 13:02:50
问题 I'm writing some tests for an async node.js function which returns a promise using the Mocha, Chai and Sinon libraries. Let's say this is my function: function foo(params) { return ( mkdir(params) .then(dir => writeFile(dir)) ) } mkdir & writeFile are both async functions which return promises. I need to test that mkdir is being called once with the params given to foo . How can I do this? I've seen quite a few examples on how to assert the overall return value of foo (sinon-as-promised is

Mock fs.readdir for testing

穿精又带淫゛_ 提交于 2019-12-07 05:04:30
问题 I'm trying to mock the function fs.readdir for my tests. At first I've tried to use sinon because this is a very good framework for this, but is hasn't worked. stub(fs, 'readdir').yieldsTo('callback', { error: null, files: ['index.md', 'page1.md', 'page2.md'] }); My second attempt was to mock the function with a self-replaced function. But it also doesn't works. beforeEach(function () { original = fs.readdir; fs.readdir = function (path, callback) { callback(null, ['/content/index.md', '