sinon-chai

Unable to mock node-fetch using fetch-mock

点点圈 提交于 2021-02-11 05:33:24
问题 I am trying to do unit testing for a simple function which sends a get request, receives a response and then returns a promise object with the success or the failure message. Following is the function: module.exports.hello = async (event, context) => { return new Promise((resolve, reject) => { fetch("https://httpstat.us/429", { headers: { 'Content-Type': 'application/json' } }).then(response => { console.log(response); if (response.status == 200) { return response; } else { throw Error

Sinon stub replacing function not working

五迷三道 提交于 2021-01-29 12:11:58
问题 I am isolated the problem I am facing in my nodeJs here. Sinon stubbing on a dependent function is not working as expected. I didn't get what I am missing here. Appreciate help. Here is the sample code. sinonTest.js "use strict"; function getSecretNumber () { return 44; } function getTheSecret () { return `The secret was: ${getSecretNumber()}`; } module.exports = { getSecretNumber, getTheSecret, }; sinonTest_spec.ts "use strict"; const sinon = require("sinon"); const sinonMediator = require("

How to mock npm module with sinon/mocha

不想你离开。 提交于 2021-01-27 06:42:00
问题 I'm trying to test a function that calls the module cors . I want to test that cors would be called. For that, I'd have to stub/mock it. Here is the function cors.js const cors = require("cors"); const setCors = () => cors({origin: 'http//localhost:3000'}); module.exports = { setCors } My idea of testing such function would be something like cors.test.js describe("setCors", () => { it("should call cors", () => { sinon.stub(cors) setCors(); expect(cors).to.have.been.calledOnce; }); }); Any

How to mock npm module with sinon/mocha

这一生的挚爱 提交于 2021-01-27 06:41:02
问题 I'm trying to test a function that calls the module cors . I want to test that cors would be called. For that, I'd have to stub/mock it. Here is the function cors.js const cors = require("cors"); const setCors = () => cors({origin: 'http//localhost:3000'}); module.exports = { setCors } My idea of testing such function would be something like cors.test.js describe("setCors", () => { it("should call cors", () => { sinon.stub(cors) setCors(); expect(cors).to.have.been.calledOnce; }); }); Any

sinon calledWith(new Error()) and with exact message

纵饮孤独 提交于 2020-01-24 04:28:13
问题 i need to test this function : //user.js function getUser(req,res,next){ helper.get_user(param1, param2, (err,file)=>{ if (err) return next(err);} my test function : it ("failed - helper.get_user throws error",sinon.test(function () { var req,res; var get_user = this.stub(helper,"get_user") get_user.yields(new Error("message")); var next = sinon.spy(next); user.get_user(req,res,next); expect(next).to.have.been.calledWith(new Error("other message")); })) for my assertion I'm using sinon-chai

How to get a sinon stub to call another function on nth call

天涯浪子 提交于 2020-01-07 05:46:27
问题 I want to use a sinon stub to asynchronously test an event emitter. I want the stub to call a callback after it is called. I thought stub.yields was what I want but not. Is there a neat way to do this? it('asynchronously emits finish after logging is complete', function(done){ const EE = require('events'); const testEmitter = new EE(); var cb = sinon.stub(); cb.calls(completed); // no such method but this is what I need testEmitter.on('finish', cb.bind(null)); testEmitter.emit('finish');

Sinon stubbing helper method defined in same file

僤鯓⒐⒋嵵緔 提交于 2019-12-23 09:52:37
问题 So I have a file, user-database , that looks something like this : export function foo(id: number): Promise<boolean> { return new Promise<boolean>((resolve, reject) => { findSomething(id) .then((data) => { //do something with data }) } } export function findSomething(id: number): Promise<Object> { return new Promise<Object> ((resolve, reject) => { let query = 'SELECT * FROM user'; db.executeQuery(query); .then(data) => { if(data.length < 1) { reject(new Error('whoops')); } resolve(data); },

How to unit test localStorage using sinon

时光毁灭记忆、已成空白 提交于 2019-12-22 09:48:45
问题 I am trying to test localStorage using sinon. Basically I am very new to unit testing so this might be very basic. Update I managed to come up with this but now its giving me a new error Should wrap property of object Test describe('Initial State', () => { it('should set the initial state for the component', () => { const props = { currentUser: {} }; sinon.stub(window.localStorage, 'setItem'); window.localStorage.setItem('none', 'nothing'); }); }); 回答1: I managed to resolve it. Thanks to

sinonStub.called prints false, though I am calling the function

无人久伴 提交于 2019-12-13 20:58:59
问题 I am running test on node js app using sinon. I want the sinonStub.called to be true but this prints false. I am calling the function indirectly( function call within function). I have given code snippet below spec.js describe.only('creating stub for Accounts method',function(){ mockResponse= [ { "AccountID": "xyz", .... } ] req1= { user:{ id:"" }, }, res1= { json:sinon.spy()//Is this correct } it('should call getActivatedAccounts and always this mock response',function(){ var getAccountsStub

How to unit-test an object that contain a type union using typescript, karma and sinon?

心已入冬 提交于 2019-12-13 02:47:33
问题 I'm doing unit-testing for a project which is written in typescript with angular framework, by applying karma with mocha and chai frameworks. And there's an interface for the object as: interface ISolution { _id: string; paymentFrequency: PaymentFrequency; }; And PaymentFrequency type is defined as: type PaymentFrequency = 'MONTHLY' | 'ANNUAL' | 'SINGLE'; In the controller, open(solution: ISolution) { }; The problem is when I tried to mock the solution as: let solution = { _id: 'aa0',