TypeScript library & unit tests
Last updated
Last updated
In this guide, we will use the Jest testing framework in conjunction with the the "Basic TS Package" template (found in OXIDE) to create a reusable library that is also fully tested.
In the end, the package structure will look like this:
Navigate to the Packages workspace, or open the Packages panel, and create a new package. In the Package template dropdown, select "Basic TS Package" and proceed to follow the prompts. This created an app package with a basic framework to generate custom PDF reports.
Note: you can also use the command palette to find the Create package action.
Since this package represents a library, we will export a simple sum
function inside the src/index.ts
file
Now we create a new file called tests/sum.test.ts
(OXIDE will create a new tests
folder) and insert the following Jest code:
To run Jest, we need to first add a few dependencies and make some changes in our package.json
file. Tip: if you cannot see the package.json
file, ensure that the Show configuration files setting in OXIDE is toggled on.
The "build"
command is changed.
Our "main"
entry now contains the 'src'
We add the latest versions of all the dependencies.
We add a "jest"
field which tells Jest to find our tests correctly
Because changes have been made to the package.json
file, we must update the lock file using yarn.
In OXIDE, right click on the package, and select the update action as shown:
When writing Jest tests using TypeScript, Jest will use Babel to transpile the files. For this reason we need to add a babel.config.js
file with the following configuration:
Since we now have TypeScript code in a tests folder, and have added a the @types/jest
package, we need to make a few changes to the tsconfig.json
file:
Next we want to use the function that we've defined in the package library within a view. The below is an example of how to achieve this, by importing the relevant function(s) from the package into the view's TypeScript.
Now when a testing deploy is triggered, not only will the library be built and accessible in your views, but the build will also only succeed if all tests pass.