node.js Jest 测试 exec.stdout.on(‘data’, data)

huangapple go评论117阅读模式
英文:

nodejs Jest testing exec.stdout.on('data',data)

问题

尝试测试一个包含child_process库exec的函数。

  1. const { exec } = require('child_process')
  2. const randomFunc = () => {
  3. const newSync = exec('要执行的一些命令')
  4. newSync.stdout.on('data', data => {
  5. console.log(data.toString())
  6. })
  7. }

testfile:

  1. const {randomFunc} = require(randomFuncFile)
  2. const { exec } = require('child_process')
  3. jest.mock('child_process')
  4. it('测试', () => {
  5. const readStreamObject = {
  6. on: jest.fn().mockImplementation(function (event, handler) {
  7. handler('streaming ')
  8. })
  9. }
  10. exec.mockImplementation(data => ({stdout: readStreamObject}))
  11. randomFunc()
  12. expect(exec.stdout.on).toHaveBeenCalled()
  13. }

我得到以下错误:

TypeError: Cannot read properties of undefined (reading 'on')

一些提示会很有帮助。

英文:

Trying to to test a function the incorporates the exec of child_process library.

  1. const { exec } = require('child_process')
  2. const randomFunc = () => {
  3. const newSync = exec('some command to execute')
  4. newSync.stdout.on('data', data => {
  5. console.log(data.toString())
  6. })
  7. }

testfile:

  1. const {randomFunc} = require(randomFuncFile)
  2. const { exec } = require('child_process')
  3. jest.mock('child_process')
  4. it('test', () => {
  5. const readStreamObject = {
  6. on: jest.fn().mockImplementation(function (event, handler) {
  7. handler('streaming ')
  8. })
  9. }
  10. exec.mockImplementation(data => ({stdout: readStreamObject})
  11. randomFunc()
  12. expect(exec.stdout.on).toHaveBeenCalled()
  13. }

I'm getting

> TypeError: Cannot read properties of undefined (reading 'on')

some tips would be great.

答案1

得分: 1

你可以通过模拟的exec()函数获取模拟执行的stdout,方法是使用exec.mock.results[0].value.stdout,详见这里

index.js

  1. const { exec } = require('child_process');
  2. const randomFunc = () => {
  3. const newSync = exec('要执行的某个命令');
  4. newSync.stdout.on('data', (data) => {
  5. console.log(data.toString());
  6. });
  7. };
  8. module.exports = { randomFunc };

index.test.js

  1. const { exec } = require('child_process');
  2. const { randomFunc } = require('./');
  3. jest.mock('child_process');
  4. it('测试', () => {
  5. const mStdout = {
  6. on: jest.fn().mockImplementation(function (event, handler) {
  7. handler('流数据');
  8. }),
  9. };
  10. exec.mockImplementation(() => ({ stdout: mStdout }));
  11. randomFunc();
  12. // expect(mStdout.on).toHaveBeenCalled();
  13. // 或者
  14. expect(exec.mock.results[0].value.stdout.on).toHaveBeenCalled();
  15. });

测试结果:

  1. PASS stackoverflow/75036469/index.test.js (8.403 s)
  2. test (15 ms)
  3. console.log
  4. 流数据
  5. at stackoverflow/75036469/index.js:6:13
  6. ----------|---------|----------|---------|---------|-------------------
  7. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
  8. ----------|---------|----------|---------|---------|-------------------
  9. All files | 100 | 100 | 100 | 100 |
  10. index.js | 100 | 100 | 100 | 100 |
  11. ----------|---------|----------|---------|---------|-------------------
  12. Test Suites: 1 passed, 1 total
  13. Tests: 1 passed, 1 total
  14. Snapshots: 0 total
  15. Time: 9.016 s
英文:

You can get the mock stdout returned by mocked exec() function via exec.mock.results[0].value.stdout, See mockFn.mock.results

index.js:

  1. const { exec } = require('child_process');
  2. const randomFunc = () => {
  3. const newSync = exec('some command to execute');
  4. newSync.stdout.on('data', (data) => {
  5. console.log(data.toString());
  6. });
  7. };
  8. module.exports = { randomFunc };

index.test.js:

  1. const { exec } = require('child_process');
  2. const { randomFunc } = require('./');
  3. jest.mock('child_process');
  4. it('test', () => {
  5. const mStdout = {
  6. on: jest.fn().mockImplementation(function (event, handler) {
  7. handler('streaming');
  8. }),
  9. };
  10. exec.mockImplementation(() => ({ stdout: mStdout }));
  11. randomFunc();
  12. // expect(mStdout.on).toHaveBeenCalled();
  13. // or
  14. expect(exec.mock.results[0].value.stdout.on).toHaveBeenCalled();
  15. });

Test result:

  1. PASS stackoverflow/75036469/index.test.js (8.403 s)
  2. test (15 ms)
  3. console.log
  4. streaming
  5. at stackoverflow/75036469/index.js:6:13
  6. ----------|---------|----------|---------|---------|-------------------
  7. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
  8. ----------|---------|----------|---------|---------|-------------------
  9. All files | 100 | 100 | 100 | 100 |
  10. index.js | 100 | 100 | 100 | 100 |
  11. ----------|---------|----------|---------|---------|-------------------
  12. Test Suites: 1 passed, 1 total
  13. Tests: 1 passed, 1 total
  14. Snapshots: 0 total
  15. Time: 9.016 s

huangapple
  • 本文由 发表于 2023年1月7日 06:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75036469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定