8. Introduction to Functions4. Unit Tests

4 - Unit Tests

This content is also available at learn.palantir.com ↗ and is presented here for accessibility purposes.

📖 Task Introduction

The ../src/__tests__/index.ts file contains a sample test that makes use of the "myFunction" method we just explored. Tests are not a mandatory part of writing Functions, but they do provide a layer of assurance that our code is operating as expected.

🔨 Task Instructions

  1. Open the ../src/__tests__/index.ts file by selecting it from the Files panel on the left.
  2. Remove the comment characters (//) from lines 1, 4, and 7.
  3. Note the test code on line 7, which uses TypeScript’s expect function to test whether an input of 42 into myFunction will return a value of 43 (recall that function executes n+1).
  4. With Code Assist running, click the ▶ Test button in the top right area of the screen.

The test executes and the results appear in the Tests helper window at the bottom of the screen. As you might suspect, if you were to change the expected value in the test to 100 or go back to your “myFunction” method and change the syntax to n+2 that the test would fail.

Read the Overview, Example, and Running tests headings in the Unit Testing documentation. The page goes on from there to describe the process of writing unit tests with Ontology types, which is not required for this tutorial.