【问题标题】:Jest not finding my function and complains of a TypeError开玩笑找不到我的功能并抱怨 TypeError
【发布时间】:2022-02-04 21:12:33
【问题描述】:

我创建了一个导出验证函数的 Node.js 应用程序。当我在 Jest 测试中使用验证器时。 Jest 抱怨 validate 不是一个函数。该功能已明确导出,当我在 VS Code 中单击它时,它会直接转到该功能。这是在名为 validator.js 的文件中导出的验证函数:

          // validator.js
             module.exports = {  
               validate: (req) => { 
               let policyType = req.body.policy_type;
               let length = req.body.term_length;
               
               if (!policyType) {
                return false;
               }
                if (!length) {
                 return false;
                }
                return  true;
               }
              };

然后我在 Jest 测试中拉入函数如下:

        const validate = require("./validator.js");
        describe(“Validator Test", ()=> {
        test('Ensure expected return ', ()=>{
        const req = {};
        req.body.policy_type= '';
        req.body.term_length= '';
    
        //TypeError: validate is not a function
         expect(validate(req)).toEqual(expectedOut);
        
       })

Jest 抱怨 validate 不是一个函数。我根本不知道为什么它抱怨找不到可以在节点应用程序的其他功能中找到的功能。我在网上搜索但找不到像我这样的用例。由于我对 Jest 很陌生,因此我将不胜感激解决此问题的任何帮助。谢谢。

【问题讨论】:

  • 对不起,我没有更新帖子。我解决了。 module.exports 很好。该错误是由于未找到验证器代码中使用的另一个导出。一旦我修复了它,它就能够找到函数
  • 感谢 dariosicily 对此进行调查,非常感谢!
  • 不客气 :-)

标签: javascript unit-testing jestjs


【解决方案1】:

module.exports 工作正常。该问题与验证器中未解决的另一个导入有关。

【讨论】:

    猜你喜欢
    • 2020-04-26
    • 2020-12-21
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2018-09-25
    • 1970-01-01
    • 2018-10-12
    相关资源
    最近更新 更多