开玩笑。每个名称访问对象键

新手上路,请多包涵

是否可以在 name.each 部分访问对象的密钥?

 let accounts =
    [
        {
            details:
            {
                company_name:
                    "company_name",
                email,
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "company_name",
                "email",
                "type",
            ],
            type:
                "creator"
        },
        {
            details:
            {
                email,
                first_name:
                    "first_name",
                last_name:
                    "last_name",
                password:
                    "asdf",
            },
            find:
            [
                "_id",
                "email",
                "first_name",
                "last_name",
                "type",
            ],
            type:
                "user"
        },
    ]

describe.each(accounts)(
    "%s", // <-- access the 'type' key, e.g. account.type
    function (account)
    {
        // test code
    }
)

原文由 A. L 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 202
2 个回答

Jest describe.each 在第一个参数中需要一个数组数组。如果传入一维数组,在内部它将映射到数组数组(即传递 [1, 2, 3] 作为第一个参数将转换为 [[1], [2], [3]] )。

数组中的每个数组都用作测试套件的数据。因此,在前面的示例中, describe.each 将生成三个测试套件,第一个以 1 作为数据,第二个以 2 作为 3 作为数据。

现在,在测试套件名称中,您只能格式化提供给它的参数。在您的情况下,您正在将 accounts 数组的每个对象中的数据传递给每个测试套件。因此,当您在测试套件名称中设置格式说明符时,它们将应用于整个帐户对象(即 %s 在您的示例中将字符串化您的对象,导致 [object Object] )。不幸的是,我认为您不能将格式说明符应用于对象的键。

一些想法来完成你想要的:

解决方案 1

如果使用 %s 格式化程序组成测试套件名称,将调用 Object 的 toString 方法(默认返回 [object Object] )。

如果您在每个帐户对象中定义了一个 toString 方法,那么将改用该方法。因此,我们可以使用此代码将 toString 方法添加到每个帐户对象(请注意,我们添加的 toString 方法返回 type 键):

 const accounts = [{
    details: {
        company_name: "company_name",
        email: "aa",
        password: "asdf",
    },
    find: [ "_id", "company_name", "email", "type", ],
    type: "creator"
}, {
    details: {
        email: 'bb',
        first_name: "first_name",
        last_name: "last_name",
        password: "asdf",
    },
    find: [ "_id", "email", "first_name", "last_name", "type", ],
    type: "user"
}].map(account => Object.assign(account, { toString: function() { return this.type; } }));

现在,使用 %s 格式说明符,您应该在每个测试套件中看到帐户类型:

 describe.each(accounts)(
    "%s", // <-- This will cause the toString method to be called.
    function (account)
    {
        // test code
    }
)

方案二

您始终可以重新定义每个测试套件数据,以便第一个参数是帐户类型(注意现在 accounts 是一个二维数组):

 let accounts = [
    [
        "creator",
        {
            details: {
                company_name: "company_name",
                email: "email",
                password: "asdf",
            },
            find: [ "_id", "company_name", "email", "type", ],
            type: "creator"
        }
    ], [
        "user",
        {
            details: {
                email: "email",
                first_name: "first_name",
                last_name: "last_name",
                password: "asdf",
            },
            find: [ "_id", "email", "first_name", "last_name", "type", ],
            type: "user"
        },
    ]
]

您现在可以使用第一个参数(即帐户类型)为测试套件命名:

 describe.each(accounts)(
    '%s',  // <-- This %s will format the first item in each test suite array.
    function (accountType, account) {
        // test code
    }
);

请注意,现在您的测试函数接收两个参数,因为每个测试套件数组都有两个元素。第一个是账户类型,第二个是账户数据。

解决方案 3

您可以使用 describe.each 的标记模板文字形式。使用此解决方案,您不必更改 accounts 数组的当前定义。

 describe.each`
    account
    ${accounts[0]}
    ${accounts[1]}
`('$account.type', function (account) {
    // test code
});

此解决方案的缺点是您必须在新行中手动将模板文字中的每个测试套件数据附加(即,如果您将新元素添加到 accounts 数组,您必须记住将其添加到新行中的模板文字为 ${accounts[2]} )。

原文由 mgarcia 发布,翻译遵循 CC BY-SA 4.0 许可协议

您可以映射您的初始帐户数组以将每个帐户转换为包含 2 个项目的数组:

  1. 帐户类型
  2. 初始帐户元素

现在,您可以使用 describe name 中的第一个元素数组

describe.each(accounts.map(account => [account.type, account]))(
    'testing %s', // %s replaced by account type
    (type, account) => { // note: 2 arguments now
        it('details should be defined ', () => {
            expect(account.details).toBeDefined();
        });
    },
);

原文由 Sergio Mazzoleni 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题