Understanding Truffle variable injection magic in test files

In Truffle, when you write tests, you can use ‘artifacts’ and ‘web3’ objects, without defining these names:

const MyContract = artifacts.require('MyContract'); //artifacts was never defined before !
const web3 = new Web3('http://localhost:8545'); //same thing for Web3!

How is it possible? this is not valid Javascript! Well, this is what Truffle calls “variable injection”, and to understand how this work, watch this video to have the answer and have a peek at Truffle internals:

https://eattheblocks-pro.teachable.com/courses/smart-contract-testing/lectures/8789238

Leave a Reply

Your email address will not be published. Required fields are marked *