1
  1. I have a function that prints a string

  2. I want to test that function actually prints the string provided

The way I understand this should work is: 1. store the log in a var 2. assert content stored in the var is toBe what I expect

const log = 'hello2';

let myFunction = (log) => {
    console.log(log);
    };
myFunction(log);

const {myFunction} = require('../function-conversion');

test('Function prints message: "hello"', () => {
    expect(myFunction(log).toBe(console.log("hello")))
});

Function prints message: "hello" (1ms)

Function prints message: "hello"

ReferenceError: log is not defined

at Object.log (tests/function-conversion.test.js:4:20)

console.log ../function-conversion.js:11 hello2

2
  • I am not sure how your test should succeed, console.log does not return a value. If you want to test that a function actually prints out something, you have to redirect the standard output stream to a file and check its contents after writing. Commented Jul 19, 2019 at 10:28
  • btw you have confusion in code between log as params and log as declared outside. should be better if they have different names Commented Jul 19, 2019 at 10:29

2 Answers 2

1

There is an error to below line:

expect(myFunction(log).toBe(console.log("hello")))

While calling function myFunction(log) you should pass any string instead of log variable which is not declared anywhere (into function-conversion.test.js file) like "hello".

I don't have an environment up so couldn't test. But what you can do is:

describe('SearchResultsComponent', () => {
  let spy;
  
  let myFunction = (log) => {
    console.log(log);
  };
  myFunction(log);
  
  beforeAll(async () => {
    spy = jest.fn();
  });

  test('Function prints message: "hello"', () => {
    const log = 'hello';
    spy = jest.spyOn(console, 'log');
    myFunction(log);
    expect(spy).toHaveBeenCalled();
    expect(spy.calls.argsFor(0)).toEqual(['hello']);
  });
});

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your comment Hardik Shah, it is a very good explanation!
@Kinfol, If it helps you, then, please upvote it and you can mark this as an answer. So other can benefit. Thanks!
I am sorry, i can not upvote it. I do not have enough experience yet to do it.
No worry. You can click on up icon near by my answer and click on tick mark grey icon to accept it as an answer.
The reason i said it was a good explanation it is because it kinda lead me to the response. Your code has this error in it Cannot read property 'argsFor' of undefined I should have specified that I d not want to mock this test, but I just want to use a simple jest test, to understand how this works. I will explain in my answer.
1

I pumped up the initial function a bit. In case one should test a console.log message, he should push the string to an array.

let arrayOfMessages = [];
let functionThatPrintsTheArrayOfMessages = () => {
// pushing the messages to the array can be done when invoking the function as in line 19
//  console.log(message.push('hello','hi'));
    for (let i in arrayOfMessages){
    console.log(arrayOfMessages[i])
    };
    return arrayOfMessages;
};
functionThatPrintsTheArrayOfMessages(arrayOfMessages.push('hello', 'hi')); 

This is the jest test.

test('Function prints message: "hello", "hi"', () => {
expect(functionThatPrintsTheArrayOfMessages()).toContain('hello')

expect(functionThatPrintsTheArrayOfMessages()).toContain('hi')

expect(functionThatPrintsTheArrayOfMessages()).toEqual(['hello', 'hi'])
});

I would like to know if this is correct and other ways of testing. Involving Jest.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.