草庐IT

catch-unit-test

全部标签

javascript - catch block 中的 return 语句发生了什么

我已经在javascript中尝试过这段代码functionabc(){try{console.log(0);throw"isempty";}catch(err){console.log(1);returntrue;}finally{returnfalse;}return(4);}console.log(abc());我得到的输出是错误的。我明白Finally总是执行,不管trycatch的结果如何,但是catch中的return语句会发生什么。 最佳答案 IunderstandFinallyalwaysexecuteregardl

Javascript 正则表达式.test() "Uncaught TypeError: undefined is not a function"

只是尝试通过.test()函数使用javascript的正则表达式功能。varnameRegex='/^[a-zA-Z0-9_]{6,20}$/';if(nameRegex.test($('#username').val())){...}错误在这一行if(nameRegex.test($('#username').val())){调试器在那里中断并说“UncaughtTypeError:undefinedisnotafunction”。好像.test()没有定义?不应该吗? 最佳答案 就目前而言,nameRegex不是正则表达式而是

javascript - 守夜人 : Better way than `.pause(1000)` to avoid brittle tests?

.pause(1000)真的是等待表单提交的最佳实践吗?我正在寻找一种可靠地提交表单的方法,而无需知道作为表单提交结果出现的页面的详细信息。例子来自homepage使用.pause(1000)等待表单提交,具有讽刺意味的是不再起作用,但是这个带有修改后的css-selector版本的版本可以:module.exports={'DemotestGoogle':function(client){client.url('http://www.google.com').waitForElementVisible('body',1000).assert.title('Google').asser

javascript - 条件 catch 子句 - 浏览器支持

哪些浏览器支持条件捕获子句?在MDN上try...catch你可以找到Conditionalcatchclauses作为非标准功能。try{myroutine();//maythrowthreeexceptions}catch(eifeinstanceofTypeError){//statementstohandleTypeErrorexceptions}catch(eifeinstanceofRangeError){//statementstohandleRangeErrorexceptions}catch(eifeinstanceofEvalError){//statementst

javascript - 我可以在不指定 catch 参数/标识符的情况下在 JavaScript 中使用 try/catch 吗?

我想知道是否有一种方法可以在执行JStry/catch时不指定参数。不过,每次我尝试这样做时,try/catch都不起作用。工作版本:try{//Breakingcode}catch(e){//Nothinghappenshere}我的想法(没有'e'):try{//Breakingcode}catch(){//Nothinghappenshere} 最佳答案 2019年可选的catch绑定(bind)节点.js在Node.js中,此功能称为OptionalCatchBinding,自Node.js版本10.3起受支持,请参阅htt

javascript - Jest : Testing window. location.reload

如何编写测试以确保方法reloadFn实际上重新加载窗口?我找到了thisresource但是我不清楚在给定函数中发生窗口重新加载时编写测试时如何期望窗口重新加载。感谢您的帮助!constreloadFn=()=>{window.location.reload(true);} 最佳答案 更新答案(2021年11月)包装:“开Jest”:“^26.6.0”"@testing-library/jest-dom":"^5.11.4"构建:create-react-app4describe("testwindowlocation'srelo

javascript - 在 JavaScript 中使用 try-catch

在JavaScript中使用try/catch有多耗时?我有一个应用程序,我在一个被调用数百次的函数中使用它。现在我担心try/catch语句花费了太多时间,应用程序将花费比没有它更长的时间。 最佳答案 jsPref上有一些不错的测试:http://jsperf.com/try-catch-performance-overheadhttp://jsperf.com/try-catch-versus-massive-ifhttp://jsperf.com/try-catch-002结论:在主流浏览器上,几乎没有差异。

javascript - 在使用 react-test-renderer 的 Jest 快照测试中,Refs 为空

目前我正在componentDidMount上手动初始化Quill编辑器,Jest测试对我来说失败了。看起来我得到的ref值在jsdom中是空的。这里有问题:https://github.com/facebook/react/issues/7371但看起来refs应该有效。有什么我应该检查的想法吗?组件:importReact,{Component}from'react';importlogofrom'./logo.svg';import'./App.css';classAppextendsComponent{componentDidMount(){console.log(this._

javascript - onrejected 与 Promise 中的 catch

这个问题在这里已经有了答案:Promise:thenvsthen+catch[duplicate](1个回答)关闭7年前。ES6Promise中的catch和then(_,onRejected)有什么区别?我只知道onRejected不处理内部Promise的拒绝状态。Promise.resolve().then(()=>{returnnewPromise((resolve,reject)=>{thrownewError('Erroroccurs');});},er=>console.log(er));//Chromethrows`Uncaught(inpromise)`Promise

javascript - 查询 : What is the difference between "var test" and "var $test"

这个问题在这里已经有了答案:WhywouldaJavaScriptvariablestartwithadollarsign?[duplicate](16个答案)关闭8年前。这些说法有什么区别?我知道“var$test”声明了一个jquery变量,但是jquery变量与一般的javascript变量有什么区别?