EvanBurchard / wish

node assertion library without special syntax
42 stars 4 forks source link

Wish fails when test fails in electron/spectron #12

Open fkopp81 opened 6 years ago

fkopp81 commented 6 years ago

Hi,

I have found wish as well via "Refactoring JS". I have seen wish is having trouble under Windows, but while I am using that OS I haven't seen this specific error in other issues:

ReferenceError: repl is not defined

  at getWishFromNodeHistory (node_modules/wish/lib/wish.js:118:5)
  at getExpression (node_modules/wish/lib/wish.js:113:14)
  at handleFalseWish (node_modules/wish/lib/wish.js:128:36)
  at wish (node_modules/wish/lib/wish.js:145:15)
  at Object.<anonymous> (src/App.test.js:15:7)
  at Object.exec (node_modules/webdriverio/build/lib/helpers/safeExecute.js:28:24)
  at Object.resolve (node_modules/webdriverio/build/lib/webdriverio.js:191:29)
  at node_modules/webdriverio/build/lib/webdriverio.js:540:32
  at _fulfilled (node_modules/q/q.js:854:54)
  at self.promiseDispatch.done (node_modules/q/q.js:883:30)
  at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
  at node_modules/q/q.js:624:44
  at runSingle (node_modules/q/q.js:137:13)
  at flush (node_modules/q/q.js:125:13)
  at process._tickCallback (internal/process/next_tick.js:150:11)

I am trying to use wish together with Spectron and Jest to test an electron app.

From what I can see, "repl" is not defined in an electron context, but it can be require'd by adding:

const repl = require('repl');

However, that leads to another error:

TypeError: Cannot read property 'match' of undefined

  at getWishFromNodeHistory (node_modules/wish/lib/wish.js:119:33)
  at getExpression (node_modules/wish/lib/wish.js:114:14)
  at handleFalseWish (node_modules/wish/lib/wish.js:129:36)
  at wish (node_modules/wish/lib/wish.js:146:15)
  at Object.<anonymous> (src/App.test.js:17:7)
  at Object.exec (node_modules/webdriverio/build/lib/helpers/safeExecute.js:28:24)
  at Object.resolve (node_modules/webdriverio/build/lib/webdriverio.js:191:29)
  at node_modules/webdriverio/build/lib/webdriverio.js:540:32
  at _fulfilled (node_modules/q/q.js:854:54)
  at self.promiseDispatch.done (node_modules/q/q.js:883:30)
  at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
  at node_modules/q/q.js:624:44
  at runSingle (node_modules/q/q.js:137:13)
  at flush (node_modules/q/q.js:125:13)
  at process._tickCallback (internal/process/next_tick.js:150:11)
EvanBurchard commented 6 years ago

@fkopp81 Hey. Thanks for filing this.

So I'm not sure what the exact failure is here because I don't know what electron does for a stack trace. repl.repl.history[0] appears to be undefined.

If you are able to link to a project with setup instructions, so that I can reproduce, I can give it a try. Mainly, I'm curious if this would be the case for any electron app (meaning I could reproduce from basic instructions).

Alternatively, if you're interested in debugging this yourself, could you console.info repl.repl and repl.repl.history in the line above line 119?

That said, it's possible it will just be blank though. If the repl isn't actually in use as in a normal node runtime, then the __LINE__ analog will need to come from somewhere else (for browsers it looks at the loaded file whereas in a node shell, it looks at the repl history).

Maybe a better option would be to edit this function:

function getExpression(){
  if(environment()==="file"){
    const line = createLineFromErrorObject();
    const content = fs.readFileSync(line.fileName, "utf8");
    return parseLine(content, line.number-1);
  } else{
    return getWishFromNodeHistory();
  };
};

Remove the else clause from that all together, and see if it works. If it does, then the environment function should find a way to distinguish between browsers, electron, and node.

Again, thanks for reporting. I'll leave this open, but probably won't make this change myself in the short term at least. If you have any other ideas or want to make a patch, let me know.