使用Mocha测试Promise时出错'TypeError: Cannot read property '

Error in testing Promise with Mocha - 'TypeError: Cannot read property 'resolve' of undefined'

本文关键字:TypeError Cannot read property Mocha Promise 出错 使用 测试      更新时间:2023-09-26

我一直得到错误'TypeError:无法读取属性'resolve'的未定义',我不知道为什么会发生。任何想法吗?

describe('render fn', () => {
    it('should return a string', () => {
        let filename = __filename, 
            content = 'Hello, World', 
            theme = './test.less';
        return file.render(filename, content, theme)
            .then((css) => {
                expect(css).to.be.a('string');
        });
    });
});

下面是我要测试的代码:

render(filename, content, theme) {
    return new Promise((resolve, reject) => {
        let options = JSON.parse(JSON.stringify(this.config.renderOptions));
        if (theme)
            options.paths.push(path.resolve(this.config.theme.path, theme));
        options.paths.push(path.resolve(this.config[filename].resolve.root));
        less.render(content, options, (e, output) => {
            if (e != null) reject(e);
            resolve(output.css);
        });
    });
}

我已经研究这个很长时间了,我很感激任何帮助。谢谢!

我一直得到错误'TypeError:不能读取属性'resolve'的我不知道为什么会这样。任何想法吗?

问题出在这一行:

options.paths.push(path.resolve(this.config[filename].resolve.root));

,试图读取对象this.config[filename]上的属性.resolve。错误信息告诉您对象是undefined。你的代码等于:

var myObject = undefined;
console.log(myObject.resolve); //Cannot read property 'resolve' of undefined

我不能从你在你的问题中列出的代码告诉为什么this.config[filename]undefined