Jest 找不到我的功能并抱怨 TypeError

Jest not finding my function and complains of a TypeError

我创建了一个导出验证函数的 Node.js 应用程序。当我在 Jest 测试中使用验证器时。 Jest 抱怨验证不是函数。该函数已明确导出,当我在 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 抱怨验证不是函数。我只是不知道为什么它会抱怨找不到可以在节点应用程序的其他功能中找到的功能。我在网上搜索但找不到像我这样的用例。由于我是 Jest 的新手,如果能帮我解决这个问题,我将不胜感激。谢谢。

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