Unit test js code with jest using node.js

Hi,
a qt developer would lough at this, but it works for me and as it took me some hrs to set it up, here a howto for whoever it may help.

two years ago i tried typescript and did jasmine tests on typescript code, but the conversion from ts to js code that can be consumed in an sfos was a pain
the gain from typecript was in fact not that great on my simple code so …

i am currently on windows, using visual studio code and qt creator in parallel.

  • install node.js, it comes with npm as package manager
  • run node --version, npm -version to check that your path env var is setup correctly
  • install jest, that would be the test framework
  • my tests are in /test folder
  • my js code is in qml
  • my tests are executed in dist folder

why, cause my js code that runs in sfos wont run in node, there are some .pragma, and .import statements which is not understood by node.js
so i have a script that converts my js sources to js that is understood by node, which was quite simple:

  • change the import statements to require
  • add some exports at the bottom
  • copy test files to dist too
  • run tests in dist folder
    done

this:
.import “Persistance.js” as DB
to that:
const DB = require(’./Persistance.js’);

and add the bottom for each method i want to test:
module.exports = { getUsersUpcommingEvents, getUsersTrackedItems, getUpcommingEventsForTrackedItem } ;

a few moder details:
after node installation
open the app folder you want to work in and then only install jest.
that will create a package.json file in the root folder, that wont be used by app but you wont deploy it anyway …

in package.config i have added this:
{
“jest”: {
“testMatch”: ["/dist/**/*Test.js"]
},
“scripts”: {
“pre-test”: “node ./test/pre-test.js”,
“test”: “jest”
} …

a) to tell jest how to find tests
b) to test npm what to execute

npm run pre-test
will run the script that does the conversion and copy all to dist
npm run test
will execute the testcases

you can check the scripts etc in my sailkick repo

yes there are also other obstacles, like the httptrequest object in qt may differ from the one in node, but that was easy to overcome, i made the object injectable which i would have needed anyway for any kind of mocking, and luckily the interface was same (only made sure to use the front-end httprequest and in browser and not the backend one as used in node.js itself …

2 Likes

Quite the interesting setup you have there.
Is there no way to use babel with a very simple vanilla js transpile? My experience is sailfish works with vanilla js in most (if not all) ways. The resulting code won’t win any beauty contest, but it could just work?

i have actually babel installed, but i do not know how to configure it to create the needed magic.
e.g. i have no clue what type/version of js we use in qt5.5. and i was not able to find it out on the internet

so yes, your question is valid, but …

ECMAScript 5th edition (JavaScript Host Environment | Qt QML 5.6)

Arent we still on 5.5 ?
I will check what babel does with that info anyway