最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I write test case for a function which uses uuid using jest? - Stack Overflow

programmeradmin3浏览0评论

I'm using jest for writing test cases. One of my function uses uuid and due to which it is not a pure function. The code is something like this:

const myFunc = () => {
    const a = uuid();

    return a;
}

I'm writing my test case as :

test('should return a unique id value', () => {
    const a = uuid();
    expect(myFunc()).toEqual(a);
});

Obviously it won't work as it will generate a unique Id every time. How can I write test case for such function.

[EDIT]

I don't want test the uuid function as it will always generate a new id and it is a library function which is already being tested. I want to test another function which uses uuid, generates new object and returns that. This function is used by some other function and so on which makes me not able to test any of that because the initial object has an id which is different that the id which I'm using while testing.

I'm using jest for writing test cases. One of my function uses uuid and due to which it is not a pure function. The code is something like this:

const myFunc = () => {
    const a = uuid();

    return a;
}

I'm writing my test case as :

test('should return a unique id value', () => {
    const a = uuid();
    expect(myFunc()).toEqual(a);
});

Obviously it won't work as it will generate a unique Id every time. How can I write test case for such function.

[EDIT]

I don't want test the uuid function as it will always generate a new id and it is a library function which is already being tested. I want to test another function which uses uuid, generates new object and returns that. This function is used by some other function and so on which makes me not able to test any of that because the initial object has an id which is different that the id which I'm using while testing.

Share Improve this question edited Mar 17, 2018 at 7:50 Ajay Gaur asked Mar 17, 2018 at 7:40 Ajay GaurAjay Gaur 5,2707 gold badges40 silver badges63 bronze badges 7
  • You need to answer the question what exactly do you want to test first. And this would already be a 50% of the answer to what you are asking. Now it looks like you want to test because of you do not know what – smnbbrv Commented Mar 17, 2018 at 7:43
  • I want to test if this function generates a unique Id every time. – Ajay Gaur Commented Mar 17, 2018 at 7:44
  • What are you testiing? myFunc or uuid? uuid is library function which does not need to be tested. So just mock that uuid function. – Prakash Sharma Commented Mar 17, 2018 at 7:44
  • The thing is, I have a function which generates unique Id then some 2nd function uses it. Then 2nd function is used by 3rd function and so on and I'm not able to test any of that. – Ajay Gaur Commented Mar 17, 2018 at 7:45
  • 1 You should test only two things : uuid() is called and it's value is returned, whatever it is. So you need to mock uuid() to return a dummy value and you will be able to test both. – Gabriel Bleu Commented Mar 17, 2018 at 8:53
 |  Show 2 more comments

2 Answers 2

Reset to default 15

I have found this works really nice.

At the top of test file:

// your regular imports here

jest.mock('uuid', () => ({ v4: () => '00000000-0000-0000-0000-000000000000' }));

// describe('test suite here', () => {

in the actual code:

import { v4 as uuidv4 } from 'uuid';

You can use jest.mock inorder to mock the import of uuid, like that:

const uuidMock = jest.fn().mockImplementation(() => {
    return 'my-none-unique-uuid';
});
jest.mock('uuid', () => {
    return uuidMock;
});

The only caveat of that approach is that you need to apply the mock in the test file before you are importing your real file.

Then you will even able to assert on the mock.

For more info read jest.mock.

发布评论

评论列表(0)

  1. 暂无评论