问题标签 [jestjs]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
javascript - 带有 jest.setMock() 的 TypeError
我想知道为什么当我明确地为玩笑设置一个模拟时会出现类型错误。
我这样做:
我明白了。
mocks/LoginStatus.jsx 就是这样:
这是 Jest 文档:https ://facebook.github.io/jest/docs/api.html#jest-setmock-modulename-moduleexports
javascript - 使用 jsdom 和 Jest
我正在尝试使用 Jest 对 Angular 应用程序进行单元测试。为了测试角度绑定,我尝试使用 jsdom(注意,我使用的是 v3.1.2,因为我使用的是节点而不是 IO)。当我需要使用 html 加载脚本时,测试似乎在加载脚本之前完成。
我已经简化了我的测试用例以使用来自 jsdom 的示例:
如果我运行此测试,测试将通过,但不会打印“已完成”日志消息,我也可以将期望替换为明显失败的内容,例如 expect(false).toBeTruthy() 并且测试仍将“经过”。如果我删除 jquery 的注入,那么一切都会按预期工作。
我应该如何确保在退出测试之前加载脚本?更一般地说,在 Jest 中显式使用 jsdom 感觉有点不对劲。有没有更好的办法?
javascript - 开玩笑不自动模拟常见的 js 模块
我正在尝试使用 Jest 测试 Flux 和 React 应用程序。
我开始使用Scotch.io 教程作为我的起点,并将创建一个完整的测试套件,以获取有关如何构建我的第一个真正的 react 和 Flux 应用程序的知识。
你可以在 github 上找到我的代码,看看我在哪里。
没有运行的测试是FluxProduct-test.jsx
它在第 100 行返回“TypeError:无法读取未定义的属性‘调用’”。
当我注销 FluxActions 时,它似乎没有被自动模拟,这就是模拟未定义并且访问调用属性引发错误的原因。
仅供参考:Jest 需要节点 0.10,不在 0.12 上运行
有用的参考文件:
这是我的 package.json 文件:
npm test
我通过在 app 目录中运行来运行测试。
reactjs - 如何在 webpack 中使用 jest?
我使用webpack开发一个 React 组件。这是它的一个简单版本:
现在,我想使用jest来测试这个组件。这是我的相关内容package.json
:
运行时npm test
,我收到以下错误:
语法错误:/Users/mishamoroshko/react-component/src/tests/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent.js:/Users/mishamoroshko/react-component/src/MyComponent.less:意外令牌非法
看起来 webpack 需要require('./MyComponent.less')
在 jest 运行测试之前进行处理。
我想知道我是否需要使用类似jest-webpack 的东西。如果是,有没有办法指定多个scriptPreprocessor
s?(注意我已经用过babel-jest
)
javascript - 尝试使用 jasmine-pit 进行测试时开玩笑超时
我正在尝试使用 Jest 对 Angular UI 运行一个小测试,但我在 5 秒后超时。这是我的代码:
我正在使用pit,所以我可以有1秒的延迟以允许角度更新视图,但似乎setTimeout中的匿名函数永远不会被调用。承诺没有兑现,测试超时。由于 Jest 使用 Jasmine 1.3.0,我也尝试过使用 runs() 和 waitsFor() 但我得到了相同的结果。
node.js - 使用 require() 时 IO.js 1.4.3 中的 Jest (JS) 分段错误 11
我正在使用 IO.js 1.4.3 和 Jest 来运行我的测试套件。我需要更新版本的 V8,因为我正在使用诸如 Promises 之类的 ES6 功能。
如果我尝试最简单的测试:
它有效(谢天谢地)。但是,如果我包括一个调用require()
(我必须这样做来测试我自己的代码),我会得到:
无论我包含哪个模块,以及我是否jest.dontMock()
首先调用,都会发生这种情况。
这并没有真正告诉我很多错误消息,而且require
确实似乎是问题所在。除了恢复到 1.0 之前的 node.js 之外,还有其他解决方案吗?
reactjs - How to test HTML of a rendered React component?
My React component has a suggestionRenderer
property that allows to specify how the component is rendered. For example:
Now, I'd like to write a jest test to make sure that suggestionRenderer
does its job. Inspecting myElement.getDOMNode().innerHTML
reveals:
which is not particularly useful.
Is there a way to get a clean HTML, without React attributes?
reactjs - calling setState on a reactjs component with test utils not re-rendering component
I have the following code in a jest based test:
My previous test tests the initial state of the component. I now want to call setState to check the component after a state change. The videoClass i'm logging here stays the same. I am on react 0.12. and latest jest 0.4.0.
Any ideas on how to test what happens after set state?
reactjs - Jest AutoMocking 在测试 React 组件时是否有效?
我有限数量的测试似乎表明答案是否定的。我正在为依赖于商店的父级 React 组件(又名控制器视图)编写单元测试。然而,Jest 并没有像文档建议的那样为商店提供自动模拟,而是调用真正的实现。
这是一个错误还是设计使然?如果是后者,那么单元测试反应组件是否不可取?
编辑 1
在测试 CommonJs 模块时,Automocking 工作得很好;它只是不适用于反应组件。
jasmine - 使用 Jest 的意外保留字错误
我正在尝试将 Jest 测试添加到我的项目中(使用 React、Browserify 和 Babel),但是在执行最基本的操作时出现错误:
使用这种结构:
而这段代码:
供应商.js:
供应商测试:
这是我得到的错误:
实际上,只要我需要一个模块,就会发生错误。有任何想法吗?