测试Jest中的函数-

epfja78i  于 2023-03-06  发布在  Jest
关注(0)|答案(2)|浏览(187)

console.log输出了正确的字符串,但是Jest总是失败。我错过了什么吗?
这是一个测试:

const filterDesignationNum = require('./function on a specific NEO.js');

test('Filter NEOs by designation number', () => {
  expect(filterDesignationNum).toMatch('Designation: 419880 (2011 AH37)')
});

下面是我要调用的函数:

//Analysis Display all NEO's that have a specific NEO

const fs = require('fs');

const neowiseData = fs.readFileSync("C:\\Users\\adamb\\Downloads\\NEOWISE_Dataset.json", 'utf8');

const neowiseDataObj = JSON.parse(neowiseData);

function filterDesignationNum(designationNum) {
    const displayNEOData = neowiseDataObj.filter(neo => {
        return neo.designation.includes(designationNum);
    });
    displayNEOData.forEach((neo) => {
        console.log(`Designation: ${neo.designation}`);
        console.log(`Discovery date: ${neo.discovery_date}`);
        console.log(`Observed Magnitude: ${neo.h_mag}`);
        console.log(`Minimum Orbit Intersection Distance: ${neo.moid_au}`);
        console.log(`Perihelion Distance: ${neo.q_au_1}`);
        console.log(`Aphelion Distance: ${neo.q_au_2}`);
        console.log(`Orbital Period: ${neo.period_yr}`);
        console.log(`Orbital Inclinatio: ${neo.i_deg}`);
        console.log(`Hazardous: ${neo.pha}`);
        console.log(`Asteroid or Comet Class: ${neo.orbit_class}`)
        console.log('------------------------');
    });
}
filterDesignationNum(419880)

// Export the function as a module.
module.exports = filterDesignationNum;
yqkkidmi

yqkkidmi1#

这条线

expect(filterDesignationNum).toMatch('Designation: 419880 (2011 AH37)')

将函数(对象)与错误的字符串进行比较,如果调用该函数:

expect(filterDesignationNum(419880)).toMatch('Designation: 419880 (2011 AH37)')

如果你没有从filterDesignationNum返回任何东西,那么它会再次失败。你可以让这个函数返回,这样你的代码就更容易测试了:

function filterDesignationNum(designationNum) {
    const displayNEOData = neowiseDataObj.filter(neo => {
        return neo.designation.includes(designationNum);
    });
    return displayNEOData.reduce((acc, neo) => {
        return `${acc}
        Designation: ${neo.designation}
        Discovery date: ${neo.discovery_date}
        Observed Magnitude: ${neo.h_mag}
        Minimum Orbit Intersection Distance: ${neo.moid_au}
        Perihelion Distance: ${neo.q_au_1}
        Aphelion Distance: ${neo.q_au_2}
        Orbital Period: ${neo.period_yr}
        Orbital Inclinatio: ${neo.i_deg}
        Hazardous: ${neo.pha}
        Asteroid or Comet Class: ${neo.orbit_class}
        ------------------------\n`
    }, '');
}

console.log(filterDesignationNum(419880));

然后,在测试中,您可以检查返回值的内容:

expect(filterDesignationNum(419880)).toMatch('Designation: 419880 (2011 AH37)');

或者,如果出于某种原因,您希望检查值是否打印到控制台,您可以spyOnconsole对象,并期望console.log已被包含您的输入片段的字符串调用:

const consoleLogSpy = jest.spyOn(console, 'log');
expect(console.log).toHaveBeenCalledWith(
  expect.stringContaining("Designation: 419880 (2011 AH37)")
);
ru9i0ody

ru9i0ody2#

你的函数没有返回任何东西,你可以修改你的代码,像这样返回字符串:

function filterDesignationNum(designationNum) {
    const displayNEOData = neowiseDataObj.filter(neo => {
        return neo.designation.includes(designationNum);
    });

    // create an array to store the output strings
    const output = [];

    displayNEOData.forEach((neo) => {
        output.push(`Designation: ${neo.designation}`);
        output.push(`Discovery date: ${neo.discovery_date}`);
        output.push(`Observed Magnitude: ${neo.h_mag}`);
        output.push(`Minimum Orbit Intersection Distance: ${neo.moid_au}`);
        output.push(`Perihelion Distance: ${neo.q_au_1}`);
        output.push(`Aphelion Distance: ${neo.q_au_2}`);
        output.push(`Orbital Period: ${neo.period_yr}`);
        output.push(`Orbital Inclinatio: ${neo.i_deg}`);
        output.push(`Hazardous: ${neo.pha}`);
        output.push(`Asteroid or Comet Class: ${neo.orbit_class}`)
        output.push('------------------------');
    });

    // join the array into a single string and return it
    return output.join('\n');
}

不要忘记更新您的jest测试来检查函数:

const filterDesignationNum = require('./function on a specific NEO.js');

test('Filter NEOs by designation number', () => {
  expect(filterDesignationNum(419880)).toMatch(`Designation: 419880 (2011 AH37)\nDiscovery date: ...\n`);
});

相关问题