Questions tagged [testdoublejs]

testdouble.js is a library for replacing real things for fake things in your unit tests, most often to facilitate test-driven development. For more reading on what a test double is and the various types, see this wiki page.

Before opening a question, be sure to peruse past issues and docs on the repo, as many people go there first to ask questions.

14 questions
16
votes
1 answer

How do you mock typeorm's getManager using testdouble?

When creating unit tests for typeorm, I want to mock my connection to the database so that I can run unit tests without actually ever connecting to the DB (a good thing!) I see places where people have mocked typeorm's repositories using testdouble…
CleverPatrick
  • 9,261
  • 5
  • 63
  • 86
11
votes
2 answers

Testing Promises with multiple thens using testdoublejs

I am using testdouble for stubbing calls within my node.js project. This particular function is wrapping a promise and has multiple then calls within the function itself. function getUser (rethink, username) { return new Promise((resolve, reject)…
ckross01
  • 1,681
  • 2
  • 17
  • 26
4
votes
0 answers

JS: Stub a method to do unit test via testdouble

I'm trying to 'stub' a method via testdoubleJS to do a unit test for this method (doing npm test). It is the first time I'm doing this, so it is still hard to understand for me. For my attempt - shown below - I do get the error TypeError:…
user3142695
  • 15,844
  • 47
  • 176
  • 332
2
votes
0 answers

Mocking es6 with mocha in Typescript

I am struggling to properly stub/mock unit tests when using es6 modules along with a project with mixed .js and .ts files. According to this post, testdouble should be able to provide the ESM mocking I need. However, it requires using…
2
votes
2 answers

Mocking third party library (ioredis) in typescript tests

I am having a surprisingly hard time being able to mock a third party library in my typescript tests. I am making a library based upon this typescript-starter library. It uses ava for testing. In my case I am trying to mock the main class of ioredis…
Automatico
  • 12,420
  • 9
  • 82
  • 110
1
vote
0 answers

Why is testDouble not able to mock a 3rd party function?

I have a local module with a function that I want to mock using tetDouble for nodeJS This is the function that I want to test: import {supportsAPL} from "skills-lib" export function example(thing: any): boolean { if (!supportsAPL(thing)) { …
linker85
  • 1,601
  • 5
  • 26
  • 44
1
vote
1 answer

Replacing internal functions with testdouble.js?

Is it possible to td.replace an internal function in a node.js module when testing with testdouble.js? The internal function consists of a DB call, so I do not want to test it. I do however want to test that this function received the expected…
JK.
  • 21,477
  • 35
  • 135
  • 214
0
votes
0 answers

Using testdouble I want to replace a dependency

In my code, I am using the library jsonwebtoken. To expedite the testing process, I would like to replace the library with a fake dependency using testdouble. To test the creation of my fake dependency, I replaced the functionality of the sign…
pythonNovice
  • 1,130
  • 1
  • 14
  • 36
0
votes
0 answers

How can I replace a function with a testdouble function?

I would like to test a file with testdouble. However, I have a method helperFunc that I am unsure how to mock in the file here. If it were a class method, I would use MyClass.prototype.method = td.func() but here I tried using const helperFunc =…
pythonNovice
  • 1,130
  • 1
  • 14
  • 36
0
votes
1 answer

Testdouble usage of td.when not getting invoked inside td.object

Probably my misunderstanding of Testdouble, but I've created this example to illustrate the issue I'm having: const test = require("ava"); const td = require("testdouble"); const reducer = async (state, event) => { if (event.id === "123") { …
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
0
votes
2 answers

How to properly stub a function return value?

Premise: JS ES6, NodeJS Testing Framework: TAP Mocking Library: testdouble.js I am attempting to mock the return value for the method of my class and keep receiving this error: not ok Unsatisfied verification on test double. Wanted: -…
Doug Wilhelm
  • 776
  • 9
  • 26
0
votes
1 answer

Delaying stub answer in Javascript

In my production code I have a function which is blocking program for 10 ms (because of use execSync). In my test I used testdouble.js library to stub it: td.when(getSignalStrength()).thenReturn.apply(null, array); However stub is responding…
user3134600
  • 1
  • 1
  • 2
0
votes
1 answer

Replacing dependencies in AMD module format with testdouble.js

I'm writing tests for a JS application using Jasmine and testdouble.js as a mocking library. I am using AMD format to organize code in modules, and RequreJS as a module loader. I was wondering how to use testdouble.js to replace dependency for the…
vladamon
  • 297
  • 1
  • 3
  • 11
-1
votes
1 answer

How to test a privately-scoped or anonymous function?

Imagine I have the following modules: foo.js module.exports = function (x, f) { f(x); }; bar.js const foo = require('./foo'); module.exports = function () { foo(40, n => n + 2); // ^ // f — How can I test this lambda? }; I only…
customcommander
  • 17,580
  • 5
  • 58
  • 84