首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JestJS:如何在嵌套(异步)函数中测试调用函数

JestJS:如何在嵌套(异步)函数中测试调用函数
EN

Stack Overflow用户
提问于 2018-10-17 06:50:56
回答 1查看 801关注 0票数 2

我正在尝试为嵌套函数编写单元测试,如下所示:

myFunction.js

代码语言:javascript
复制
const anotherFunction = require('./anotherFunction.js')

module.exports = (app, io) => {
  return (req, res) => {
    const { id, value } = req.query
    req.app.locals['target' + id].pwmWrite(value)
    anotherFunction(app, io)
    res.send({ value })
  }
}

我想测试一下是否调用了pwmWrite()anotherFunction()

但由于return (req, res) => {}和导入函数的原因,我遇到了一些问题。

这是我的尝试,它不起作用:

myFunction.test.js

代码语言:javascript
复制
test('should call pwmWrite() and anotherFunction()', async () => {
  const app = {}
  const io = { emit: jest.fn() }
  const req = {
    app: {
      locals: {
        target1: { pwmWrite: () => 25 }
        }
      }
    }
  }
  expect.assertions(1)
  expect(req.app.locals.target1.pwmWrite).toHaveBeenCalled()
  await expect(myFunction(app, io)).resolves.toEqual(25)
})
EN

回答 1

Stack Overflow用户

发布于 2019-09-17 14:33:51

以下是解决方案:

myFunction.js

代码语言:javascript
复制
const anotherFunction = require('./anotherFunction.js');

module.exports = (app, io) => {
  return (req, res) => {
    const { id, value } = req.query;
    req.app.locals['target' + id].pwmWrite(value);
    anotherFunction(app, io);
    res.send({ value });
  };
};

anotherFunction.js

代码语言:javascript
复制
module.exports = (app, io) => {
  return 'do something';
};

单元测试:

代码语言:javascript
复制
jest.mock('./anotherFunction');

const myFunction = require('./myFunction');
const anotherFunction = require('./anotherFunction');

describe('test suites', () => {
  test('should call pwmWrite() and anotherFunction()', () => {
    const app = {};
    const io = { emit: jest.fn() };
    const id = '1';
    const value = 'jest';
    const req = {
      query: { id, value },
      app: {
        locals: {
          target1: { pwmWrite: jest.fn() }
        }
      }
    };
    const res = { send: jest.fn() };
    myFunction(app, io)(req, res);
    expect(anotherFunction).toBeCalledWith(app, io);
    expect(req.app.locals.target1.pwmWrite).toBeCalledWith(value);
  });
});

单元测试结果和覆盖率报告:

代码语言:javascript
复制
 PASS  src/stackoverflow/52845000/myFunction.spec.js
  test suites
    ✓ should call pwmWrite() and anotherFunction() (5ms)

--------------------|----------|----------|----------|----------|-------------------|
File                |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------------|----------|----------|----------|----------|-------------------|
All files           |    88.89 |      100 |    66.67 |    88.89 |                   |
 anotherFunction.js |       50 |      100 |        0 |       50 |                 2 |
 myFunction.js      |      100 |      100 |      100 |      100 |                   |
--------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.113s

下面是完成的演示:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/52845000

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52845000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档