Go back to the main page

Getting started with Nodejs and QuickBooks Online Part 3



Let's get npm test working

In part 1 and part 2 we just got our feet wet. An important part of digging deeper is to check out the tests, which display more complex transactions. Now, in part 3 we will get npm test working being it is not as simple as you might think.

  1. Grab the latest from node-quickbooks or your fork of it.
  2. git checkout pull upstream master
    Minimul says —

    I am back to using the node-quickbooks master so in that regard I am not picking up directly from part 2.

    We need to spin up the example app again to get the access tokens and realm id so we can record them into config.js, which is used by npm test to properly talk to your sandbox.

  3. I noticed some changes in for example/package.json after the upstream pull so:
  4.   $ cd example
      $ npm install
  5. Edit example/app.js and make the following mods:
  6. -var consumerKey    = '',
    -    consumerSecret = ''
    +var consumerKey    = process.env.MINIMULCASTS_CONSUMER_KEY, // substitute your env variable here
    +    consumerSecret = process.env.MINIMULCASTS_CONSUMER_SECRET
    // ...
    -                         true); // turn debugging on
    +                         false); // turn debugging off
    // ...
           accounts.QueryResponse.Account.forEach(function(account) {
    -        console.log(account.Name)
    +        //console.log(account.Name)
  7. Still in the example dir do a nodemon app.js
  8. Go to the browser http://localhost:3000/.
  9. Click the "Connect to QuickBooks" button and authenticate with your sandbox.
  10. Minimul says —

    Make sure your Intuit app is enabled both for "QuickBooks" & "Payments" or you will get an error when connecting.

    Need to be enabled for both API capabilities.
  11. Go back to your console running nodemon app.js and you should see the information we need for the config.js
  12. Copy the app tokens and realm id from the console.
    This section is at the 8:44 mark.
  13. You can shutdown the example app now.
  14. Create a .env file (don't commit to source control) in the project root directory looking like this:
  15. export OAUTH_TOKEN_SECRET=[ paste secret here (from step 7) ]
    export OAUTH_TOKEN=[ paste token here ]
    export REALM_ID=[ paste realm id here ]
  16. Make sure to "source" this file in all of your console windows: source .env
  17. This section is at the 10:33 mark.
  18. Edit config.js to look like this:
  19. module.exports = {
      consumerKey:     process.env.MINIMULCASTS_CONSUMER_KEY, // change for your app's consumer key
      consumerSecret:  process.env.MINIMULCASTS_CONSUMER_SECRET, // change for your app's consumer secret
      token:           process.env.OAUTH_TOKEN,
      tokenSecret:     process.env.OAUTH_TOKEN_SECRET,
      realmId:         process.env.REALM_ID,
      useSandbox:      true,
      debug:           false,
      // Set useSandbox to false when moving to production. For info, see the following url:
      // https://developer.intuit.com/v2/blog/2014/10/24/intuit-developer-now-offers-quickbooks-sandboxes
      testEmail:       ''  // Use this email address for testing send*Pdf functions
  20. Now you are ready to run the tests, so again in the console that you ran source .env run npm test
  21.   $ source .env
      $ npm test
    Getting some green!


That's a wrap for part 3, in which we "simply" got the tests running. In part 4 I am going to be leveraging this test setup to answer a question on the Intuit developer forums that will show how you can leverage the tests to getting going on more complex transactions. Also, reference the code for this and part 4 of the tutorial.