Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call-through original function? #512

Closed
13 tasks
aytekin-smartcar opened this issue Apr 20, 2023 · 1 comment
Closed
13 tasks

How to call-through original function? #512

aytekin-smartcar opened this issue Apr 20, 2023 · 1 comment

Comments

@aytekin-smartcar
Copy link

Description

I want to spy and call through original function.

Issue

I am getting RangeError: Maximum call stack size exceeded

Environment

  • node -v output: v14.19.1
  • npm -v (or yarn --version) output: 6.14.16
  • npm ls testdouble (or yarn list testdouble) version:

Failing Test

  • Fork the repo
  • Add a failing test (probably to the `/regression/src' directory)
  • Submit a pull request for the failing test or link to your branch here

Example Repo

  • Create a minimal repository that reproduces the issue
  • Make sure that a fresh clone can run only npm it and observe the issue
  • Link to that repo here

Runkit Notebook

  • Create a Runkit notebook
  • Invoke var td = require('testdouble') at the top
  • Verify the behavior your issue is concerned with by clicking "Run"
  • Link to the Runkit here

Code-fenced Examples

import * as td from 'testdouble';

const myObj = {
  myFunc(arg1, arg2) {
    console.log(`I am executed with: ${arg1} and ${arg2}`);
  }
};

// Create a test double for `myObj.myFunc`
const myFunc = td.replace(myObj, 'myFunc');

// Call through the test double and execute the original `myObj.myFunc` method
td.when(myFunc('foo', 'bar')).thenDo((...args) => myObj.myFunc(...args));

// Call the code that uses `myObj.myFunc`
myObj.myFunc('foo', 'bar');

// Verify that `myObj.myFunc` was called once with the specified arguments
td.verify(myObj.myFunc('foo', 'bar'), { times: 1 });

console.log('ALL TESTS PASSED!');

I would like to spy on an existing function and also execute it. Specifically, I want to run

console.log(`I am executed with: ${arg1} and ${arg2}`);

In some other libraries, this can be done with a callThrough method, but I have been unable to do so here. Instead, I am receiving a RangeError: Maximum call stack size exceeded.

How can I resolve this issue?

@searls
Copy link
Member

searls commented Apr 28, 2023

td.replace overwrites the reference until td.reset is called. If you wanted to do this for whatever reason, you'd have to grab a reference to the function you're overwriting (see ogFunc = … below).

Note that stubbing this interaction and then verifying it was called is a separate issue that triggers a warning in testdouble.js because it's a common test smell with mocking

const td = require('testdouble');


const myObj = {
  myFunc(arg1, arg2) {
    console.log(`I am executed with: ${arg1} and ${arg2}`);
  }
};


const ogFunc = myObj.myFunc;
// Overwrite myObj.myFunc with a test double
const myFunc = td.replace(myObj, 'myFunc');

// Call through the test double and execute the original `myObj.myFunc` method
td.when(myFunc('foo', 'bar')).thenDo((...args) => ogFunc(...args));

// Call the code that uses `myObj.myFunc`
myObj.myFunc('foo', 'bar');

// Verify that `myObj.myFunc` was called once with the specified arguments
td.verify(myObj.myFunc('foo', 'bar'), { times: 1 });

// Restore myObj.myFunc
td.reset();

console.log('ALL TESTS PASSED!');

@searls searls closed this as completed Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants