开玩笑-'child_process'包中的模拟函数 [英] Jest - mock function in 'child_process' package

查看:190
本文介绍了开玩笑-'child_process'包中的模拟函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写单元测试,并模拟包'child_process'中的'exec'方法.

I'm writing unit tests and which to mock the 'exec' method in package 'child_process'.

__ mocks __/child_process.js

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

module.exports = child_process;

这是测试文件:

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(() => {Promise.resolve()})

        await runCassandraMigration(args.migration_script_path, args.logger)
    });
  });

运行测试时,出现以下错误:

When I run the test I get the following error:

child_process.exec.mockImplementation is not a function

我测试的模块

const fs = require('fs-extra')
const promisify = require('util').promisify
const execAsync = promisify(require('child_process').exec)

module.exports = async (migration_script_path, logger) => {
  try {
    console.log()
    const {stdout, stderr} = await execAsync(`cassandra-migration ${migration_script_path}`)
    logger.info({stdout: stdout, stderr: stderr}, 'Finished runing cassandra migration')
    await fs.remove(migration_script_path)
  } catch (e) {
    logger.error(e, 'Failed to run cassandra migration')
    throw Error()
  }
}

请告知.

推荐答案

迟到的... answer ?...

A late... answer?...

昨天我遇到了同样的错误,问题是我没有在测试文件中调用jest.mock('child_process').

Yesterday I got the same error and the problem was that I wasn't calling jest.mock('child_process') in my test file.

Jest 文档表示,在嘲笑Node的核心模块时调用jest.mock('child_process') is required.我看到您这样做了,但由于某种原因它无法正常工作(也许Jest没有将其提升到顶部).

Jest documentation says that when mocking Node's core modules calling jest.mock('child_process') is required. I see you do this but for some reason it is not working (maybe Jest is not hoisting it to the top).

无论如何,在Jest版本24.9.0中,我没有得到child_process.exec.mockImplementation is not a function错误,但是由于您的测试没有很好地实现而出现了一些其他错误.

Anyways, with Jest version 24.9.0 I don't get the child_process.exec.mockImplementation is not a function error but get some other errors because your test is not well implemented.

为使您的测试工作顺利进行,我:

To make your test work I:

logger

exec的实现更新为child_process.exec.mockImplementation((command, callback) => callback(null, {stdout: 'ok'})) 并且(对于通过测试不是必需的)将fs.remove的实现更新为fs.remove.mockImplementation(() => Promise.resolve())

Updated the implementation of exec to child_process.exec.mockImplementation((command, callback) => callback(null, {stdout: 'ok'})) And also (not necessary for the test to pass) updated the implementation of fs.remove to fs.remove.mockImplementation(() => Promise.resolve())

赞:

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

const defaultArguments = () => {
  return {
    migration_script_path: './home',
    logger: {
      info: function () {},
      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((command, callback) => callback(null, {stdout: 'ok'}))

        await runCassandraMigration(args.migration_script_path, args.logger)
    });
  });
});

这篇关于开玩笑-'child_process'包中的模拟函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆