草庐IT

javascript - jest mockgoose - jest 在测试运行完成后一秒钟没有退出

我有一个Mongoose模型:varmongoose=require("mongoose");vartransactionSchema=mongoose.Schema({category:{type:String,required:[true,"Categoryisrequired."]},amount:Number,comment:String,tags:Array,currency:String});varTransaction=mongoose.model("Transaction",transactionSchema);module.exports=Transaction;以及

javascript - 禁用 Jest setTimeout 模拟

我正在为依赖于websocket库的代码编写Jest测试。websocket库被模拟。我想发送一条消息,等待异步操作完成,然后检查响应。it('sendsamessageandgetsaresponse',()=>{processor(ws).sendMessage()//doabunchofasyncstuff,callwebsocket.sendMessage()setTimeout(()=>{expect(ws.getResponse()).toEqual('alldone')},100)})不幸的是,因为Jest模拟了setTimeout,所以setTimeout失败了。如果我

javascript - 使用 Jest 模拟基于 promise 的请求

我正在尝试使用Jest对函数进行单元测试,但我在处理jest模拟模块时遇到了一些麻烦(相当于nodejs世界中的rewire或proxyquire)。我实际上是在尝试测试是否已使用一些参数在模拟模块上调用了spy程序。这是我要测试的功能。注意:当前测试只涉及“fetch(...)”部分,我正在尝试测试fetch是否已使用good参数调用。exportconstfetchRemote=slug=>{returndispatch=>{dispatch(loading());returnfetch(Constants.URL+slug).then(res=>res.json()).then(

javascript - Jest 中的动态导入产生 : SyntaxError: Unexpected token import

还不知道如何解决这些错误,Jest提示像这样的动态导入:constimportModules=Promise.all([import('containers/HomePage/reducer'),import('containers/HomePage/sagas'),import('containers/HomePage'),]);错误信息:F:\react-boilerplate\app\store.js:49import('./reducers').then(function(reducerModule){^^^^^^SyntaxError:Unexpectedtokenimpor

javascript - JEST 错误 TypeError : specificMockImpl. apply is not a function

尝试使用来自api的回调来模拟其中一个函数并得到错误TypeError:specificMockImpl.applyisnotafunctionimport{IEnvironmentMap,load}from'dotenv-extended';import{getTokensWithAuthCode,sdk}from'../src/connection-manager';describe('getTokensWithAuthCodefunctionTests',()=>{jest.useFakeTimers();letboxConfig:IEnvironmentMap;beforeAl

javascript - React-Redux/Jest 不变违规 : Could not find "store"

我设置了一个简单的测试文件,几乎与create-react-app使用的文件相同:App.test.jsimportReactfrom'react';importReactDOMfrom'react-dom';import{App}from'./App';it('renderswithoutcrashing',()=>{constdiv=document.createElement('div');ReactDOM.render(,div);});当我运行yarntest我不断收到此错误消息:InvariantViolation:Couldnotfind"store"ineitherth

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 - 如何 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