'how to test class methods with jest (on return values)

Im trying to write unit tests for class methods with jest ( new to jest) I have methods that e.g. take arrays and modify them and bring them into different form to satisfy algorithm needs. But I dont see a way how I simply can test class method receiving and returning values. Looks like there is a problem with classes, class methods cant be tested as simple functions. But if I look at the docs I dont see it covering these topics, it only covers e.g. was a class instance called, was a class method called..

Edited: this is my code example

import MyClass from "../MyClass.js";

// mocked data
const inputArrayMock=[{someObject}]
const outputArrayMock=[{modifiedObject}]

test("test MyClass method a", () => {
    const obj = new MyClass();
    const result = obj.methodA(inputArrayMock);
    expect(result).toEqual(outputArrayMock);
});

I just ran my code again, it's throwing the error:

Received: {Symbol(async_id_symbol): 293, Symbol(trigger_async_id_symbol): 281, Symbol(destroyed): {"destroyed": false}} 

Note: Both arrays (in- and output values I wrote as mock data. The expected array is correct, but the received not, which throws the error.



Solution 1:[1]

Without knowing your code I can only make a guess.

import MyClass from "../MyClass.js";

test("your test name", () => {
    const obj = new MyClass();
    const result = obj.theMethodYouWantToCheck();
    expect(result).toBe("the desired result");
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dieter Raber