我是新来的玩笑,目前只是为现有功能进行了一些测试。我有一个函数,该函数(从JSON文件中(获取一系列数据并将其映射到特定位置,绘制点。
这是函数绘图点:
function plotPoints(data) {
console.log(data.subset[0]);
const sub0 = data.subset[0];
...
Plotpoints将JSON数据作为参数。该函数之所以起作用,是因为它可以正确记录控制台中的数据,而其他功能则按预期工作。
但是测试总是失败:
plotpoints.test.js
import plotPoints from '../functions/plotPoints';
const dataSrc = require('../../public/data/jsonfile.json');
test('it finds the plot data', () => {
expect(plotPoints(dataSrc)).toBeDefined();
});
返回此错误:
it finds the plot data
TypeError: Cannot read property '0' of undefined
3 | // takes raw JSON data and builds a big object with matching joint pairs
4 | function plotPounts(data) {
> 5 | const sub0 = data.subset[0];
| ^
6 |
at plotPoints (src/functions/plotPoints.js:5:16)
at Object.<anonymous>.test (src/tests/plotPoints.test.js:5:10)
我以为该功能也许在JSONFILE之前在测试中运行。
test('it finds the correct joints', done => {
function callback(input) {
expect(input).toBeDefined();
done();
}
mapJoints(callback(dataSrc));
});
,但这没什么区别。是否有任何理由为什么该功能在data.subset[0]
数组中毫无困难而不是在测试中找到?
您是否检查了dataSrc
是否定义?
尝试用:
替换要求import dataSrc from '../../public/data/jsonfile.json'