1- import { vol } from 'memfs' ;
21import { MEMFS_VOLUME } from '@code-pushup/test-utils' ;
32import * as utils from '@code-pushup/utils' ;
43import type {
@@ -16,42 +15,15 @@ describe('nxHandler', () => {
1615 nxProjectsFilter : '--with-target={task}' ,
1716 } ;
1817
19- describe ( 'isConfigured' , ( ) => {
20- it ( 'should detect Nx when nx.json exists and `nx report` succeeds' , async ( ) => {
21- vol . fromJSON ( { 'nx.json' : '{}' } , MEMFS_VOLUME ) ;
22- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
23- code : 0 ,
24- stdout : 'NX Report complete - copy this into the issue template' ,
25- } as utils . ProcessResult ) ;
26-
27- await expect ( nxHandler . isConfigured ( options ) ) . resolves . toBeTrue ( ) ;
28- } ) ;
29-
30- it ( "should NOT detect Nx when nx.json doesn't exist" , async ( ) => {
31- vol . fromJSON ( { 'turbo.json' : '{}' } , MEMFS_VOLUME ) ;
32- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
33- code : 0 ,
34- } as utils . ProcessResult ) ;
35-
36- await expect ( nxHandler . isConfigured ( options ) ) . resolves . toBeFalse ( ) ;
37- } ) ;
38-
39- it ( 'should NOT detect Nx when `nx report` fails with non-zero exit code' , async ( ) => {
40- vol . fromJSON ( { 'nx.json' : '' } , MEMFS_VOLUME ) ;
41- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
42- code : 1 ,
43- stderr : 'Error: ValueExpected in nx.json' ,
44- } as utils . ProcessResult ) ;
45-
46- await expect ( nxHandler . isConfigured ( options ) ) . resolves . toBeFalse ( ) ;
47- } ) ;
48- } ) ;
49-
5018 describe ( 'listProjects' , ( ) => {
19+ const nxReportSuccess = { code : 0 } as utils . ProcessResult ;
20+
5121 beforeEach ( ( ) => {
52- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
53- stdout : '["backend","frontend"]' ,
54- } as utils . ProcessResult ) ;
22+ vi . spyOn ( utils , 'executeProcess' )
23+ . mockResolvedValueOnce ( nxReportSuccess )
24+ . mockResolvedValueOnce ( {
25+ stdout : '["backend","frontend"]' ,
26+ } as utils . ProcessResult ) ;
5527 } ) ;
5628
5729 it ( 'should list projects from `nx show projects`' , async ( ) => {
@@ -95,20 +67,39 @@ describe('nxHandler', () => {
9567 } satisfies utils . ProcessConfig ) ;
9668 } ) ;
9769
70+ it ( 'should throw if `nx report` fails' , async ( ) => {
71+ vi . spyOn ( utils , 'executeProcess' )
72+ . mockReset ( )
73+ . mockResolvedValueOnce ( {
74+ code : 1 ,
75+ stderr : 'Error: ValueExpected in nx.json' ,
76+ } as utils . ProcessResult ) ;
77+
78+ await expect ( nxHandler . listProjects ( options ) ) . rejects . toThrow (
79+ "'nx report' failed with exit code 1 - Error: ValueExpected in nx.json" ,
80+ ) ;
81+ } ) ;
82+
9883 it ( 'should throw if `nx show projects` outputs invalid JSON' , async ( ) => {
99- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
100- stdout : 'backend\nfrontend\n' ,
101- } as utils . ProcessResult ) ;
84+ vi . spyOn ( utils , 'executeProcess' )
85+ . mockReset ( )
86+ . mockResolvedValueOnce ( nxReportSuccess )
87+ . mockResolvedValueOnce ( {
88+ stdout : 'backend\nfrontend\n' ,
89+ } as utils . ProcessResult ) ;
10290
10391 await expect ( nxHandler . listProjects ( options ) ) . rejects . toThrow (
10492 "Invalid non-JSON output from 'nx show projects' - SyntaxError: Unexpected token" ,
10593 ) ;
10694 } ) ;
10795
10896 it ( "should throw if `nx show projects` JSON output isn't array of strings" , async ( ) => {
109- vi . spyOn ( utils , 'executeProcess' ) . mockResolvedValue ( {
110- stdout : '"backend"' ,
111- } as utils . ProcessResult ) ;
97+ vi . spyOn ( utils , 'executeProcess' )
98+ . mockReset ( )
99+ . mockResolvedValueOnce ( nxReportSuccess )
100+ . mockResolvedValueOnce ( {
101+ stdout : '"backend"' ,
102+ } as utils . ProcessResult ) ;
112103
113104 await expect ( nxHandler . listProjects ( options ) ) . rejects . toThrow (
114105 'Invalid JSON output from \'nx show projects\', expected array of strings, received "backend"' ,
0 commit comments