jquery - jest.js 中的 loadFixtures 方法

标签 jquery jestjs

jest 中是否有与 loadFixtures 方法等效的方法。我似乎在 Jest 文档中找不到任何内容。我希望能够将 html 固定装置加载到 jest 测试文件中?

或者有没有另一种方法可对于Jest地做 html stub 我错过了?

请注意,我没有将 React 用作​​ Jquery 的旧项目。

所以,而不是写一些类似的东西。

window.$ = require('jquery');

beforeEach(() => {


    document.body.innerHTML =
        '<div>' +
        ' <input id="exMonth" value="02" />' +
        ' <input id="exYear" value="2017" />'
        '</div>';

});


test("exMonth should be 02", () =>{

    expect($('#exMonth').val()).toBe('01');

});

我想将我的 html 抽象为一个 html 固定文件并要求它
document.body.innerHTML = require(myHtmlFixture.html)

最佳答案

您可以使用节点的 fs用于服务器端测试。

var fs = require('fs');

var htmlFixture = fs.readFileSync('spec/fixtures/myFixture.html')

document.body.innerHTML = htmlFixture;
见:https://nodejs.dev/learn/reading-files-with-nodejs

关于jquery - jest.js 中的 loadFixtures 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44548730/

相关文章:

dynamic - 如何使用 jQuery 将单击事件 Hook 到动态创建的 anchor 标记

php - 输入键以保存文本区域值

javascript - 使用 Jest 无法从 'ReactNative' 找到模块 'react-native.js'

jestjs - 带有 jest.config.js 的 Jest cli testPathIgnorePatterns 结果未找到测试

javascript - 慢慢停止随机化

javascript - 选择它们时从选择中删除选项并将值添加到 div。当点击div时,将选项返回给select

jQuery ui 选项卡锚定,如何防止?

unit-testing - 我如何 Jest 模拟react-native-ibeacon

node.js - 在 CircleCI : "npm test died unexpectedly" 上运行 Jest 时出错

unit-testing - Enzyme Mock ToastAndroid React Native 组件