Day One: Create-React-App with Mocha and Enzyme – with No NPM Eject!

There is a saying. Job hunting is a full time job. God, were they correct. I have been splitting my time between reading postings, researching companies, applying jobs, updating job site portfolios, interview prep, code testing (recently did a React app in 5 days), networking in-person and online, helping out at startup Sponsorlane, and still staying updated with tech news.

By the time Labor Day rolled by, I realized I haven’t worked on my side project involving Redux and mocha for over a month! Not to say I haven’t learn anything – I have been studying algorithm and design pattern. My debug and Node skill have also noticeably improved thanks to my effort in Sponsorlane. But I set up the goal to learn Redux and mocha months ago, and I want to finish what I started, damn it!

Thus, I am setting a new goal. I am going to re-start my app, Nextbus Compare. A fresh start. This time I will record my process: I will update my app one day, and post the process the next day.

So, here is Day One! The topic is:

Create-React-App with Mocha and Enzyme – with No NPM Eject!

The last time I tried to do this, there were multiple different sources of guides, most of them either confused me with too many steps or didn’t worked. I ended up doing npm eject. I also wasn’t what I did and what package I installed to made it work. This time, I am much more prepared. So, here is what I did:

I started with Dave Schinkel’s blog post, Mocha + Enzyme with create-react-app. It is a good read, although there are some things that don’t work with my system. Here is essentially what I did.

  1. create-react-app nextbus-compare
  2. Since I don’t have yarn installed and babel-preset-react-app kept giving me import errors, I did npm install –save-dev es2015 babel-preset-react preset-stage-0. While Dave stated es2015 is ignored by create-react-app, I ended up needing it. The use of babel-core compiler later on resulted in import error because babel-core don’t recognize jsx. As a babel newbie, I don’t know why my outcome was different. I just know that babel-preset-react-app doesn’t work on my system. Please comment if you know why……
  3. In package.json:
    1. Added: “babel”: { “presets”: [ “react”, “es2015”, “stage-0” ] }
    2. Modify “test” command to: “NODE_ENV=development mocha –compilers js:babel-core/register –require ignore-styles src/test/*.spec.js” . Note that in Dave’s post, his test command is missing the double quotes. Not a big deal, but if you were a Node newbie like me, know that double quote is required! Also, there is an additional –require ignore-styles here. That’s because if I don’t, babel-core will try to compile css files as js files, therefore triggering “unknown token” errors:
      CSS unexpected token
      The unexpected token

      There are ways to bypass it, but I don’t plan to test my styles at this stage, so I am just going to do a simple –require ignore-styles in my test command line.

  4. For testing, depending on if you also want chai or sinon, you would do npm install –save-dev mocha enzyme chai sinon react-test-renderer. Note that react-test-renderer is a a dependency requirement for enzyme. Without it, you will get this error message:

    Enzyme implicit dependencies
    Implicit dependencies error!
  5. Here is a very simple test script I did in src/test/App.spec.js:

    test file
    Super simple test file
  6. Now do a simple npm test, and this should happen:
Mocha testing successful
Test Successful!!!

That’s it for day one!

Save

Save