1

I want to write some Jest tests for JavaScript functions in a .mjs file. Within these functions, variables are defined by calling other functions. For example:

export function getCurrentVideo() {
     var currentVideo = VideoList.GetCurrentVideo()
    console.log(currentVideo);
    return "This is the video:" + currentVideo
}

In this case I will receive undefined, right? Because VideoList.GetCurrentVideo can't be reached.

Test will be something like:

const  getCurrentVideo  = import('../').getCurrentVideo;

describe('getCurrentVideo', () => {
    test('if getCurrentVideo will return "This is the video:" + currentVideo', () => {
        expect(getCurrentVideo).toBe('This is the video:" + currentVideo');
    });
});

I know you can add parameters to the function, but that will mean that I have to re-write the functions just for test purposes. It isn't my code, it's a huge project where the owner wants some tests for it.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Rowin_nb2
  • 164
  • 1
  • 13

1 Answers1

0

You can mock function, assuming they work correctly (and test them separately):

const  getCurrentVideo  = import('../').getCurrentVideo;
describe('getCurrentVideo', () => {
    it('if getCurrentVideo will return "This is the video:" + currentVideo', () => {
        const currentVideo = 'testCurrentVideo';
        window.VideoList = {
            GetCurrentVideo: jest.fn(() => currentVideo)
        }
        expect(getCurrentVideo).toBe('This is the video:' + currentVideo);
    });
});

Or you can provide a full context of VideoList, so that the two function are tested together.

Greedo
  • 3,438
  • 1
  • 13
  • 28
  • But if I provide a full context of the VideoList, I have to re-write the function ? The function atm is without parameters I tried your solution to mock it, but still: undefined. Because it will still try to define the variable in the real function, if I'm correct. – Rowin_nb2 Sep 29 '20 at 14:40
  • in your test implementation, is `currentVideo` defined? The `jest.fn` will mock the function based on the name, so the real function won't be executed, but instead the provided one – Greedo Sep 29 '20 at 14:45
  • Yeah it is. I did make a string of it. Instead of => currentVideo, I used: => 'currentVideo'. That should work, if I'm correct. If I use your example and define currentVideo, it will gave me the same output, undefined. – Rowin_nb2 Sep 29 '20 at 14:52
  • I think this should work, I don't see why not. But there is another problem at the moment. The import is not working (and that's maybe why your code isn't working). The function I want to test is inside a .mjs file. The error that I get is: Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript. – Rowin_nb2 Sep 30 '20 at 10:59
  • `import { sum } from '../../directoryname/sum.mjs';` But even after changing it to a .js file, I got the same error. `SyntaxError: Cannot use import statement outside a module` – Rowin_nb2 Sep 30 '20 at 11:00
  • i just notice, should be `it` and not `test` – Greedo Sep 30 '20 at 11:24