【问题标题】:Is there a way to fix this "TypeError: express.json is not a function" when testing Express with Jest?用 Jest 测试 Express 时,有没有办法解决这个“TypeError:express.json 不是函数”?
【发布时间】:2021-01-23 07:44:38
【问题描述】:

我正在尝试学习如何在 express.js 上运行 Jest 测试,但出现此错误

TypeError: express.json is not a function 

但是,如果我从 index.js 中注释掉这两行:

app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb', extended: true}));

然后它会工作并通过前 2 个测试。我该如何解决这个错误?

Here the index.js

and here the index.test.js

【问题讨论】:

    标签: node.js unit-testing express jestjs backend


    【解决方案1】:

    你没有模拟 express.json 方法。

    例如

    index.js:

    const express = require('express');
    const cors = require('cors');
    const app = express();
    const corsOptions = {
      origin: true,
    };
    const PORT = process.env.PORT || 4002;
    app.use(cors(corsOptions));
    app.use(express.json({ limit: '50mb' }));
    app.use(express.urlencoded({ limit: '50mb', extended: true }));
    
    app.listen(PORT, (err) => {
      if (err) {
        console.log('Rumble in the Bronx! ' + err);
      } else {
        console.log(`? <(Communications active at port http://localhost:${PORT}/)`);
      }
    });
    

    index.test.js:

    const express = require('express');
    
    const useSpy = jest.fn();
    const listenSpy = jest.fn();
    const urlencodedMock = jest.fn();
    const jsonMock = jest.fn();
    
    jest.mock('express', () => {
      return () => ({
        listen: listenSpy,
        use: useSpy,
      });
    });
    
    express.json = jsonMock;
    express.urlencoded = urlencodedMock;
    
    describe('64259504', () => {
      test('should initialize an express server', () => {
        require('./index');
        expect(jsonMock).toBeCalled();
        expect(urlencodedMock).toBeCalled();
        expect(listenSpy).toHaveBeenCalled();
      });
    
      test('should call listen fn', () => {
        require('./index');
        expect(jsonMock).toBeCalled();
        expect(urlencodedMock).toBeCalled();
        expect(listenSpy).toHaveBeenCalled();
      });
    });
    

    带有覆盖率报告的单元测试结果:

     PASS  src/stackoverflow/64259504/index.test.js (13.203s)
      64259504
        ✓ should initialize an express server (626ms)
        ✓ should call listen fn (1ms)
    
    ----------|----------|----------|----------|----------|-------------------|
    File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
    ----------|----------|----------|----------|----------|-------------------|
    All files |       75 |       50 |        0 |       75 |                   |
     index.js |       75 |       50 |        0 |       75 |          13,14,16 |
    ----------|----------|----------|----------|----------|-------------------|
    Test Suites: 1 passed, 1 total
    Tests:       2 passed, 2 total
    Snapshots:   0 total
    Time:        15.01s
    

    【讨论】:

    • `无效的变量访问:listenSpy` - 我收到了这个错误
    • 感谢幻灯片。明确地将 json 设置为相同的模拟对我有帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 2020-12-13
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多