I have a JS script which I pass to truffle test from command line:

truffle test my_script.js

I would like this script to take an input argument (retrieved via process.argv[2]).

Does `truffle support this in some way?

I tried truffle test my_script.js 0 and received the following error from truffle:

TypeError: file.match is not a function
    at C:\Users\...\cli.bundled.js:319282:21
    at Array.filter (<anonymous>)
    at C:\Users\...\cli.bundled.js:319281:21
    at getFiles (C:\Users\...\cli.bundled.js:319274:16)
    at Object.run (C:\Users\...\cli.bundled.js:319280:5)
    at Command.run (C:\Users\...\cli.bundled.js:160766:20)
    at Object.<anonymous> (C:\Users\...\cli.bundled.js:318404:9)
    at __webpack_require__ (C:\Users\...\cli.bundled.js:22:30)
    at C:\Users\...\cli.bundled.js:68:18
    at Object.<anonymous> (C:\Users\...\cli.bundled.js:71:10)

I tried truffle "test my_script.js 0" and received the following error from ganache-cli:

TypeError: Cannot read property 'toJSON' of null
    at C:\Users\...\cli.node.js:90686:25
    at C:\Users\...\cli.node.js:21771:5
    at C:\Users\...\cli.node.js:88595:7
    at C:\Users\...\cli.node.js:7542:16
    at C:\Users\...\cli.node.js:7759:32
    at C:\Users\...\cli.node.js:7534:16
    at C:\Users\...\cli.node.js:88590:9
    at Object.decode (C:\Users\...\cli.node.js:49614:5)
    at C:\Users\...\cli.node.js:88587:20
    at C:\Users\...\cli.node.js:7671:20

I tried truffle test "my_script.js 0" but then the test doesn't seem to be doing anything:

0 passing (0ms)

Thank you!!!


truffle test does not seem to support such a feature as of this writing.

It takes the following arguments :

truffle test <test_file> [--compile-all] [--network <name>] [--verbose-rpc]

So it does not seem to have a way to pass arguments to the test file.

See command reference for the details.

One option you may be able to use is (Depending on your use case) it to set an environment variable, and read the environment variable in your test (in place of reading an argument passed to your test)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.