在Azure管道yaml中运行cypress测试时找不到package.json

时间:2020-09-01 13:27:41

标签: azure-devops azure-pipelines cypress

我正在尝试使用YAML在Azure Pipeline作业中运行赛普拉斯测试。当管道运行时,它到达了cypress测试运行的行并抛出此错误:

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 

这是我的YAML文件:

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

我的package.json已签入我的Git存储库中,并且位于我的WebApp子文件夹中。我该如何解决这个错误?

已更新: 我按照建议添加了workingDirectory参数,但现在出现了这个新错误:npm ERR! missing script: cypress

我解决了“脚本丢失”错误。我在YAML文件中使用的cypress run命令与我在package.json中使用的cypress run命令不匹配。我更新了赛普拉斯的运行步骤,现在测试在管道中运行。

1 个答案:

答案 0 :(得分:0)

任务在根文件夹中而不是在package.json中搜索WebApp

只需添加:

workingDirectory: $(Build.SourcesDirectory)/WebApp

进入cypress步骤:

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