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

查看:17
本文介绍了jest .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
    }
)

推荐答案

Jest 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 方法正在返回 输入键):

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 是一个二维数组):

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.

您可以使用 describe 的标记模板文字形式.每个.使用此解决方案,您无需更改 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]}).

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

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