Write Unit tests using ChatGPT
Tags: chatgpt, openai, javascript, testing
Published: 2023-02-25 07:15:24

Writing unit tests can be a challenging task for developers. It is crucial to ensure that the code is functional and free from bugs. However, the process of writing tests can be time-consuming, especially when dealing with complex code. In this article, we will discuss a solution to this problem using OpenAI's natural language processing capabilities to generate unit tests.


TL;DR  _In this article, we will discuss how to integrate the OpenAI GPT language model with a Node.js application to generate unit test case for custom code_.


## What is OpenAI GPT?

The OpenAI GPT (Generative Pre-trained Transformer) language model is a state-of-the-art machine learning model that has been pre-trained on vast amounts of text data. It is capable of generating human-like text data, making it an ideal tool for a wide range of applications, including chatbots, language translation, and content generation.



![ChatGPT Nodejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pk4heugq3mi7sifqnxe.png)


## Node and OpenAI GPT

Node.js is a popular runtime environment for building server-side applications using JavaScript. It provides a wide range of features and packages that make it an excellent choice for building scalable and high-performance applications. One of the most exciting developments in recent years has been the rise of natural language processing (NLP) and machine learning (ML) technologies, which have revolutionised the way we interact with and process text data. In this article, we will discuss how to integrate the OpenAI GPT language model with a Node.js application to generate unit test case for custom code.


## Integrating ChatGPT with Node.js for writing Unit testcases

To integrate OpenAI GPT with a Node.js application, we can use the OpenAI API, which provides a RESTful interface for interacting with the language model. Here's an example of how to use the OpenAI API to generate text data in a Node.js application


First, we need to create an account on the OpenAI website and obtain an API key. [here](https://platform.openai.com/account/api-keys)

![OpenAI token genration page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17ibir07tch43olh1a72.png)

Next, we need to install the "openai" package using NPM, which provides a client for the OpenAI API.

```
npm install openai
```
Once the package is installed, we can use it to create a client object that can be used to interact with the OpenAI API:

 ```
 const openai = require('openai')('YOUR_API_KEY')
 ```
To generate text data using the OpenAI GPT model, we need to create a prompt that describes what we want the model to generate. Here's an example of a prompt

```
const unitTestPrompt = `I have the following file, write me unit tests using jest.
        Here is the file:
        ${req.body.promptText}`;
```

Next, we can use the OpenAI client to generate text data based on the prompt

```
const completion = await openai.createCompletion({
  model: "text-davinci-003",
  prompt: unitTestPromt,
  max_tokens: 2018,
  logprobs: 0,
  best_of: 1,
  temperature: 0,
  top_p: 1,
  frequency_penalty: 0.5,
  presence_penalty: 0,
});
```
we are using the "createCompletion" method of the OpenAI client to generate text data. We specify the language model we want to use ("davinci"), the prompt we created earlier, the maximum number of tokens to generate (1024), the number of responses to generate (1), and the "temperature" parameter, which controls the randomness of the generated text.


Below is the code in action, We generated unit test for a factorial function 

```
const factorial = function fac(n) {
  return n < 2 ? 1 : n * fac(n - 1);
};
```

![postman curl for generating unit testcases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ivq35fnpcwct62aahe4.png)




Finally, here is the complete JavaScript code.

It defines a route for '/unit-test' which accepts a POST request with a promptText field in the request body. The promptText is then used to create a GPT-3 prompt with a specific format that instructs the language model to generate unit tests for the file described in the promptText.

{% embed https://gist.github.com/deepeshk1204/2d4e2b570cf3b3f53340fcfb0c58faf1.js %}