【问题标题】:Ignore an error with Jest忽略 Jest 的错误
【发布时间】:2017-03-15 17:00:26
【问题描述】:

有没有办法忽略 Jest 的错误?

我在导入包时收到错误消息,我想忽略该错误,以便测试我的其余代码。

【问题讨论】:

    标签: react-native jestjs


    【解决方案1】:

    您应该将 transformIgnorePatterns 添加到您的 package.json:

    "jest": {
      "preset": "react-native",
      "transformIgnorePatterns": [
        "/node_modules/(?!react-native|*put library here*)"
      ]
     },
    

    【讨论】:

    • 不幸的是,它并没有忽略我的错误。仍然感谢您的回答。
    • TypeError: Cannot read property 'style' of undefined at Object.<anonymous> (node_modules/react-native-snap-carousel/index.js:472:842) 运行应用程序时不会弹出此错误
    • 你将什么传递给 renderer.create()?
    • <Provider store={store}> <Guides {...props} /> </Provider>和我的组件Guides导入包
    • 我在使用不同的第 3 方组件时遇到了同样的问题。但是它只抛出(控制台)警告,而不是错误,因此测试通过。也许您应该在react-native-snap-carousel 提出问题?
    【解决方案2】:

    在你的测试文件中添加这个玩笑testdescribe

    console.error = jest.fn()

    基本上它会让 jest 模拟 console.error 函数,所以它会通过测试。

    【讨论】:

    • 它不起作用。似乎 jest 处理错误并在不使用 console.error 的情况下打印它
    【解决方案3】:

    在我的 jestsetup.js 中,请添加

    console.error = message => {
    //    throw new Error(message);
     };
    

    我的 jestsetup.js 文件

    // Make Enzyme functions available in all test files without importing
    import { shallow, render, mount } from 'enzyme';
    global.shallow = shallow;
    global.render = render;
    global.mount = mount;
    // Fail tests on any warning
     console.error = message => {
    //    throw new Error(message);
     };
    

    【讨论】:

      【解决方案4】:

      只需使用test.skipit.skipxitxtest 而不是testit 即可跳过特定测试。看看docs

      【讨论】:

      • 这个解决方案的问题是我仍然想运行测试,因为它测试了我的组件。但它们在我的组件中导入时出错。
      猜你喜欢
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 2015-12-29
      相关资源
      最近更新 更多