1+ import { writeFile } from 'node:fs/promises' ;
12import path from 'node:path' ;
23import { expect , test } from '@rstest/core' ;
34import { resolveRelatedTests } from '../../src/relatedTests.ts' ;
45
6+ const getOutputFile = ( args : string [ ] ) : string => {
7+ const outputFile = args [ args . indexOf ( '--json' ) + 1 ] ;
8+ if ( outputFile === undefined ) throw new Error ( 'Missing related-test output file.' ) ;
9+ return outputFile ;
10+ } ;
11+
512test ( 'lists related tests through the current Rstack CLI and normalizes its JSON result' , async ( ) => {
613 const calls : unknown [ ] = [ ] ;
714 const packageRoot = path . resolve ( '/workspace/packages/app' ) ;
@@ -16,13 +23,14 @@ test('lists related tests through the current Rstack CLI and normalizes its JSON
1623 {
1724 runCli : ( request ) => {
1825 calls . push ( request ) ;
19- return Promise . resolve ( {
20- stdout : JSON . stringify ( [
26+ const outputFile = getOutputFile ( request . args ) ;
27+ return writeFile (
28+ outputFile ,
29+ JSON . stringify ( [
2130 { file : 'tests/index.test.ts' , type : 'file' } ,
2231 { file : path . join ( packageRoot , 'tests/index.test.ts' ) , type : 'file' } ,
2332 ] ) ,
24- stderr : '' ,
25- } ) ;
33+ ) . then ( ( ) => ( { stdout : 'config log that is not JSON' , stderr : '' } ) ) ;
2634 } ,
2735 } ,
2836 ) ;
@@ -38,6 +46,7 @@ test('lists related tests through the current Rstack CLI and normalizes its JSON
3846 path . join ( packageRoot , 'src/index.ts' ) ,
3947 '--filesOnly' ,
4048 '--json' ,
49+ expect . stringMatching ( / r s t a c k - r e l a t e d - t e s t s - / u) ,
4150 '--config' ,
4251 configPath ,
4352 ] ,
@@ -50,7 +59,13 @@ test('reports invalid related-test JSON as an Rstest list failure', async () =>
5059 await expect (
5160 resolveRelatedTests (
5261 { packageRoot : '/workspace' , sources : [ '/workspace/src/index.ts' ] } ,
53- { runCli : ( ) => Promise . resolve ( { stdout : 'not json' , stderr : 'list failed' } ) } ,
62+ {
63+ runCli : ( request ) =>
64+ writeFile ( getOutputFile ( request . args ) , 'not json' ) . then ( ( ) => ( {
65+ stdout : '' ,
66+ stderr : 'list failed' ,
67+ } ) ) ,
68+ } ,
5469 ) ,
5570 ) . rejects . toThrow ( 'Rstest related-test listing returned invalid JSON' ) ;
5671} ) ;
0 commit comments