在macOS上运行dockermhart/alpine-node:8nodejs(6.10.3-r0)(18/18)yarn0.24.6jest20.0.4但是,在运行代码时,我有一个__tests__/index.test.js文件node_modules/.bin/jest--watchAll我得到以下输出NotestsfoundIn/usr/src/app5fileschecked.testMatch:/__tests__//*.js?(x),**/?(*.)(spec|test).js?(x)-1matchtestPathIgnorePatterns:/node_modules
我正在尝试这个:expect(AP.require).toBeCalledWith('messages',()=>{})其中AP.require是一个模拟函数,它应该接收一个字符串和一个函数作为第二个参数。测试失败并显示消息:Expectedmockfunctiontohavebeencalledwith:[Functionanonymous]asargument2,butitwascalledwith[Functionanonymous] 最佳答案 要断言任何函数,您可以使用expect.any(constructor):所以你的
jest.resetModules()清除所有模块的require缓存,但有没有办法只为单个模块清除它?无法使用require.cache,因为Jest似乎绕过了它。我正在测试一个有状态的Node模块(即它依赖于对require的多次调用返回相同实例的事实)。对于我的测试,我需要重置模块的状态以测试不同的场景。jest.resetModules()有效,但我需要重新require其他一些不需要重置的模拟模块。 最佳答案 正如问题中所解释的,jest.resetModules()重置模块缓存,这对您的模块保持某些本地状态并且您希望在测
有没有办法用jest@20测试匿名函数的相等性?我正在尝试通过类似于以下内容的测试:constfoo=i=>j=>{returni*j}constbar=()=>{baz:foo(2),boz:1}describe('Testanonymousfunctionequality',()=>{it('+++foo',()=>{constobj=foo(2)expect(obj).toBe(foo(2))});it('+++bar',()=>{constobj=bar()expect(obj).toEqual({baz:foo(2),boz:1})});});目前产生:●>>>Testano
我正在使用Jest框架并有一个测试套件。我想关闭/跳过我的一项测试。谷歌搜索文档没有给我答案。您知道要检查的答案或信息来源吗? 最佳答案 我在这里找到了答案https://devhints.io/jesttest('itisraining',()=>{expect(inchesOfRain()).toBeGreaterThan(0);});test.skip('itisnotsnowing',()=>{expect(inchesOfSnow()).toBe(0);});Linkonoffdoc
当我想使用命令npmruntest运行我的项目时,我收到以下错误。这是什么原因造成的?FAIL●TestsuitefailedtorunSecurityError:localStorageisnotavailableforopaqueoriginsatWindow.getlocalStorage[aslocalStorage](node_modules/jsdom/lib/jsdom/browser/Window.js:257:15)atArray.forEach() 最佳答案 如果您使用http://localhost前缀访问您的
我可以使用Jest测试多个文件,但我不知道如何测试单个文件。我有:运行npminstalljest-cli--save-dev更新了package.json:`{..."scripts":{"test":"jest"}...}写了许多测试。运行npmtest按预期工作(目前它运行14个测试)。如何测试单个文件,例如测试app/foo/__tests__/bar.spec.js?我已尝试运行npmtestapp/foo/__tests__/bar.spec.js(从项目根目录),但出现以下错误:npmERR!Error:ENOENT,open'/node_modules/app/foo/
我正在尝试使用jest模拟Mongoose模型,但出现Cannotcreateproperty'constructor'onnumber'1'错误。我能够通过使用下面显示的2个文件创建项目来重现该问题。有没有办法用jest模拟Mongoose模型?./model.jsconstmongoose=require('mongoose')constSchema=mongoose.Schemaconstschema=newSchema({name:String})module.exports=mongoose.model('Test',schema)./model.test.jsjest.mo
我正在尝试使用jest模拟Mongoose模型,但出现Cannotcreateproperty'constructor'onnumber'1'错误。我能够通过使用下面显示的2个文件创建项目来重现该问题。有没有办法用jest模拟Mongoose模型?./model.jsconstmongoose=require('mongoose')constSchema=mongoose.Schemaconstschema=newSchema({name:String})module.exports=mongoose.model('Test',schema)./model.test.jsjest.mo
我想用jest和mongoose编写一些单元测试来验证与mongo的数据交互。我不想在这里模拟mongoose,因为我特别想验证mongo文档的创建/修改/处理方式。package.json配置为不模拟Node模块:{"jest":{"unmockedModulePathPatterns":["node_modules"]}}在我的实际测试中,我设置了一个beforeAll()钩子(Hook)来负责连接到mongo:constmongoose=require('mongoose');describe('MyTest',()=>{beforeAll((done)=>{mongoose.c