簡體   English   中英

如何使用代碼 0 退出“npm run start”?

[英]How do I make "npm run start" exit with a code 0?

我正在使用使用 React 和 JS 構建的應用程序。 使用 Azure DevOps 構建 CI/CD 管道,以構建、測試應用程序並將其部署到 Azure 服務。

在我的 CI 管道中,我需要運行使用 Cypress 編寫的功能測試。 “e2e:local”測試在 localhost 上托管的應用程序上運行。 單獨運行腳本“npm start”需要在終端中手動輸入 (Ctrl+C) 以退出進程,因此在管道中作為獨立步驟運行“npm start”將使其永遠運行。 因此,我使用“npm concurrently”來運行“npm run start”和功能測試“npm run e2e”local”。

"scripts": {
        "nx": "nx",
        "start": "nx serve",
        "build": "nx build",
        "test": "nx test",
        "e2e:local": "nx run haven-e2e:e2e --baseUrl=http://localhost:4200",
        "e2e:localhost": "concurrently --kill-others \"npm run start\" \"npm run e2e:local\"
} 

在我的本地機器上運行“npm run e2e:localhost”,在 localhost:4200 上為應用程序提供服務並運行功能測試。 下面的日志來自在本地機器上運行的腳本。

> Executing task: npm run e2e:localhost <
        
        
        > didit-tech-challenge@0.0.0 e2e:localhost
        > concurrently --success -k "npm run start" "npm run e2e:local"
        
        [0] 
        [0] > challenge@0.0.0 start
        [0] > nx serve
        [0]
        [1]
        [1] > challenge@0.0.0 e2e:local
        [1] > nx run haven-e2e:e2e --baseUrl=http://localhost:4200
        [1]
        [0] 
        [0] > nx run haven:serve 
        [1] 
        [1] > nx run haven-e2e:e2e --baseUrl=http://localhost:4200 
        [1] 
        [0] Starting type checking service...
        [0] Using 6 workers with 2048MB memory limit
        [0] 
        [0] >  NX  Web Development Server is listening at http://localhost:4200/
        [0]
        [1] 
        [1] ====================================================================================================
        [1]
        [1]   (Run Starting)
        [1]
        [1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
        [1]   │ Cypress:    8.2.0                                                                              │
        [1]   │ Browser:    Electron 89                                                                        │
        [1]   │ Specs:      1 found (functional/login.spec.ts)                                                 │
        [1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
        [1]
        [1]
        [1] ────────────────────────────────────────────────────────────────────────────────────────────────────
        [1]
        [1]   Running:  functional/login.spec.ts                                                        (1 of 1)
        [0] No type errors found
        [0] Version: typescript 4.2.4
        [0] Time: 14681ms
        [0] Hash: 7f72c1685a3c8c11842f
        [0] Built at: 12/02/2021 8:12:01 AM
        [0] Entrypoint main [big] = runtime.js runtime.js.map vendor.js main.js main.js.map
        [0] Entrypoint polyfills [big] = runtime.js runtime.js.map polyfills.js polyfills.js.map
        [0] Entrypoint styles [big] = runtime.js runtime.js.map styles.js styles.js.map
        [0] chunk {main} main.js, main.js.map (main) 696 KiB ={runtime}= ={vendor}= [initial] [rendered]
        [0] chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 654 KiB ={runtime}= [initial] [rendered]
        [0] chunk {runtime} runtime.js, runtime.js.map (runtime) 0 bytes ={main}= ={polyfills}= ={styles}= ={vendor}= [entry] [rendered]
        [0] chunk {styles} styles.js, styles.js.map (styles) 1.87 MiB ={runtime}= [initial] [rendered]
        [0] chunk {vendor} vendor.js (vendor) 1.42 MiB ={main}= ={runtime}= [initial] [rendered] split chunk (cache group: vendor) (name: vendor)
        [1] Starting type checking service...
        [1] Using 1 worker with 2048MB memory limit
        [1] Starting type checking service...
        [1] Using 1 worker with 2048MB memory limit
        [0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
        [0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
        [0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
        [0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
        [0] [HPM] Error occurred while trying to proxy request /login from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
        [1] Warning: We failed processing this video.
        [1]
        [1] This error will not alter the exit code.
        [1]
        [1] TimeoutError: operation timed out
        [1]     at afterTimeout (C:\Users\AppData\Local\Cypress\Cache\8.2.0\Cypress\resources\app\packages\server\node_modules\bluebird\js\release\timers.js:46:19)
        [1]     at Timeout.timeoutTimeout [as _onTimeout] (C:\Users\AppData\Local\Cypress\Cache\8.2.0\Cypress\resources\app\packages\server\node_modules\bluebird\js\release\timers.js:76:13)     
        [1]     at listOnTimeout (internal/timers.js:554:17)
        [1]     at processTimers (internal/timers.js:497:7)
        [1]
        [1]
        [1]   (Results)
        [1]
        [1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
        [1]   │ Tests:        1                                                                                │
        [1]   │ Passing:      1                                                                                │
        [1]   │ Failing:      0                                                                                │
        [1]   │ Pending:      0                                                                                │
        [1]   │ Skipped:      0                                                                                │
        [1]   │ Screenshots:  0                                                                                │
        [1]   │ Video:        false                                                                            │
        [1]   │ Duration:     2 seconds                                                                        │
        [1]   │ Spec Ran:     functional/login.spec.ts                                                         │
        [1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
        [1] 
        [1] 
        [1] ====================================================================================================
        [1]
        [1]   (Run Finished)
        [1]
        [1]
        [1]        Spec                                              Tests  Passing  Failing  Pending  Skipped  
        [1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
        [1]   │ √  functional/login.spec.ts                 00:02        1        1        -        -        - │
        [1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
        [1]     √  All specs passed!                        00:02        1        1        -        -        -  
        [1]
        [1] 
        [1] ———————————————————————————————————————————————
        [1]
        [1] >  NX   SUCCESS  Running target "e2e" succeeded
        [1]
        [1]
        [1] npm run e2e:local exited with code 0
        --> Sending SIGTERM to other processes..
        [0] npm run start exited with code 1
        The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run e2e:localhost" terminated with exit code: 1.
        
        Terminal will be reused by tasks, press any key to close it.

從日志中,作為“npm run start exited with code 1”,在 Azure DevOps CI 管道中運行相同的 npm 腳本,它會導致進程失敗。 以下日志來自在 Azure DevOps Pipeline 上運行的腳本。 如何使用代碼 0 退出“npm run start”?

Starting: npm haven-e2e:local
==============================================================================
Task         : npm
Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
Version      : 1.187.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm
==============================================================================
/usr/local/bin/npm --version
8.1.0
/usr/local/bin/npm config list
; "env" config from environment

userconfig = "/home/vsts/work/1/npm/409.npmrc" 

; node bin location = /usr/local/bin/node
; cwd = /home/vsts/work/1/s
; HOME = /home/vsts
; Run `npm config ls -l` to show all defaults.
/usr/local/bin/npm run e2e:localhost

> didit-tech-challenge@0.0.0 e2e:localhost
> concurrently --kill-others "npm run start" "npm run e2e:local"

[1] 
[1] > didit-tech-challenge@0.0.0 e2e:local
[1] > nx run haven-e2e:e2e --baseUrl=http://localhost:4200
[1] 
[0] 
[0] > didit-tech-challenge@0.0.0 start
[0] > nx serve
[0] 
[1] 
[1] > nx run haven-e2e:e2e --baseUrl=http://localhost:4200 
[0] 
[0] > nx run haven:serve 
[1] It looks like this is your first time using Cypress: 8.2.0
[1] 
[1] [STARTED] Task without title.
[0] Starting type checking service...
[0] Using 1 worker with 2048MB memory limit
[0] (node:1989) [DEP0111] DeprecationWarning: Access to process.binding('http_parser') is deprecated.
[0] (Use `node --trace-deprecation ...` to show where the warning was created)
[0] 
[0] >  NX  Web Development Server is listening at http://localhost:4200/
[0] 
[0] (node:1989) [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./" in the "exports" field module resolution of the package at /home/vsts/work/1/s/node_modules/postcss/package.json.
[0] Update this package.json to use a subpath pattern like "./*".
[1] [SUCCESS] Task without title.
[1] 
[1] Opening Cypress...
[0] No type errors found
[0] Version: typescript 4.2.4
[0] Time: 12030ms
[0] Hash: c9337ccd5e2d5b163cd1
[0] Built at: 12/02/2021 10:45:15 AM
[0] Entrypoint main [big] = runtime.js runtime.js.map vendor.js main.js main.js.map
[0] Entrypoint polyfills [big] = runtime.js runtime.js.map polyfills.js polyfills.js.map
[0] Entrypoint styles [big] = runtime.js runtime.js.map styles.js styles.js.map
[0] chunk {main} main.js, main.js.map (main) 695 KiB ={runtime}= ={vendor}= [initial] [rendered]
[0] chunk {polyfills} polyfills.js, polyfills.js.map (polyfills) 654 KiB ={runtime}= [initial] [rendered]
[0] chunk {runtime} runtime.js, runtime.js.map (runtime) 0 bytes ={main}= ={polyfills}= ={styles}= ={vendor}= [entry] [rendered]
[0] chunk {styles} styles.js, styles.js.map (styles) 1.87 MiB ={runtime}= [initial] [rendered]
[0] chunk {vendor} vendor.js (vendor) 1.42 MiB ={main}= ={runtime}= [initial] [rendered] split chunk (cache group: vendor) (name: vendor)
[1] 
[1] ================================================================================
[1] 
[1]   (Run Starting)
[1] 
[1] tput: No value for $TERM and no -T specified
[1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
[1]   │ Cypress:    8.2.0                                                                              │
[1]   │ Browser:    Electron 89                                                                        │
[1]   │ Specs:      1 found (functional/login.spec.ts)                                                 │
[1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
[1] 
[1] 
[1] ────────────────────────────────────────────────────────────────────────────────────────────────────
[1]
[1]   Running:  functional/login.spec.ts                                                        (1 of 1)
[1] Starting type checking service...
[1] Using 1 worker with 2048MB memory limit
[1] Starting type checking service...
[1] Using 1 worker with 2048MB memory limit
[0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[0] [HPM] Error occurred while trying to proxy request /health from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[0] [HPM] Error occurred while trying to proxy request /login from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
[1] 
[1]   (Results)
[1] 
[1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
[1]   │ Tests:        1                                                                                │
[1]   │ Passing:      1                                                                                │
[1]   │ Failing:      0                                                                                │
[1]   │ Pending:      0                                                                                │
[1]   │ Skipped:      0                                                                                │
[1]   │ Screenshots:  0                                                                                │
[1]   │ Video:        true                                                                             │
[1]   │ Duration:     3 seconds                                                                        │
[1]   │ Spec Ran:     functional/login.spec.ts                                                         │
[1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
[1] 
[1] 
[1]   (Video)
[1] 
[1]   -  Started processing:  Compressing to 32 CRF                                                     
[1]   -  Finished processing: /home/vsts/work/1/s/dist/cypress/apps/haven-e2e/videos/func    (2 seconds)
[1]                           tional/login.spec.ts.mp4                                                  
[1] 
[1] 
[1] ================================================================================
[1] 
[1]   (Run Finished)
[1] 
[1] 
[1] tput: No value for $TERM and no -T specified
[1]        Spec                                              Tests  Passing  Failing  Pending  Skipped  
[1]   ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
[1]   │ ✔  functional/login.spec.ts                 00:03        1        1        -        -        - │
[1]   └────────────────────────────────────────────────────────────────────────────────────────────────┘
[1]     ✔  All specs passed!                        00:03        1        1        -        -        -  
[1] 
[1] 
[1] ———————————————————————————————————————————————
[1] 
[1] >  NX   SUCCESS  Running target "e2e" succeeded
[1] 
[1] 
[1] npm run e2e:local exited with code 0
--> Sending SIGTERM to other processes..
[0] 
[0] ———————————————————————————————————————————————
[0] 
[0] >  NX   ERROR  Running target "haven:serve" failed
[0] 
[0]   Failed tasks:
[0]   
[0]   - haven:serve
[0] 
[0] npm run start exited with code SIGTERM
##[warning]Couldn't find a debug log in the cache or working directory
##[error]Error: Npm failed with return code: 1
Finishing: npm haven-e2e:local

我能夠使用“ --success = first ”修復此腳本。 這將根據“第一個”子節點終止(npm run e2e:local)的成功或失敗返回退出代碼 0 或退出代碼 1。

"e2e:localhost": "concurrently --kill-others --success= first \"npm run start\" \"npm run e2e:local\"

感謝@Lawrence Cherone的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM