BretCameron / mocha-vm

Run Mocha tests inside a vm2 sandbox environment
5 stars 4 forks source link

trying to use mocha-vm in another project #8

Open tansaku opened 2 years ago

tansaku commented 2 years ago

I'm having all sorts of trouble trying to use mocha-vm as part of another project. I've linked mocha-vm into my project like so:

$ npm link <path-to-local-mocha-vm>

and I can see it linked up in node-modules/.package-lock

And I can see mocha-vm in the node_modules/ directory of my project. If I cd into the node_modules/mocha-vm/ directory within my project (which is of course just where I've git cloned mocha-vm on my local system) I can run it fine from the node console like so:

$ node
Welcome to Node.js v16.3.0.
Type ".help" for more information.
> const { run } = require('../mocha-vm');
undefined
> code =  "describe('tripler', function(){\n  it('triples 2', function(){\n    expect(1).to.eql(1)\n  })\n})"
> process.on('uncaughtException', (err) => {
  console.error('uncaughtException (from vm2?):', err);
})
> run(code).then(obj => console.log(obj));

Promise { <pending> }
>   tripler
    ✓ triples 2

  1 passing (6ms)

{
  summary: {
    passed: 1,
    failed: 0,
    tests: 1,
    suites: 1,
    depth: 0,
    duration: '3ms'
  },
  data: [ { depth: 0, suite: 'tripler', tests: [Array], duration: '2ms' } ]
}

however if I try from the node console at the root of my project I get this error:

$ node
> const { run } = require('mocha-vm');
> code =  "describe('tripler', function(){\n  it('triples 2', function(){\n    expect(1).to.eql(1)\n  })\n})"
> process.on('uncaughtException', (err) => {
  console.error('uncaughtException (from vm2?):', err);
})
> run(code).then(obj => console.log(obj));
Promise { <pending> }
> 

  2) Uncaught error outside test suite
uncaughtException (from vm2?): TypeError [Error]: Cannot read property 'title' of undefined
    at Proxy.<anonymous> (vm.js:89:18)
    at Object.base.apply (/Users/samueljoseph/Documents/Github/BretCameron/mocha-vm/node_modules/vm2/lib/contextify.js:228:34)
    at Runner.emit (node:events:406:35)
    at Runner.emit (node:domain:532:15)
    at processImmediate (node:internal/timers:464:21)
    at process.topLevelDomainCallback (node:domain:147:15)
    at process.callbackTrampoline (node:internal/async_hooks:128:24) {
  uncaught: true
}
  1) Uncaught error outside test suite

I've been looking at the code specified in the stack trace, e.g. node_modules/vm2/lib/contextify.js:228 which is:

return Decontextify.value(fnc.apply(context, args));

but don't really know where to go from there ...

tansaku commented 2 years ago

I've added:

console.log(`context: ${context}`)
console.log(`args: ${args}`)

which gives me this:

> run(code).then(obj => console.log(obj));
context: undefined
args: describe('tripler', function(){
  it('triples 2', function(){
    expect(1).to.eql(1)
  })
}),
context: [object Promise]
args: data => resolve(data)
Promise { <pending> }
> 

context: (e) => {
      if (!runnerStart) runnerStart = Date.now();
      suiteCount += 1;
      suiteStart = Date.now();

      if (obj.suite) {
        data.push(obj);
        obj = {};
      }

      const { title, parent } = e;
      if (parent.title !== "") obj.parent_suite = parent.title;

      let parentName = parent.title;
      let nextParent = e.parent,
        nextTitle,
        count = 0;

      // Set count based on number of parents
      while (nextTitle !== "") {
        try {
          nextParent = nextParent.parent;
          nextTitle = nextParent.title;
          count++;
        } catch {
          break;
        }
      }

      // Set current suite based on title
      currentSuite = title;

      // Styling aid, suggesting how much to indent by
      obj.depth = count;
      if (count > nestedLevels) nestedLevels = count;
      if (title) obj.suite = title;
      // if (parent.title) obj.parent = parent.title;
    }
args: [object Object],[object Object]

but I still can't work out what's going wrong:

uncaughtException (from vm2?): TypeError [Error]: Cannot read property 'title' of undefined
    at Proxy.<anonymous> (vm.js:89:18)

The vm.js file in mocha-vm only has 35 lines ...

tansaku commented 2 years ago

interesting, I appear to now have things working by adding the following clause to runner.js:

      if(typeof(parent) !== 'undefined') {