什么是 TypeError:mime.lookup 不是 Supertest 中的函数?

问题描述

我刚刚升级到节点 14.15.4,现在任何带有 send 的 supertest post 请求都失败了,并显示错误“TypeError: mime.lookup is not a function”。


    it("Should be able to select an address",() => {
        return request(app).post(`/admin/machines/${newmachine._id}/assignments/add`)
        .send({userid: testuser._id.toString()})
        .expect(302)
        .then( (res) => {
            expect(res.headers.location).to.include(`/admin/machines/${newmachine._id}`);
        });
    });

出现错误

       Should be able to select an address:
     TypeError: mime.lookup is not a function
      at Test.Request.type (node_modules/supertest/node_modules/superagent/lib/node/index.js:265:38)
      at Test.RequestBase.send (node_modules/supertest/node_modules/superagent/lib/request-base.js:575:21)
      at Context.<anonymous> (test/routes/admin/machines.js:275:10)
      at processImmediate (internal/timers.js:461:21)

但是,如果我使用“字段”而不是发送,错误就会消失:

        return request(app).post(`/admin/machines/${newmachine._id}/assignments/add`)
        .field('userid',testuser._id.toString())
        .expect(302)
        .then( (res) => {
            expect(res.headers.location).to.include(`/admin/machines/${newmachine._id}`);
        });
    });

我错过了什么?

解决方法

我找到了答案,或者至少找到了解决方法。如果我添加:

.set('Content-Type','application/x-www-form-urlencoded')

错误消失了。所以要更新上面的原始代码:

it("Should be able to select an address",() => {
    return request(app).post(`/admin/machines/${newmachine._id}/assignments/add`)
    .set('Content-Type','application/x-www-form-urlencoded')
    .send({userid: testuser._id.toString()})
    .expect(302)
    .then( (res) => {
        expect(res.headers.location).to.include(`/admin/machines/${newmachine._id}`);
    });
});

虽然迂腐地说,这不一定是一个准确的测试,因为表单类型更改为“x-www-form-urlencoded”,但考虑到 node.js 实现确实可以互换地支持任何表单类型,这对所有人来说可能都很好测试目的。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...