0

I have an esm project I'm trying to test with ava, nyc and sinon. I have had some success using sinon to spy on console.log and report whether or not it is being called, but on another set of files, where I feel I'm doing the same things, I can't get it to work. I don't know if it's sinon or ava or what is the problem. I made a test repo here. When you run npm run test it will fail and say console.log was not called, but you can see at the top of the output a statement was logged. What am I doing wrong?

Thank you!

James South
  • 534
  • 4
  • 15

1 Answers1

1

main() is asynchronous and you only call console.log() after awaiting. At that point you've already torn down the spy. Use await main() and make the test implementation asynchronous as well.

Mark Wubben
  • 3,329
  • 1
  • 19
  • 16
  • Thank you @mark-wubben but I tried that with no luck. I have updated the repo to make the test async and await the call to main. The tested flow is this: set up spy, call main with no args, no args throws an error, catch error and log message to console, test that it is true that console.log was called 1 time, remove spy. output of `npm run test`: error message logged but test fails, async or not. – James South Feb 14 '19 at 20:57
  • oooh finally getting somewhere - when you comment out the import statement in main, the test passes! So I guess `esm` is where the problem lies...?? – James South Feb 14 '19 at 22:37