6
6
7
7
import assert from 'assert/strict' ;
8
8
import fs from 'fs' ;
9
+ import path from 'path' ;
9
10
10
11
import { readJson } from '../../../core/test/test-utils.js' ;
11
12
import * as Printer from '../../printer.js' ;
@@ -34,11 +35,9 @@ describe('Printer', () => {
34
35
} ) ;
35
36
36
37
it ( 'throws for invalid paths' , ( ) => {
37
- const path = '! /#@.json' ;
38
+ const path = '/ /#@.json' ;
38
39
const report = JSON . stringify ( sampleResults ) ;
39
- return Printer . write ( report , 'html' , path ) . catch ( err => {
40
- assert . ok ( err . code === 'ENOENT' ) ;
41
- } ) ;
40
+ return assert . rejects ( Printer . write ( report , 'html' , path ) ) ;
42
41
} ) ;
43
42
44
43
it ( 'returns output modes' , ( ) => {
@@ -49,4 +48,20 @@ describe('Printer', () => {
49
48
assert . strictEqual ( typeof mode , 'string' ) ;
50
49
} ) ;
51
50
} ) ;
51
+
52
+ it ( 'creates missing directories when writing to file' , ( ) => {
53
+ const dirPath = './non/existent/directory/.test-file.json' ;
54
+ const report = JSON . stringify ( sampleResults ) ;
55
+ const dir = path . dirname ( dirPath ) ;
56
+ if ( fs . existsSync ( dir ) ) {
57
+ fs . rmdirSync ( dir , { recursive : true } ) ;
58
+ }
59
+ return Printer . write ( report , 'json' , dirPath ) . then ( _ => {
60
+ assert . ok ( fs . existsSync ( dir ) , `Directory ${ dir } should exist now` ) ;
61
+ const fileContents = fs . readFileSync ( dirPath , 'utf8' ) ;
62
+ assert . ok ( / l i g h t h o u s e V e r s i o n / gim. test ( fileContents ) ) ;
63
+ fs . unlinkSync ( dirPath ) ;
64
+ fs . rmdirSync ( dir , { recursive : true } ) ;
65
+ } ) ;
66
+ } ) ;
52
67
} ) ;
0 commit comments