javascript - How to match partially a string in an array of Objects in Jest? - Stack Overflow

I'm using the following:Node.js: 9.8.0Jest: 22.4.2There's an array like the following being

I'm using the following:

  • Node.js: 9.8.0
  • Jest: 22.4.2

There's an array like the following being returned from myFunction:

[
    ...
    {
        id: 00000000,
        path: "www.someUrl/some/path/to"
    }
    ...
]

And I want to match it against the following kind of array:

const output = [
    ...
    {
        id: 00000000,
        path: "path/some/path/to"
    }
    ...
]

In a nutshell: I want to totally match the id, but only partially the path.

But I just don't know how... I've tried the following:

expect(myFunction()).toEqual(expect.arrayContaining(output));

But the gives me an error.

I'm using the following:

  • Node.js: 9.8.0
  • Jest: 22.4.2

There's an array like the following being returned from myFunction:

[
    ...
    {
        id: 00000000,
        path: "www.someUrl./some/path/to"
    }
    ...
]

And I want to match it against the following kind of array:

const output = [
    ...
    {
        id: 00000000,
        path: "path/some/path/to"
    }
    ...
]

In a nutshell: I want to totally match the id, but only partially the path.

But I just don't know how... I've tried the following:

expect(myFunction()).toEqual(expect.arrayContaining(output));

But the gives me an error.

Share Improve this question asked Jul 6, 2018 at 2:58 Lucas Almeida CarottaLucas Almeida Carotta 5677 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I've solved with the following code:

const output = JSON.parse(readFileSync('./myFunction.json', 'utf8'));

describe('Testing myFunction.', () => {
    test('Deafult test.', () => {
        const input = myFunction();

        input.map((value, index) => {
            const { imageURL, ...remaining } = output[index];

            expect(value).toMatchObject({
                ...remaining,
                imageURL: expect.stringContaining(imageURL)
            });
        });
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744403856a4572525.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信