【问题标题】:What is TypeError: mime.lookup is not a function in Supertest?什么是 TypeError:mime.lookup 不是 Supertest 中的函数?
【发布时间】:2021-02-24 16:44:17
【问题描述】:

我刚刚升级到节点 14.15.4,现在任何带有 send 的超测试 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}`);
        });
    });

我错过了什么?

【问题讨论】:

    标签: node.js express mocha.js supertest


    【解决方案1】:

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

    .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 实现可以互换地支持任何表单类型,这可能对所有人都很好测试目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2017-11-21
      • 1970-01-01
      • 2021-04-19
      • 2016-03-12
      • 2017-03-22
      • 1970-01-01
      相关资源
      最近更新 更多