How to do unit testing on node.js web application in visual studio?

Wayne Roseberry 20 Reputation points
2024-01-28T19:45:56.46+00:00

I created a project in Visual Studio 2022. I created a node.js web application using the "Blank node.js web application" template.

I want to create a unit test. What is the recommended way to do that? I hit problems following an article that advised installing Chutzpah as the test runner framework and Jasmine as the test library. Specifically, when trying to install the Jasmine packge, Nuget Package Manager gave me the following error:

PM> Install-Package Jasmine Install-Package : Project 'Default' is not found.

Looking in Package Manager console, the "Default project:" dropdown is empty, which as I understand is intentional with Node.js solution projects.

How is a person supposed to setup unit testing in Visual Studio when working with Node.js? Do they have leave the IDE entirely? Is there a way to make it work?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,827 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
336 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pinaki Ghatak 2,795 Reputation points Microsoft Employee
    2024-01-28T20:45:35.2633333+00:00

    You can set up unit testing in Visual Studio for a Node.js project. Here are the steps you can follow:

    1. Choose a Test Framework: Visual Studio supports several JavaScript frameworks for unit testing, including Mocha, Jasmine, Tape, and Jest. You can choose the one that best suits your needs.
    2. Install the Test Framework: Before adding unit tests to your project, make sure the framework you plan to use is installed locally in your project. This can be done using the npm package installation window.
    3. Configure the Test Framework in Visual Studio: In Solution Explorer, right-click the project and choose Edit Project File. Make sure that the following properties are present in the .esproj file with the values shown:
    <PropertyGroup>
      <JavaScriptTestRoot>src\\</JavaScriptTestRoot>
      <JavaScriptTestFramework>Jest</JavaScriptTestFramework>
    </PropertyGroup>
    

    This example specifies Jest as the test framework. You could specify Mocha, Tape, or Jasmine instead¹. The JavaScriptTestRoot element specifies that your unit tests will be in the src folder of the project root.

    1. Install npm Packages: In Solution Explorer, right-click the npm node and choose Install new npm packages. Use the npm package installation dialog to install the following npm packages: jest, jest-editor-support. These packages are added to the package.json file under dependencies.
    2. Add Test Section in package.json: In package.json, add the test section at the end of the scripts section:
    "scripts": {
      ...
      "test": "jest"
    },
    
    1. Add a Unit Test File: In Solution Explorer, right-click the src folder and choose Add > New Item, and then add a new file named App.test.tsx. This adds the new file under the src folder. Add the following code to App.test.tsx:
    describe ('testAsuite', () => {
      it ('testA1', async () => {
        expect (2).toBe (2);
      });
    });
    
    1. Run the Tests: You can run the tests from the Test Explorer in Visual Studio. If you don’t already have Test Explorer open, you can find it by selecting Test > Test Explorer in the menu bar¹. To run unit tests from the command-line, right-click the project in Solution Explorer, choose Open in Terminal, and run the command specific to the test type.

    I hope this helps

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful