玩笑.each名称访问对象键 [英] jest .each name access object key

查看:79
本文介绍了玩笑.each名称访问对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在.eachname部分内访问对象的键?

Is it possible to access an object's key inside the name portion of a .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
    }
)

推荐答案

开玩笑 describe.each 在第一个参数中需要一个数组数组.如果您传入一维数组,则在内部会将其映射到数组数组(即,传递[1, 2, 3]作为第一个参数将被转换为[[1], [2], [3]]).

Jest describe.each expects an array of arrays in the first parameter. If you pass in a 1D array, internally it will be mapped to an array of arrays (i.e. passing [1, 2, 3] as first parameter would be converted to [[1], [2], [3]]).

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

Each one of the arrays inside of the array is used as the data for a test suite. So, in the previous example, describe.each would generate three test suites, the first with 1 as data, the second with 2 as data and the third with 3 as data.

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

Now, in the test suite name, you can only format the parameters you are providing to it. In your case, you are passing to each test suite the data in each object of the accounts array. So, when you set the format specifiers in the test suite name, they will apply to the whole account object (i.e. the %s in your example will stringify your object resulting in [object Object]). Unfortunately, I don't think you can apply the format specifiers to a key of the object.

一些想法可以实现您想要的:

Some ideas to accomplish what you want:

如果使用%s格式化程序编写测试套件名称,则将调用Object的toString方法(默认情况下返回[object Object]).

If you use the %s formatter to compose the test suite name, the toString method of Object will be called (which by default returns [object Object]).

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

If you define a toString method in each of your accounts objects, that method will be used instead. So, we could add the toString method to each one of the account objects with this code (note that the toString method we are adding is returning the value for the type key):

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格式说明符,您应该在每个测试套件中看到帐户类型:

Now, with the %s format specifier you should see the account type in each test suite:

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

解决方案2

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

Solution 2

You can always redefine each one of your test suite data so that the first parameter is the account type (note that now accounts is a 2D array):

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"
        },
    ]
]

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

You can now use that first parameter (which is the account type) to give the test suite its name:

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

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

Note that now your test function receives two parameters as each test suite array has two elements. The first one is the account type and the second one is the account data.

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

You can use the tagged template literal form of describe.each. With this solution you don't have to change your current definition of accounts array.

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

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

The downside of this solution is that you have to manually append each test suite data in the template literal in a new line (i.e. if you add a new element to the accounts array you have to remember to add it in the template literal in a new line as ${accounts[2]}).

这篇关于玩笑.each名称访问对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆