Understanding Truffle variable injection magic in test files

Julien Klepatch

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:


Leave a Reply

More great articles

Simple smart contract

Starting with Solidity smart contract is a bit overwhelming... the tools, the Ethereum Blockchain, the Solidity language... that's a lot…

Read Story

Inspect storage with calls & return statements

In Solidity, you can return some values from functions, like in other programming languages. Its also possible to use these…

Read Story

ToDo List Ethereum Dapp (Step2) | Writing the smart contract – part II

This tutorial is part of the series ToDo List Ethereum Dapp. It is the Step 2. Tutorials already published: Intro…

Read Story

Never miss a minute

Get great content to your inbox every week. No spam.
[contact-form-7 id="6" title="Footer CTA Subscribe Form"]