相关疑难解决方法(0)

Jest - 'child_process' 包中的模拟函数

我正在编写单元测试并模拟包“child_process”中的“exec”方法。

__mocks__/child_process.js

const child_process = jest.genMockFromModule('child_process');
child_process.exec = jest.fn()

module.exports = child_process;
Run Code Online (Sandbox Code Playgroud)

这是测试文件:

const fs = require('fs-extra'),
      child_process = require('child_process'),
      runCassandraMigration = require('../../lib/runCassandraMigration.js')

const defaultArguments = () => {
  return {
    migration_script_path: './home',
    logger: {
      error: function () {}
    }
  };
}

jest.mock("fs-extra")
jest.mock("child_process")

describe('Running cassandra migration tests', function () {
  describe('successful flow', function () {
    it('Should pass without any errors ', async function () {
        let args = defaultArguments();
        let loggerSpy = jest.spyOn(args.logger, 'error')

        fs.remove.mockImplementation(() => {Promise.resolve()})
        child_process.exec.mockImplementation(() => …
Run Code Online (Sandbox Code Playgroud)

unit-testing jestjs

6
推荐指数
1
解决办法
4012
查看次数

标签 统计

jestjs ×1

unit-testing ×1