草庐IT

babel-jest

全部标签

javascript - 使用 Babel.js 将 ES6 箭头函数编译为 Es5

在Mozilla文档中查看ES6箭头函数的文档时,我了解到箭头函数应用严格模式的所有规则,除了在link中描述的规则。varf=()=>{'usestrict';returnthis};varg=function(){'usestrict';returnthis;}console.log(f());//printsWindowconsole.log(g());//printsundefined//wecantestthisinfirefox!但是,Babel.js将箭头函数代码转换为ES5代码,返回undefined而不是Window(demolink)"usestrict";setT

javascript - "export default from"不适用于 Babel React

我使用react来构建我的组件库。我需要一个index.js来将所有组件导入一个地方。像这样:MyComponents/Button.jsLabel.jsindex.js在index.js中,我接下来尝试做的是://thisexportnothingexport{default}from'./Button';//thistellsmeaboutsyntaxerrorexportdefaultfrom'./Button';我发现只有这个解决方案有效importButtonfrom'./Button';exportdefaultButton;但我发现一些React组件库使用我上面提到的语法

javascript - webpack.config.babel.js中使用 'import'报错

(function(exports,require,module,__filename,__dirname){importpathfrom'path'^^^^^^SyntaxError:Unexpectedtokenimport当我使用webpack-dev-server--hot时出现此错误。出现这种情况好像是因为它无法读取import或者webpack不支持import。我尝试使用babel-register但它不起作用。有什么办法可以解决这个问题吗?请引用下面的代码。webpack.config.babel.jsimportpathfrom'path'importwebpackf

javascript - Babel ES6 导入错误,SyntaxError : Unexpected token import

我正在尝试设置一个基本的模块化程序,但我似乎遇到了导入模块的问题。我尝试导入我的自定义模块,但出现以下错误:(function(exports,require,module,__filename,__dirname){importtestStepfrom'testStep';^^^^^^SyntaxError:Unexpectedtokenimport导致问题的代码:测试用例.jsimporttestStepfrom'testStep';testStep.hello();测试步骤.jsvartestStep={hello:hello,};varhello=()=>{console.lo

javascript - 使用 Jest 和 enzyme 进行 IndexedDB 测试 - ReferenceError : indexedDB is not defined

我正在为我的应用程序寻求单元测试方面的帮助,我正在使用indexedDB。在我实现indexedDB功能之前,测试是正确的。但是现在,对于所有这些,我都看到一个错误:ReferenceError:indexedDBisnotdefined有人可以给我建议如何消除该错误吗?我正在搜索信息,并尝试了不同的方法来模拟window或indexedDB,但没有结果。 最佳答案 这个问题是由于Dexie期望window.indexedDB被定义,当以没有真正的DOM或的headless模式(使用Jest)运行时,情况并非如此窗口范围。在Dexi

javascript - 使用 Jest 异步设置环境

在Jest中运行端到端测试之前,我需要从服务器获取身份验证token。是否可以在全局范围内执行此操作并以某种方式将其设置为每个测试的全局环境/上下文?我尝试使用globalSetup配置选项:constauth=require('./src/auth')constctx=require('./src/context')module.exports=()=>{returnnewPromise(res=>{auth.getToken().then(token=>{ctx.init({token})global.token=tokenres()})})}context.jslet_token

javascript - 如何验证 Jest 和 Enzyme 中的 React Prop ?

所以我尝试学习React中的测试,我有这个:Button.js和Button.test.js问题连同下面的代码一起评论://Button.jsimportReactfrom'react';import{string,bool,func}from'prop-types';import{StyledButton}from'./styled'constButton=({size,text,})=>({text}//butthetextpropsishere.Thatismycurrentpracticeofpassingthepropstothechildren,amImissinganyt

javascript - 从 7 升级到 Babel-loader 8?我需要改变什么?

我正在尝试从版本7升级到版本8。但我遇到了一些错误。我想我需要升级一些东西但不确定是什么这就是我的包(我删除了与我的问题无关的插件)"dependencies":{"babel-plugin-emotion":"^9.2.5","babel-polyfill":"^6.26.0","http-proxy-middleware":"^0.18.0","koa-connect":"^2.0.1","koa-router":"^7.4.0","koa2-connect-history-api-fallback":"0.0.6","npm":"^6.1.0","react":"^16.4.0"

javascript - 如何 Jest 模拟 react-native-camera?

我正在尝试测试react-native-camera模块与jest所以我有以下package.json:{"name":"app","version":"0.0.1","private":true,"main":"index.js","license":"MIT","scripts":{"start":"nodenode_modules/react-native/local-cli/cli.jsstart","test":"jest"},"dependencies":{"react":"~15.4.0-rc.4","react-native":"0.40.0","react-nativ

javascript - 为什么我的 jest.mock 中的 Promise reject() 会转到 then() 而不是 catch()?

我有两个文件,getItemInfo.js进行API调用,getItemInfo.test.js是相应的Jest测试文件。在测试文件中,我正在模拟由Node模块request-promise触发的http调用。问题在第二个代码块上,被*********包围。基本上为什么reject()错误仍然会在第二个单元测试中进入then()block?//getItemInfo.jsconstrp=require('request-promise');constgetItemInfo=(id)=>{constroot='https://jsonplaceholder.typicode.com/po