0
votes

I'm trying to run a cypress test in an Azure Pipeline job using YAML. When the pipeline runs, it gets to the line where the cypress test runs and throws this error:

npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path C:\myagent\_work\5\s\package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'C:\myagent\_work\5\s\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

Here is my YAML file:

trigger:
  - master

pool:
    name: Default

variables:
    buildConfiguration: 'Release'

steps:
  - task: NodeTool@0
    displayName: 'Use Node 12.x'
    inputs:
        versionSpec: 12.18.3
        checkLatest: false
  
  - task: Npm@1
    displayName: 'npm install'
    inputs:
        workingDir: WebApp
        verbose: false

  - task: Npm@1
    displayName: 'Build Angular'
    inputs:
        command: custom
        customCommand: run build -- --prod
        workingDir: WebApp
  
  - task: UseDotNet@2
    displayName: 'Use .NET Core SDK 3.1.300'
    inputs:
        packageType: sdk
        version: 3.1.300
  
  - task: DotNetCoreCLI@2
    displayName: 'dotnet restore'
    inputs:
        command: restore
        projects: AppApi/AppApi.csproj
        vstsFeed: '*************'

  - task: DotNetCoreCLI@2
    displayName: 'dotnet publish'
    inputs:
        command: publish
        arguments: '--no-restore --configuration $(buildConfiguration) --verbosity quiet --output $(build.artifactstagingdirectory)'
        publishWebProjects: true
        zipAfterPublish: true

  - script: 'npx cypress verify'
    displayName: 'cypress verify'
    failOnStderr: true

  - script: 'npm run cy:run --headless --spec cypress/integration/TestDemoExpanded.spec.ts --project ./WebApp' 
    displayName: 'run cypress tests'
    workingDirectory: $(Build.SourcesDirectory)/WebApp

My package.json is checked into my Git repository and is located in my WebApp subfolder. How do I solve this error?

UPDATED: I added the workingDirectory parameter as suggested but now I have this new error: npm ERR! missing script: cypress

I solved the 'missing script' error. The cypress run command I had in my YAML file didn't match the cypress run command I had in my package.json. I updated the cypress run step and now the tests run in the pipeline.

1

1 Answers

1
votes

The task searches the package.json in the root folder and not in WebApp.

Just add:

workingDirectory: $(Build.SourcesDirectory)/WebApp

To the cypress step:

- script: 'npm run cypress run --headless --spec cypress/integration/TestDemoExpanded.spec.ts --project ./WebApp' 
  displayName: 'run cypress tests'
  workingDirectory: $(Build.SourcesDirectory)/WebApp