Machine Learning based Unit Tesing in JavaScript

Rate this content
Bookmark

The talk covers the current scenario of writing test cases in JavaScript and the problems associated with the time and resources spent by companies to write the test cases and the lack of automation in this area.

Then the talk will cover how AI and machine learning is being leveraged by tools such as Github Copilot and Ponicode to autogenerate test cases thus simplifying the software testing process.

This talk has been presented at TestJS Summit 2022, check out the latest edition of this Tech Conference.

FAQ

The speaker is Shabail Amba, a TensorFlow.js working group member and a Google support mentor at TensorFlow.

The title of the talk was 'Machine Learning based unit testing in JavaScript.'

Machine learning can help automate the process of writing unit test cases, reduce manual work, improve test coverage, and suggest useful test data combinations that might be missed by human testers.

Unit testing is a way of testing the smallest pieces of code, such as functions, sub-routines, or methods, to ensure they work correctly. It is a crucial part of the entire test-driven cycle in software development.

Manual unit testing can be very time-consuming and labor-intensive, especially for large codebases. It requires writing extensive boilerplate code and may miss out on some test cases.

Automation in unit testing reduces the manual effort required, helps in generating useful test data combinations, improves code coverage, and ensures more comprehensive testing.

Pony Code is an AI-powered extension that generates unit test cases based on the functions in your codebase. It provides recommendations and automatically creates unit test files, helping to improve test coverage and reduce manual work.

GitHub Copilot is an AI pair programmer that uses OpenAI Codex to suggest code completions and entire functions. It can auto-complete unit test cases based on the context of your code, making the process faster and easier.

The tools demonstrated were Pony Code and GitHub Copilot. Both tools help in automating the generation of unit test cases and improving test coverage.

You can find more information about Pony Code at docs.ponycode.com.

Shivay Lamba
Shivay Lamba
22 min
03 Nov, 2022

Comments

Sign in or register to post your comment.

Video Summary and Transcription

This talk explores machine learning-based unit testing in JavaScript and TypeScript, focusing on tools like the Pony Code VS Code extension and GitHub Copilot. The Pony Code tool provides a graphical user interface for generating and managing unit test cases. GitHub Copilot acts as an intelligent code auto-completion tool, understanding the context of the code and suggesting unit test cases. These tools aim to improve code coverage and achieve 100% coverage.

1. Introduction to Machine Learning in Unit Testing

Short description:

Hello everyone. Welcome to my talk at TestJS Summit 2022. My talk title is Machine Learning based unit testing in JavaScript. Today, we are going to be taking a look at a few of the tools that are available today to software developers to be able to do automation in unit testing with the help of and specifically within either JavaScript or TypeScript. Now the first tool that we're going to be taking a look at is the Pony Code tool.

Hello everyone. Welcome to my talk at TestJS Summit 2022. My talk title is Machine Learning based unit testing in JavaScript. A very quick introduction about myself. I'm Shabail Amba, a TensorFlow.js working group member and a Google support mentor at TensorFlow.

So first question that of course will come to our mind is why machine learning and software testing. Now, if you want to understand more about how machine learning is actually used in software testing, I'd recommend you to go and watch my last year's video at TestJS Summit 2021 where I presented a talk at predictive testing in JavaScript with the help of machine learning and how you can basically use machine learning for being able to not just predict what type of test cases should be put but also how you can make your test coverage and basically make better test cases with the help of machine learning. So in case you're interested in that, I'll definitely recommend you to check this out. But of course today's session is all about unit testing and how you can basically include machine learning in unit testing.

So in case you're not aware of what unit testing is all about, so basically unit test is a way of testing the smallest pieces of code and these could be let's say a function, a sub-routine or a method that might be defined in a programming language. And of course we have different types of testing techniques like integration testing but unit testing is by far the smallest testing procedure that works on a single function or a single method. And that makes unit testing also a really important part of your entire test driven cycle, because it's very important to ensure that we are able to test each and every smallest functionality of a software because if you have a very large code base, even a small function that might not work properly might end up causing some unknown errors inside of your entire code base. So it's really important, and unit test cases form a very important aspect of the entire test driven programming for any organization where testing is being conducted.

But one of the drawbacks that we can discuss about unit testing is that it's a very manual process. So whenever software testers are writing unit test cases for functions, now let's say if you have a very large code base, you could have a multitude of different functions that you're writing. So if you end up writing unit test cases for each one of them, then it can take a lot of time to just write and it's a lot of manual work. So over the past few years, a lot of effort is going into automating the unit test case procedures. And again, the main idea is that with the help of the automation in unit testing, we are able to actually capable of reducing the boilerplate code that is required for unit test cases. And we are also able to suggest and create useful a combination of test data that can be provided to software testers to be used as part of their unit testing capabilities. And this overall helps us to actually improve our code coverage as well. And helps us to write custom test cases that might have been missed out by the software developers. And that's where the advancements in machine learning and AI is basically helping us to be able to do this automation in unit test casing and helping us to improve the overall test coverage that was more difficult to do manually if there is manual test cases being written by humans. So today, we are going to be taking a look at a few of the tools that are available today to software developers to be able to do automation in unit testing with the help of and specifically within either JavaScript or TypeScript. Now the first tool that we're going to be taking a look at is the Pony Code tool. So Pony Code is an AI powered extension that helps to basically generate unit test cases for you and based on the functions that you write inside of your code base, it will recommend certain test cases for you and automatically generate the unit test file for you and this essentially is also done with the help of a very intuitive graphic interface. So you basically get either, uh, like, you know, you can either use the VS code extension, or you can also use the CLI that is provided by Pony Code that, uh, can be used for let's say writing unit test cases or automating the unit test cases for an entire, uh, directory. And you can also use, uh, the CLI tool, uh, as mentioned, like the CLI tool as well. Uh, and you can also use GitHub actions, uh, provided by Pony Code that allow you to basically combine test generation during your CI-CD platform. So let's basically take a look at the product demo. And of course, if you want to explore, uh, deeper into how to get started Pony Code, I'll recommend you to go ahead and take a look at docs.ponycode.com.

2. Using the Pony Code VS Code Extension

Short description:

But for this demo, we'll quickly move on to VS code. You can install the Pony code VS code extension, CLI, or use it as a GitHub action. The Pony Code VS code extension provides a graphical user interface where you can generate unit test cases. It suggests test cases to increase test coverage. You can easily add or remove test cases using the Pony Code UI. The generated test files are automatically created by Pony Code.

But for, uh, this demo, we'll just quickly now move on to VS code. And first let's basically go to this particular link for the docs. So as you can see, like you can either install the Pony code VS code extension or the CLI or you can also use it as a GitHub action, and it's free, free to use for individual developers.

So if you click on the VS code extension and install it inside of your VS code, it will look something like this. So if you're basically using, uh, you can see that it will basically, uh, show up over here as this Pony Code icon inside of your VS code. And you can take a look at some of the things that has the getting started page. Uh, and this basically shows you how to very quickly get started and generate a unit test cases inside of your application.

So, uh, the one that we're going to be using is the Pony Code VS code extension. So in this case, uh, in my file directory, I have created a Pony Code, um, folder. And over here, what I've done is I've just created a simple index.cs file, and I have just created a simple function for adding two numbers. So first let's take a look how this will basically So, um, as soon as you basically write any function, uh, Pony Code will automatically be able to detect the function that you have written, like a JavaScript function. And over here, you can directly see that, uh, we get this, uh, UI graphical user interface where you can see that we can click on this, uh, Pony Code unit test, which will basically generate a UI, um, platform. And you can see that it basically comes up with some suggestions for unit test cases that you can basically write.

So we, you can see that these are the suggestions being provided by Pony Code in order for you to, let's say, increase your test coverage. So let's say that, uh, I want to probably take this example of A and B. Basically, since it's an add to numbers function, we are having two variables and here it takes these two values. So what I can very easily do is that either I can test this particular function. And see that, you know, uh, it basically just generates the response and I can actually click on the plus sign. If I want to basically add this particular specific test case inside of my, uh, Pony Code. And what it immediately does is that now we have a new folder in place and this folder is the Pony Code folder. So if I go and explore this Pony Code folder, you can see that it is the index.ts.js file. So this is an automated, automatically created, uh, test generation file that has been created by Pony Code. So if we basically take a look at, like, you know, what it includes, right. So you can see that, uh, it has, first of all, what it does, it has done. Is that it has, uh, like, you know, imported our index.js file and now it has basically described a function, uh, index.addToNumbers because that's a function that we like, you know, have from our index.js file that we have created and we have generated a over here where it is, uh, like, you know, getting to these two values. Now, let's say if we want to add some additional test cases, we can very easily just click on the UI, uh, on the Pony Code UI and add that. So that will basically automatically get added, uh, inside of your index.test.js function. As you can see that we have another, uh, like, you know, function, another function that we have added, another test case that we have added. So this way you can either remove or delete, uh, these test cases and automatically the unit test that is being generated for you, uh, will be changed. Now let's also take an example for an async function.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Network Requests with Cypress
TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Cecilia Martinez, a technical account manager at Cypress, discusses network requests in Cypress and demonstrates commands like cydot request and SCI.INTERCEPT. She also explains dynamic matching and aliasing, network stubbing, and the pros and cons of using real server responses versus stubbing. The talk covers logging request responses, testing front-end and backend API, handling list length and DOM traversal, lazy loading, and provides resources for beginners to learn Cypress.
Testing Pyramid Makes Little Sense, What We Can Use Instead
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
Gleb Bahmutov
Roman Sandler
2 authors
The testing pyramid - the canonical shape of tests that defined what types of tests we need to write to make sure the app works - is ... obsolete. In this presentation, Roman Sandler and Gleb Bahmutov argue what the testing shape works better for today's web applications.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.

Workshops on related topic

Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Featured Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
How to Start With Cypress
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
Filip Hric
Filip Hric
The web has evolved. Finally, testing has also. Cypress is a modern testing tool that answers the testing needs of modern web applications. It has been gaining a lot of traction in the last couple of years, gaining worldwide popularity. If you have been waiting to learn Cypress, wait no more! Filip Hric will guide you through the first steps on how to start using Cypress and set up a project on your own. The good news is, learning Cypress is incredibly easy. You'll write your first test in no time, and then you'll discover how to write a full end-to-end test for a modern web application. You'll learn the core concepts like retry-ability. Discover how to work and interact with your application and learn how to combine API and UI tests. Throughout this whole workshop, we will write code and do practical exercises. You will leave with a hands-on experience that you can translate to your own project.
Leveraging LLMs to Build Intuitive AI Experiences With JavaScript
JSNation 2024JSNation 2024
108 min
Leveraging LLMs to Build Intuitive AI Experiences With JavaScript
Featured Workshop
Roy Derks
Shivay Lamba
2 authors
Today every developer is using LLMs in different forms and shapes, from ChatGPT to code assistants like GitHub CoPilot. Following this, lots of products have introduced embedded AI capabilities, and in this workshop we will make LLMs understandable for web developers. And we'll get into coding your own AI-driven application. No prior experience in working with LLMs or machine learning is needed. Instead, we'll use web technologies such as JavaScript, React which you already know and love while also learning about some new libraries like OpenAI, Transformers.js
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
Detox 101: How to write stable end-to-end tests for your React Native application
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
Top Content
WorkshopFree
Yevheniia Hlovatska
Yevheniia Hlovatska
Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop