1

Imagine this code:

const myFunc = () => exec('node foo.js') 

Now my library executes myFunc, but it doesn't know if it executes another script. How to make it know it?

P.S. The lib, which is a test runner + coverage tool, needs to know every piece of code that was run by the test.

3
  • Why does the lib need to 'know' this? This is none of its concern. A function is a function. Commented Nov 10, 2018 at 6:55
  • The lib, which is a test runner + coverage tool, needs to know every piece of code that was run by the test. This is a simplified description. Commented Nov 10, 2018 at 7:13
  • Consider updating the question with relevant details, so it would be clearer for users who may have the same problem. Commented Nov 10, 2018 at 7:21

1 Answer 1

1

If there's a need to detect API calls that may result in uncontrolled script execution and there's a need to detect calls, APIs have to be patched, e.g.:

const childProcess = require('child_process'); const { exec } = childProcess; childProcess.exec = function () { console.error(new Error('No coverage')); return exec.apply(this, arguments); }; 

This applies to global.eval, global.Function, all child_process module functions, some vm and worker_threads functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I always over-engineer things, when indeed this (patching/decorating) can be done! Great idea, thanks! And listing possible patch-ables is very good!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.