0

I am trying to run a JS test script with truffles but I can not seem to find the right place to store the assertRevert file so that the module can be found by the script when I try to run the test.

const { assertRevert } = require('assertRevert');

Is that the top of my script.

The Error I am getting is Cannot find module 'assertRevert'

Thanks in advance !

conwise17
  • 111
  • 1
  • 13
  • Try require('assertRevert.js'), otherwise, node searches in its own installation path rather than in your local path. – goodvibration Jul 31 '18 at 12:31
  • Still not working. Where would you store the file in the test folder or main directory of the project ? – conwise17 Jul 31 '18 at 12:46
  • Sorry, try require('./assertRevert.js'). Of course, if the file assertRevert.js is not located in the same path as your script, then use the correct relative path instead of ./. – goodvibration Jul 31 '18 at 13:36
  • Still no luck, any other ideas what could be going wrong ? The assetRevert file is in my test folder and still getting no luck. – conwise17 Aug 01 '18 at 10:10
  • Do you properly export in that file? i.e., do you assign module.exports = ... somewhere? Also, why exactly do you do const { assertRevert } = ... with those curly braces? Seems like you're not entirely familiar with how to export JS code properly. There's a lot of information on that, and you probably want to search it on Stack Overflow, because it doesn't have anything to do specifically with Ethereum. – goodvibration Aug 01 '18 at 12:29
  • Yes here is the code

    module.exports = { assertRevert: async (promise) => { try { await promise; } catch (error) { const revertFound = error.message.search('revert') >= 0; assert(revertFound,Expected "revert", got ${error} instead); return; } assert.fail('Expected revert not received'); }, };

    And I copied the code from the open zeppelin erc20 testing source code.

    – conwise17 Aug 01 '18 at 13:45
  • So you should do const assertRevert = require('./assertRevert.js').assertRevert;. – goodvibration Aug 01 '18 at 13:48
  • Thanks for you help mate, its greatly appreciated ! I ended up creating a helpers folder within the project directory.

    The final piece of code that worked is const assertRevert = require('../helpers/assertRevert.js').assertRevert;

    – conwise17 Aug 02 '18 at 09:29
  • You're welcome. If you're looking for a cleaner way to catch exceptions in your tests, then you can check out this answer of mine on a related question. – goodvibration Aug 02 '18 at 10:07

0 Answers0