D
D
Destell2019-10-24 13:39:37
JavaScript
Destell, 2019-10-24 13:39:37

How do you go about testing a transform function?

There is a function that takes two data structures as input, attaches the data of the second structure to the first and converts the final result to a different format. All actions are performed by a set of smaller functions, each of which is responsible for its part. If I have to write a test that checks the correctness of the final result, what is the correct approach? Actually, what approach generally should be?
Similar to the example from the Jest documentation

const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3);
});

Create constants (variables, as you like) with examples of incoming data and the result, and substitute them into the written test?
Write separate tests for each of the internal functions, also with examples of the data that should come to them as input and be the result?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksei Podgaev, 2019-10-24
@alexiusp

It depends on the details, but in general - the first option, yes. You need to lock the input data, lock the expected output, and test like that. But this does not mean that smaller internal functions should not be tested.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question