Skip to content

Commit 1d85c3f

Browse files
Merge pull request #14150 from Mikejo5000/mikejo-br24
Update TS unit testing for ASP.NET Core
2 parents cc66115 + a4b1086 commit 1d85c3f

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

docs/javascript/unit-testing-javascript-with-visual-studio.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Unit testing JavaScript and TypeScript"
33
description: Explore unit testing support in Visual Studio for JavaScript and TypeScript code by using the Node.js Tools for Visual Studio.
4-
ms.date: "12/06/2024"
4+
ms.date: "7/31/2025"
55
ms.topic: "how-to"
66
ms.devlang: javascript
77
author: "mikejo5000"
@@ -33,13 +33,13 @@ The supported frameworks are:
3333
::: moniker-end
3434

3535
::: moniker range="<=vs-2019"
36-
If your favorite framework is not supported, see [Add support for a unit test framework](#addingFramework) for information on adding support.
36+
If your favorite framework isn't supported, see [Add support for a unit test framework](#addingFramework) for information on adding support.
3737
::: moniker-end
3838

3939
::: moniker range=">=vs-2022"
4040
## Write unit tests for a CLI-based project (.esproj)
4141

42-
The [CLI-based projects](../javascript/javascript-in-vs-2022.md#project-templates) supported in Visual Studio 2022 work with Test Explorer. Vitest is the built-in test framework for React and Vue projects (previously Jest), and Karma and Jasmine is used for Angular projects. By default, you will be able to run the default tests provided by each framework, as well as any additional tests you write. Just hit the **Run** button in Test Explorer. If you don’t already have Test Explorer open, you can find it by selecting **Test** > **Test Explorer** in the menu bar.
42+
The [CLI-based projects](../javascript/javascript-in-vs-2022.md#project-templates) supported in Visual Studio 2022 work with Test Explorer. Vitest is the built-in test framework for React and Vue projects (previously Jest), and Karma and Jasmine is used for Angular projects. By default, you'll be able to run the default tests provided by each framework, as well as any additional tests you write. Just hit the **Run** button in Test Explorer. If you don’t already have Test Explorer open, you can find it by selecting **Test** > **Test Explorer** in the menu bar.
4343

4444
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.
4545

@@ -77,7 +77,7 @@ The following example is based on the TypeScript React project template provided
7777

7878
- vitest
7979

80-
This package are added to the *package.json* file under dependencies.
80+
This package is added to the *package.json* file under dependencies.
8181

8282
> [!NOTE]
8383
> If you're using jest, the jest-editor-support npm package is required as well as the jest package.
@@ -107,12 +107,15 @@ The following example is based on the TypeScript React project template provided
107107
});
108108
```
109109

110-
1. Open Test Explorer (choose **Test** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.
110+
1. Open Test Explorer (choose **Test** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list.
111111

112112
![Screenshot of Test Explorer test discovery (.esproj).](../javascript/media/vs-2022/unit-tests-esproj-discovery.png)
113113

114114
> [!NOTE]
115-
> For TypeScript, do not use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
115+
> For TypeScript, don't use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
116+
117+
> [!IMPORTANT]
118+
> If the output from **Tests** in the Output window shows a `ReadOnlySpan` error during test discovery, use this workaround for a known [MSBuild issue](https://github.com/dotnet/msbuild/issues/7873). Open the folder, *Program Files\Microsoft Visual Studio\2022\\<version\>\Common7\IDE\Extensions\TestPlatform*, and rename *System.Memory.dll* to a different name. This fix enables test discovery.
116119
117120
### Run tests (.esproj)
118121

@@ -128,13 +131,13 @@ For some unit test frameworks, unit tests are typically run against the generate
128131
> In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing **Debug**. In more complex scenarios, such as some scenarios that use source maps, you may have difficulty hitting breakpoints in TypeScript code. As a workaround, try using the `debugger` keyword.
129132
130133
> [!NOTE]
131-
> Profiling tests and code coverage are not currently supported.
134+
> Profiling tests and code coverage aren't currently supported.
132135
133136
::: moniker-end
134137

135138
## Write unit tests for ASP.NET Core
136139

137-
To add support for unit testing of JavaScript and TypeScript in an ASP.NET Core project, you need to add TypeScript, Npm, and unit testing support to the project by including required NuGet packages.
140+
To add support for unit testing of JavaScript and TypeScript in an ASP.NET Core project, you need to add TypeScript, npm, and unit testing support to the project by including required NuGet packages.
138141

139142
### Add a unit test (ASP.NET Core)
140143

@@ -148,9 +151,16 @@ The following example is based on the ASP.NET Core Model-View-Controller project
148151

149152
1. In the **Browse** tab, search for the following packages and install each one:
150153

154+
::: moniker range=">=vs-2022"
155+
- [Microsoft.TypeScript.MSBuild](https://www.nuget.org/packages/Microsoft.TypeScript.MSBuild)
156+
- [Npm](https://www.nuget.org/packages/Npm)
157+
::: moniker-end
158+
159+
::: moniker range="vs-2019"
151160
- [Microsoft.TypeScript.MSBuild](https://www.nuget.org/packages/Microsoft.TypeScript.MSBuild)
152161
- [Npm](https://www.nuget.org/packages/Npm)
153162
- [Microsoft.JavaScript.UnitTest](https://www.nuget.org/packages/Microsoft.JavaScript.UnitTest/)
163+
::: moniker-end
154164

155165
Use the NuGet package to add TypeScript support instead of the npm TypeScript package.
156166

@@ -177,7 +187,7 @@ The following example is based on the ASP.NET Core Model-View-Controller project
177187

178188
# [Mocha](#tab/mocha)
179189

180-
The `JavaScriptTestRoot` element specifies the location for your unit tests. To simplify the configuration for Mocha, in this example we point the test root to the output folder, *wwwroot\js\tests\*.
190+
The `JavaScriptTestRoot` element specifies the location for your unit tests. To simplify the configuration for Mocha, in this example we point the test root to the output folder, *wwwroot\js\tests\\*.
181191

182192
```xml
183193
<PropertyGroup>
@@ -337,12 +347,17 @@ The following example is based on the ASP.NET Core Model-View-Controller project
337347
})
338348
```
339349

340-
1. Open Test Explorer (choose **Test** > **Windows** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list. The following illustration shows the Jest example, with two different unit test files.
350+
1. Open Test Explorer (choose **Test** > **Windows** > **Test Explorer**) and Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list. The following illustration shows the Jest example, with two different unit test files.
341351

342352
![Screenshot of Test Explorer test discovery (ASP.NET Core).](../javascript/media/vs-2022/unit-tests-asp-dotnet-discovery.png)
343353

344354
> [!NOTE]
345-
> For TypeScript, do not use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
355+
> For TypeScript, don't use the `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests. You can use the `outdir` option, but make sure that configuration files such as `package.json` and `tsconfig.json` are in the project root.
356+
357+
::: moniker range="vs-2022"
358+
> [!IMPORTANT]
359+
> If the output from **Tests** in the Output window shows a `ReadOnlySpan` error during test discovery, use this workaround for a known [MSBuild issue](https://github.com/dotnet/msbuild/issues/7873). Open the folder, *Program Files\Microsoft Visual Studio\2022\\<version\>\Common7\IDE\Extensions\TestPlatform*, and rename *System.Memory.dll* to a different name. This fix enables test discovery.
360+
::: moniker-end
346361

347362
### Run tests (ASP.NET Core)
348363

@@ -358,7 +373,7 @@ For some unit test frameworks, unit tests are typically run against the generate
358373
> In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing **Debug**. In more complex scenarios, such as some scenarios that use source maps, you may have difficulty hitting breakpoints in TypeScript code. As a workaround, try using the `debugger` keyword.
359374
360375
> [!NOTE]
361-
> Profiling tests and code coverage are not currently supported.
376+
> Profiling tests and code coverage aren't currently supported.
362377
363378
::: moniker range="<=vs-2019"
364379
## <a name="addingFramework"></a>Add support for a unit test framework
@@ -387,7 +402,7 @@ Discovery of available test frameworks occurs at Visual Studio start. If a frame
387402

388403
## Unit tests in .NET Framework
389404

390-
You are not limited to writing unit tests in just your Node.js and ASP.NET Core projects. When you add the TestFramework and TestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them using the Test Explorer window.
405+
You aren't limited to writing unit tests in just your Node.js and ASP.NET Core projects. When you add the TestFramework and TestRoot properties to any C# or Visual Basic project, those tests will be enumerated and you can run them using the Test Explorer window.
391406

392407
To enable this, right-click the project node in the Solution Explorer, choose **Unload Project**, and then choose **Edit Project**. Then in the project file, add the following two elements to a property group.
393408

@@ -455,12 +470,12 @@ property in the **Properties** window is set to the correct test framework for y
455470
> [!NOTE]
456471
> The unit test options will take preference over the settings for individual files.
457472
458-
After opening Test Explorer (choose **Test** > **Windows** > **Test Explorer**), Visual Studio discovers and displays tests. If tests are not showing initially, then rebuild the project to refresh the list.
473+
After opening Test Explorer (choose **Test** > **Windows** > **Test Explorer**), Visual Studio discovers and displays tests. If tests aren't showing initially, then rebuild the project to refresh the list.
459474

460475
![Screenshot of Test Explorer.](../javascript/media/UnitTestsDiscoveryMocha.png)
461476

462477
> [!NOTE]
463-
> For TypeScript, do not use the `outdir` or `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests.
478+
> For TypeScript, don't use the `outdir` or `outfile` option in *tsconfig.json*, because Test Explorer won't be able to find your unit tests.
464479
465480
## Run tests (Node.js)
466481

0 commit comments

Comments
 (0)