简体   繁体   English

Jest 测试函数绑定

[英]Jest Test function bind

How can one bind s.th.一个人怎么能绑定某些东西? to the test function executed by jest?到 jest 执行的测试函数?

I want to run the same set of tests twice for different configs, like this:我想为不同的配置运行相同的一组测试两次,如下所示:

function wrap(title, fn) {
    [1,2].forEach(x => {
        this.hello = x
        fn.bind(this)
        fn() // works
        test(title, fn) // does not work
    })
}

wrap('test hello world', function() {
    console.log('this.hello', this.hello)
});

But this is undefined when run by jest.但是当由 jest 运行时, thisundefined

Turns out jest provides a built-in helper to do exactly this:结果 jest 提供了一个内置的帮助程序来做到这一点:

describe.each([1,2])('test', x => {
    test(`test ${x}`, function() {
        console.log('x', x)
    });
})
HTML CODE : 

<!-- createNewAbout is my module name and submitAboutContent is the Controller You you can call it the backend function . 

You Can Use URL in Form Action too.您也可以在表单操作中使用 URL。 --> Enter Title Enter Description Add Image Submit --> 输入标题 输入描述 添加图片 提交

<!-- this is the function Which i am Calling . I am taking value of  name attribute of button to disable button for multiple fuction call and also taking an array of values of name attribute of  manditory fields. Here we cant use values of ID attribute so we have to use name . -->
<script> 
function validationOnlySubmitForm(button_name="", arr="") {
var fields = arr, flag  = 0;
document.getElementsByName(button_name)[0].setAttribute("disabled" , true);
for (var i = 0 ; i <= fields.length - 1 ; i++) {
if (document.getElementsByName(fields[i])[0].value.length == 0) {
flag = 1;
document.getElementsByName(fields[i])[0].focus();
alert(fields[i] + " is manditory.");
document.getElementsByName(button_name)[0].removeAttribute("disabled");
return false;
}
}

if (flag == 0)
return true;
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM