Writing Tests for NativeScript apps

This has been bugging me for a while. While working on my business logic and REST API connection in my NativeScript project, I couldn’t write tests because requiring the tns-core-modules broke the test.

NativeScript is a library with tooling to create iOS and Android apps in JavaScript. And it works smoothly. However at the moment the docs aren’t always complete. Anyways, building and deploying an app on a mobile device (or emulator) takes somewhere in the range of a minute or so on a fast machine.

That doesn’t really work if you’re not sure if your logic makes sense.

In comes <a href="http://npmjs.com/package/proxyquire" target="_blank">proxyquire</a>. Install proxyquire:

npm install --save-dev proxyquire

Then make sure you stub the package you want to be stubbed, with @noCallThru.

Example:

So now you can setup mocha for example to work with the app.

npm install --save-dev mocha

Edit your package.json so your npm scripts reads:

"scripts": {
    "test": "mocha tests/*",
    "start": "node index.js"
  },

And go ahead and run npm test

Cover image courtesy of Willie Hardin: https://www.flickr.com/photos/14934865@N07/3134743730/


Words by fritzvd

comments powered by Disqus