- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Jest 的模拟可以处理我没有编写的模块中的函数吗?
node-yelp-api-v3有 Yelp.searchBusiness(String)
但我尝试使用 Jest's mocking functionality不成功。 Jest 示例似乎假设我在模拟项目中的一个模块。从文档中我也不清楚如何模拟模块中的特定功能。
这些都不起作用:
jest.mock('Yelp.searchBusiness', () => {
return jest.fn(() => [{<stubbed_json>}])
})
jest.mock('Yelp', () => {
return jest.fn(() => [{<stubbed_json>}])
})
sinon
但只想使用 Jest。这种 Sinon 方法有效:
var chai = require('chai')
var should = chai.should()
var agent = require('supertest').agent(require('../../app'))
const Yelp = require('node-yelp-api-v3')
var sinon = require('sinon')
var sandbox
describe('router', function(){
beforeEach(function(){
sandbox = sinon.sandbox.create()
stub = sandbox.stub(Yelp.prototype, 'searchBusiness')
})
afterEach(function(){
sandbox.restore()
})
it ('should render index at /', (done) => {
/* this get invokes Yelp.searchBusiness */
agent
.get('/')
.end(function(err, res) {
res.status.should.equal(200)
res.text.should.contain('open_gyro_outline_500.jpeg')
done()
})
})
})
最佳答案
模拟外部模块解释 here .
If the module you are mocking is a Node module (e.g.:
lodash
), the mock should be placed in the__mocks__
directory adjacent tonode_modules
(unless you configured roots to point to a folder other than the project root) and will be automatically mocked. There's no need to explicitly calljest.mock('module_name')
.
__mocks__
带文件
node-yelp-api-v3.js
在里面。在该文件中,您使用
genMockFromModule
从原始模块创建一个模拟对象。并覆盖您要模拟的方法。
// __mocks__/node-yelp-api-v3.js
const yelp = jest.genMockFromModule('node-yelp-api-v3')
function searchBusiness() {
return [{<stubbed_json>}]
}
yelp.searchBusiness = searchBusiness
module.exports = yelp
searchBusiness
在
jest.fn
如果你想调用
searchBusiness.mock.calls.length
之类的断言以后用这个方法。
关于jestjs - 如何使用 Jest 模拟外部模块的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45617362/
有没有办法用 Jestjs 编写更多数据驱动的测试? 我想出了这样的东西: it('assignments and declarations', () => { testCases.f
我想知道如何为 package.json 中的所有 JestJS 测试设置全局超时? 默认的 5000 毫秒对于我的测试来说是不够的。我不想为我的每个测试执行以下代码: it('should retu
我们正在使用 Jest/Enzyme 进行我们的 React 集成测试。我们有一个组件在等待 iframe 加载时显示微调器。当此页面在真实世界的浏览器环境中加载时,微调器会明显显示。但是,在测试 中
Jest 不提供我的手动模拟。一切似乎都在正确的目录中。我错过了什么? src/adapters/__mocks__/Foo.js const spy = jest.genMockFromModule
我对 Jest 有点陌生,所以如果这是一个明显的答案,请原谅我,但在滚动浏览文档后我找不到答案。 我有一个函数 (funcA),它将一个不同长度的数组传递给另一个函数 (funcB),具体取决于 fu
我正在GitLab CI中进行代码覆盖率的玩笑测试,并且GitLab从gitlab中的运行程序的标准输出中捕获了百分比。 jest --coverage在stdout中产生覆盖率,而gitlab使用/
我正在使用Jest运行Selenium测试。我希望登录测试在其余的Webapp功能测试之前进行。我可以使用jest -i依次运行文件,但找不到控制文件运行顺序的方法。我尝试更改文件名,希望它按文件名的
这个和这个不一样enzyme-to-snapshot-render-object-as-json因为 这里我想生成对象的带有JSON定义的快照 另一个我想为组件生成的 only for HTML 生成
我编写了一个脚本,其主要目的是向某些表格的单元格添加新元素。 测试是通过类似这样的方式完成的: document.body.innerHTML = `
我看到有两个配置选项可以在每次测试之前运行一些代码:setupFiles和setupFilesAfterEnv。在我看来,setupFilesAfterEnv提供了更大的灵活性(我可以使用jest,b
我已经找到了一些特定于版本的questions on SO,用于 Jest 单元测试,并将其结果发布在VSTS构建测试结果标签中。但是找不到适当的解决方案。 最佳答案 经过一些研究,我发现Jest t
我想在Jest测试代码中摆脱全局变量。具体来说describe,it和expect: describe('Welcome (Snapshot)', () => { it('Welcome rend
我想测试一个非常简单的JS函数 export function displaySpinner() { const loadingOverlayDOM = document.createElem
该项目具有客户端应用程序和服务器站点代码,一旦项目在本地构建,它将把所有构建的前端和后端 Assets 放入 build目录,它包含客户端需要使用的所有静态 Assets (所有测试用例也包括在那里)
我了解Jest是用于JavaScript的开发人员的单元测试工具。 Jest是类似于Selenium的基于浏览器的测试工具还是功能测试工具? 最佳答案 该npm库可以与您的jest测试集成在一起,以在
我发现“笑话快照摘要”有点令人困惑。在我们的一个存储库中运行测试后,我得到以下摘要: Snapshot Summary › 2 snapshots written in 1 test suite.
我对 Jest 用于运行测试的顺序有一些疑问。 假设我有两个测试: module1.spec.ts module2.spec.ts 这些模块中的每一个都使用一个公共(public)数据库。在每个文件的
我想为我的代码库中的所有文件运行覆盖率报告,包括那些目前没有任何测试的文件。 我正在使用这个命令: jest --coverage --collectCoverageFrom='src/feature
我想在 jest 中断言数组包含具有某些属性的对象,例如: [ { id: 1, name: 'A' }, { id: 2, name: 'B' }, { id: 3 }
我正在开发一个文本生成器,我想将生成的字符串与存储在示例文件中的文本进行比较。文件某些行有缩进,在 TS/js 中构造这些字符串非常麻烦 是否有一种简单的方法可以从相对于当前测试甚至 Jest 项目根
我是一名优秀的程序员,十分优秀!