microsoft / microsoft/nodejstools

Unable to detect unit tests in project with multiple package.json files

Open
#2,486 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
1.8k
Forks
355
PR merge metrics
No merged PRs in 30d

Description

Expected Behavior

Unit tests are found when multiple package.json files exist in the project.

Actual Behavior
  • NTVS Version: 1.5.10610.1
  • Visual Studio Version: 2022 17.7.6
  • Node.js Version: 20.8.1
Steps to Reproduce
  1. Create a project using the ASP.NET Core with Angular template
  2. Add a package.json at the root level of the project, now the folder structure should look as follows (excerpt):
    MyProject/
    ├─ ClientApp/
    │  ├─ package.json
    ├─ Controllers/
    ├─ Pages/
    ├─ wwwroot/
    ├─ package.json
    ├─ Program.cs
    
  3. Edit the project configuration for unit testing with Karma/Jasmine:
<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
  	  <TargetFramework>net7.0</TargetFramework>
  	  <Nullable>enable</Nullable>
  	  <IsPackable>false</IsPackable>
  	  <SpaRoot>ClientApp\</SpaRoot>
  	  <SpaProxyServerUrl>https://localhost:44445</SpaProxyServerUrl>
  	  <SpaProxyLaunchCommand>npm start</SpaProxyLaunchCommand>
  	  <ImplicitUsings>enable</ImplicitUsings>

  	  <!-- Unit tests -->
  	  <JavaScriptTestRoot>ClientApp\</JavaScriptTestRoot>
  	  <JavaScriptTestFramework>Jasmine</JavaScriptTestFramework>
  	  <GenerateProgramFile>false</GenerateProgramFile>
    </PropertyGroup>

    <ItemGroup>
  	  <PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="7.0.13" />
  	  <PackageReference Include="Microsoft.JavaScript.UnitTest" Version="1.5.10610.1">
  		  <PrivateAssets>all</PrivateAssets>
  		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  	  </PackageReference>
    </ItemGroup>

    <ItemGroup>
  	  <!-- Don't publish the SPA source files, but do show them in the project files list -->
  	  <Content Remove="$(SpaRoot)**" />
  	  <None Remove="$(SpaRoot)**" />
  	  <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
    </ItemGroup>

    <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
  	  <!-- Ensure Node.js is installed -->
  	  <Exec Command="node --version" ContinueOnError="true">
  		  <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
  	  </Exec>
  	  <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
  	  <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
  	  <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    </Target>

    <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
  	  <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
  	  <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  	  <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --configuration production" />

  	  <!-- Include the newly-built files in the publish output -->
  	  <ItemGroup>
  		  <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
  		  <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
  			  <RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
  			  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  			  <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
  		  </ResolvedFileToPublish>
  	  </ItemGroup>
    </Target>
</Project>
  1. Reload the project
  2. Notice the Test Explorer is unable to find any tests

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the ASP.NET Core with Angular project described in the issue, including the root and ClientApp package.json files and the JavaScriptTestRoot configuration. Inspect how the Microsoft.JavaScript.UnitTest integration discovers tests for the project and verify the result in Test Explorer. Done means Karma/Jasmine unit tests are detected with both package.json files present.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, csharp, javascript, node.js
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.