Being obsessive about testing (and conformity), I adopted Jest to test my interfaces, simply because that’s what the good folk over at Facebook use too. As such, unit testing was to be built into my project template right from the beginning. This is where I ran into a problem…
grunt-init calls on a file called
template.js. The code in this file typically prompts the user for project-specific details (name, description, homepage, etc.) and writes all the information to the project’s
package.json file. You can see my full template here. The following shows the
project.json properties that are not configurable by the user:
props primarily consist of the development dependencies I always uses when developing with React. They are written to the file by the
init.writePackageJSON function. For those familiar with Jest, you know that
package.json needs a little Jest-specific addition for the the script preprocessor, which looks like this:
Being somewhat naive, I assumed that I could simply add the
jest property to the
props object like this:
This does not work.
init.writePackageJSON only sets the typical
package.json options. The
jest property is not typical, and the official documentation is not especially helpful. Here’s the relevant part pertaining to the
The callback can be used to post-process properties to add/remove/whatever.
So yes, it is possible. You just have to add/remove/whatever the
jest property. After poking around the code a little bit, I figured out that this is how you do it:
See my ever-changing React project template here: https://github.com/RaphaelDeLaGhetto/grunt-init-gebo-react-hai.