如何在 Jest 中部分匹配对象数组中的字符串?

Luc*_*tta 3 javascript typescript jestjs

我正在使用以下内容:

  • Node.js:9.8.0
  • 开玩笑:22.4.2

myFunction 返回一个如下所示的数组:

[
    ...
    {
        id: 00000000,
        path: "www.someUrl.com/some/path/to"
    }
    ...
]
Run Code Online (Sandbox Code Playgroud)

我想将它与以下类型的数组进行匹配:

const output = [
    ...
    {
        id: 00000000,
        path: "path/some/path/to"
    }
    ...
]
Run Code Online (Sandbox Code Playgroud)

简而言之:我想完全匹配 id,但只部分匹配路径。

但我只是不知道如何......我尝试了以下方法:

expect(myFunction()).toEqual(expect.arrayContaining(output));
Run Code Online (Sandbox Code Playgroud)

但这给了我一个错误。

Luc*_*tta 5

我已经用以下代码解决了:

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)
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)