首页 > 解决方案 > 在 azure 管道 yaml 中运行 cypress 测试时找不到 package.json

问题描述

我正在尝试使用 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 命令不匹配。我更新了 cypress 运行步骤,现在测试在管道中运行。

标签: azure-devopsazure-pipelinescypress

解决方案


该任务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

推荐阅读