【问题标题】:can't find package.json when running cypress test in azure pipeline yaml在 azure 管道 yaml 中运行 cypress 测试时找不到 package.json
【发布时间】:2020-09-02 01:00:07
【问题描述】:

我正在尝试使用 YAML 在 Azure Pipeline 作业中运行 cypress 测试。当管道运行时,它会到达 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-devops azure-pipelines cypress


    【解决方案1】:

    任务搜索根文件夹中的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
    

    【讨论】:

    • 感谢您的帮助。我添加了您建议的代码,但现在我收到了这个新错误:npm ERR! missing script: cypress
    • 我的猜测是您需要检查您的安装步骤,以便它位于您运行它的同一位置
    • 我想通了。我的 cypress run 命令与 package.json 中的 cypress run 命令不匹配。测试现在在管道中运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-23
    • 2019-11-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    相关资源
    最近更新 更多