V
V
Vadim2019-09-20 09:45:35
Amazon Web Services
Vadim, 2019-09-20 09:45:35

How can you swap a function call inside another function during testing?

Function example:

const getSomeData = require('path/to/file.js');

const someFunction = async () => {
  const someRequestedData = await getSomeData(param1,param2);
  // do something
  return result;
};
module.exports = someFunction;

The getSomeData function calls AWS Lambda directly, which, in turn, makes a request to DynamoDB and retrieves certain data. The problem is that when testing to simulate some states and conditions, there will be no data in the database, but you need to test them.
Tell me, is it possible somehow during testing on Mocha / Chai or Karma in the test when calling someFunction , when calling getSomeData , write an object with data yourself and return it so that there is no real call to getSomeData ? Something like: if there is a call to getSomeData , return such and such data.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shvets, 2019-09-20
@Xuxicheta

https://sinonjs.org/
https://scotch.io/tutorials/how-to-test-nodejs-app...

L
Lynn "Coffee Man", 2019-09-20
@Lynn

I don't really like jest, but everything is already there
https://jestjs.io/docs/en/mock-functions#mocking-m...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question