sinon

How to test async function with Sinon JS?

℡╲_俬逩灬. 提交于 2021-02-11 17:06:19
问题 Below is a minimal example of what I want to achieve: I want to test fn3() being called if the async function fn2() resolved (when calling fn1 ). But I somehow failed to do that with sinon 's stub syntax. I would like to know what I misunderstood. // System Under Test export function fn1() { fn2().then(() => { fn3(); }); } export async function fn2() { return new Promise((resolve, reject) => { // expensive work resolve(); }); } export function fn3() { console.log("fn3"); } // Test import * as

How to test async function with Sinon JS?

旧街凉风 提交于 2021-02-11 17:05:31
问题 Below is a minimal example of what I want to achieve: I want to test fn3() being called if the async function fn2() resolved (when calling fn1 ). But I somehow failed to do that with sinon 's stub syntax. I would like to know what I misunderstood. // System Under Test export function fn1() { fn2().then(() => { fn3(); }); } export async function fn2() { return new Promise((resolve, reject) => { // expensive work resolve(); }); } export function fn3() { console.log("fn3"); } // Test import * as

setTimeout not triggered while using Sinon's fake timers

核能气质少年 提交于 2021-02-08 04:33:30
问题 I have a test similar to what is shown below. Basically I want to test if a specific method is getting delayed. The following example works as expected, i.e. the resolve method gets called and the test passes: it(`should delay execution by 1 second`, function () { const clock = sandbox.useFakeTimers(); const p = new Promise(function (resolve) { setTimeout(resolve, 1000); }); clock.tick(1000); return p; }); However, if I wrap the setTimeout in another Promise, the resolve never gets called: it

setTimeout not triggered while using Sinon's fake timers

 ̄綄美尐妖づ 提交于 2021-02-08 04:32:10
问题 I have a test similar to what is shown below. Basically I want to test if a specific method is getting delayed. The following example works as expected, i.e. the resolve method gets called and the test passes: it(`should delay execution by 1 second`, function () { const clock = sandbox.useFakeTimers(); const p = new Promise(function (resolve) { setTimeout(resolve, 1000); }); clock.tick(1000); return p; }); However, if I wrap the setTimeout in another Promise, the resolve never gets called: it

How to mock/replace getter function of object with Jest?

浪尽此生 提交于 2021-02-05 20:40:11
问题 In Sinon I can do the following: var myObj = { prop: 'foo' }; sinon.stub(myObj, 'prop').get(function getterFn() { return 'bar'; }); myObj.prop; // 'bar' But how can I do the same with Jest? I can't just overwrite the function with something like jest.fn() , because it won't replace the getter "can't set the value of get" 回答1: You could use Object.defineProperty Object.defineProperty(myObj, 'prop', { get: jest.fn(() => 'bar'), set: jest.fn() }); 回答2: For anyone else stumbling across this

Test simple logger functions with full code coverage

…衆ロ難τιáo~ 提交于 2021-01-29 14:33:35
问题 I'm using Chai, Sinon and Instanbul to test a NodeJS application. Here's the Logger code: import Debug, { IDebugger } from 'debug'; export default class Logger { private readonly dbg: IDebugger; constructor(name: string) { this.dbg = Debug(name); } public log(str: string): void { this.dbg(str); } } Here's the test that I have built to start with: import * as fs from 'fs'; import sinon from 'sinon'; import { expect } from 'chai'; import Logger from '../../src/utils/Logger'; import Debug from

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("

sinon stub for Lambda using promises

天涯浪子 提交于 2021-01-28 08:27:45
问题 I just started using sinon, and I had some initial success stubbing out DynamoDB calls: sandbox = sinon.createSandbox() update_stub = sandbox.stub(AWS.DynamoDB.DocumentClient.prototype, 'update').returns({ promise: () => Promise.resolve(update_meeting_result) }) This works great. But I also need to stub Lambda, and the same approach isn't working: lambda_stub = sandbox.stub(AWS.Lambda.prototype, 'invoke').returns({ promise: () => Promise.resolve({lambda_invoke_result}) // }) With this, I get

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