The code being tested

All of the code used in this example is located in the /chapter-09-node/testing-example folder. The code and corresponding tests are very simple and are not representative of real-world applications, but they're intended to demonstrate how to use Jest for testing. The following code represents the file structure of the /testing-example folder:

├── /src
| ├── /__tests__
| │ └── main.test.js
| └── main.c
├── package.json
└── package-lock.json

The contents of the C file that we'll test, /src/main.c, is shown as follows:

int addTwoNumbers(int leftValue, int rightValue) {
return leftValue + rightValue;
}

float divideTwoNumbers(float leftValue, float rightValue) {
return leftValue / rightValue;
}

double findFactorial(float value) {
int i;
double factorial = 1;

for (i = 1; i <= value; i++) {
factorial = factorial * i;
}
return factorial;
}

All three functions in the file are performing simple mathematical operations. The package.json file includes a script to compile the C file to a Wasm file for testing. Run the following command to compile the C file:

npm run build

There should be a file named main.wasm in the /src directory. Let's move on to describing the testing configuration step.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset