Rooting for Truffle
Truffle already has a
test command (run all tests) and a
watch command (watch for file changes and rebuild my app), but I like my workflow to combine the two - watch for when I save files and automatically run the tests.
If you’ve been working with truffle you’ll know that you install truffle with
npm install -g truffle which gives you a set of commands to run. But how can you change those commands? How do you get your changes back into the project to be used by others?
Here’s what we’re going to do:
- Fork the
truffle-coreproject in github
- Clone our fork to a local repository
- Verify the project
- Create a new branch that describes the problem
- Make our changes
- Test them out
- Create a pull request with our new code
Many people will be able to skip this section (and the next), but if you’re not familiar with github and pull requests, you’re going to want to sign up with github, navigate to https://github.com/trufflesuite/truffle-core, and click the
Fork button. You now have a copy of this project at
If you have
git installed, you'll want to go to your projects directory and
git clone https://github.com/<username>/truffle-core at this point and change into the directory it creates. If you'd prefer a GUI, you can install the github desktop client, gitkraken sourcetree and click on the 'Open in desktop' button on
Before continuing, you need to install the dependencies (
npm install) and make sure everything is as it should be by running the project tests (
We’re now going to create a branch that describes what we’re trying to do. This branch will live until our code is accepted. It will have the initial changes, but it might also have some updates because we found a bug or the reviewer asked it to be changed. I’m calling mine
269_watch_tests (269 is the issue people raised because they wanted this feature).
git checkout -b 269_watch_tests
If you’re using the GUI, there’s going to be a branch button in there somewhere…
Hm. This bit is your problem. You need to know that the entry point is
npm test again. If tests now fail, you've broken it and you're going to need to fix it. If you haven't (congrats!) you need to test that the changes you've made actually work.
For this step we’re going to link your version of truffle with a new project and go through the steps to initialise and run the project. In your truffle core project
This is going to allow you to use your project from an npm project you’re about to create. From your projects directory:
> mkdir test
> cd test
> npm init (accept all the defaults - it doesn't matter for our use case)
Is this ok? (yes) > npm link truffle-core
test/node_modules/truffle-core -> ~/.nodebrew/node/v7.10.0/lib/node_modules/truffle-core -> ~/truffle-core
Okay, now you have a new project linked to your truffle-core, let’s try using it:
> node_modules/truffle-core/cli.js init
Unhandled rejection No packages found
Documentation: http://truffleframework.com/docs Commands:
Compile: truffle compile
Migrate: truffle migrate
Test: truffle test
Yay! Now I’m going to try out my new command.
> node_modules/truffle-core/cli.js watch --network=development --tests
Perfect! Let’s get this code into a pull request.
git add lib/commands/watch.js
git commit -m "Add --tests flag to watch"
git push origin 269_watch_tests
The last step is to go to your
https://github.com/<username>/truffle-core and click 'Open pull request' Congratulations - you have now moved the entire community forward a tiny increment.