简体   繁体   English

开玩笑:无法读取未定义的属性(读取“0”)

[英]Jest: Cannot read properties of undefined (reading '0')

I'm newbie with jest and I have made a test to check several calls to datababase from an url.我是开玩笑的新手,我已经进行了测试以检查从 url 对数据库的多次调用。

I run the test with this command:我使用以下命令运行测试:

npx jest --watchAll file.test.js

And everything works fine.一切正常。 But, If I modify a comma, for example, the test is running again and I've got errors.但是,如果我修改逗号,例如,测试会再次运行并且我有错误。 I haven't any "OK" from the api and I've got this error too:我没有来自 api 的任何“OK”,我也遇到了这个错误:

TypeError: Cannot read properties of undefined (reading '0')

When I try to check what I have received with what I have expected, like we can see in this line:当我尝试检查收到的内容是否符合我的预期时,就像我们在这一行中看到的那样:

expect(1).toBe(result.data[0].id);

If I stop the test and run again with: npx jest --watchAl file.test.js everything works fine.如果我停止测试并再次运行: npx jest --watchAl file.test.js 一切正常。

What happend?发生了什么事? Why Have I got these errors when the test is running for twice?为什么测试运行两次时出现这些错误?

The code of my test is:我的测试代码是:

import config from "../config";
import {getData} from "../com/functions";

describe("Test several connections with Postgress making a request via POST", () => {
    describe("Test read data from database", () => {
        test("Test 1: Read data from tbl001_users", async () => {
            let data = {
                "query": 1,
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            console.log(result);
            expect(1).toBe(result.data[0].id);
            expect("OVT").toBe(result.data[0].code);
            expect(2).toBe(result.data[1].id);
            expect("TTHOT").toBe(result.data[1].code);
        });
        test("Test 2: Read data from tbl001_users", async () => {
            let data = {
                "query": 1,
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            expect(1).toBe(result.data[0].id);
            expect("OVT").toBe(result.data[0].code);
            expect(2).toBe(result.data[1].id);
            expect("TTHOT").toBe(result.data[1].code);
        });
        test("Test 3: Read data from tbl001_users", async () => {
            let data = {
                "query": 1,
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            expect(1).toBe(result.data[0].id);
            expect("OVT").toBe(result.data[0].code);
            expect(2).toBe(result.data[1].id);
            expect("TTHOT").toBe(result.data[1].code);
        });
        test("Test 4: Read data from tbl002_suppliers", async () => {
            let data = {
                "query": 2,
                "params": ["OVT"],
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            expect("OK").toBe(result.result);
            expect(1).toBe(result.data.length);
        });
        test("Test 5: Read data from tbl002_suppliers", async () => {
            let data = {
                "query": 2,
                "params": ["OVT"],
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            expect("OK").toBe(result.result);
            expect(1).toBe(result.data.length);
        });
        test("Test 6: Read data from tbl002_suppliers", async () => {
            let data = {
                "query": 2,
                "params": ["OVT"],
                "origin": "Mapping-Service"
            };
    
            let result = await getData(`${config.backend.protocol}://${config.backend.domain}:${config.backend.port}/api/youmalou`, data);
            expect("OK").toBe(result.result);
            expect(1).toBe(result.data.length);
        });
    });
});

The file where I dispatch the requests has this code:我发送请求的文件有以下代码:

import express from "express";
import {getData} from "../com/functions";
import config from "../config";
import cors from "cors";
import {YoumalouDDBB} from "../database/Youmalou/youmalou_ddbb";


const router = express.Router();

const ddbb = new YoumalouDDBB();
async function start(){
    await ddbb.connect();
}

start();

let url = null;
let urls = null;

if ((config.node_env === "test") || (config.node_env === "development") ||  (config.node_env === "production")){
    url = `${config.frontend.protocol}://${config.frontend.domain}:${config.frontend.port}`;
    urls = `${config.frontend.protocols}://${config.frontend.domain}:${config.frontend.port}`;
}

const corsOptions = {
    origin: [url, urls],
    optionSuccessStatus: 200,
    methods: ["POST", "OPTIONS"],
};

if ((config.node_env === "test") || (config.node_env === "development") || (config.node_env === "production")){
    router.all("*", cors(corsOptions));
}

router.post("/api/youmalou", async(req, res)=> {
    let result = null;
    

    if (typeof(req.body.query) === "number"){
        console.log(req.body);
        result = await ddbb.getDataFromDDBB(req.body.query, req.body.params);
    }

    res.json(result.data);
});

module.exports.Router = router;

I have found the problem.,.我发现了问题。, I have installed nodemon module too.我也安装了 nodemon 模块。 So, each time I modify my code the server is restarted.所以,每次我修改我的代码时,服务器都会重新启动。

What happend?发生了什么事? When I re running my test the server is not up at 100% and for this reason I've got an error in my requests.当我重新运行测试时,服务器没有达到 100%,因此我的请求中有错误。

The code is correct and everything works fine when the server is up previously.代码是正确的,并且在服务器之前启动时一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest Vuejs 测试 - 类型错误:无法读取未定义的属性(读取“参数”) - Jest Vuejs Test - TypeError: Cannot read properties of undefined (reading 'params') TypeError:无法读取未定义(读取“长度”)JEST 测试的属性 - TypeError: Cannot read properties of undefined (reading 'length') JEST test 笑话:- TypeError:无法读取未定义的属性(读取“params”)。 开玩笑时出错 - Jest:- TypeError: Cannot read properties of undefined (reading 'params'). Error coming in jest 无法读取未定义的属性(读取 'then') - Cannot read properties of undefined (reading 'then') 无法读取未定义的属性(读取“4”) - Cannot read properties of undefined (reading '4') 无法读取未定义的属性(读取“______”) - Cannot read properties of undefined (reading '______') 无法读取未定义的属性(读取 *) - Cannot read properties of undefined (reading *) 无法读取未定义的属性(读取“打开”) - Cannot read properties of undefined (reading 'on') TypeError: Cannot read properties of undefined (reading 'then') - while Mocking Fetch call in Jest - TypeError: Cannot read properties of undefined (reading 'then') - while Mocking Fetch call in Jest Jest 导入模块,TypeError:无法读取未定义的属性(读取“utf16le”) - Jest import module, TypeError: Cannot read properties of undefined (reading 'utf16le')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM