|
1 | | -import * as fs from 'fs'; |
2 | 1 | import * as os from 'os'; |
3 | 2 | import * as path from 'path'; |
| 3 | +import * as fs from 'fs-extra'; |
4 | 4 | import { IoHelper } from '../../../lib/api-private'; |
5 | 5 | import { CliIoHost } from '../../../lib/cli/io-host'; |
6 | 6 | import { FileTelemetrySink } from '../../../lib/cli/telemetry/file-sink'; |
@@ -69,10 +69,8 @@ describe('FileTelemetrySink', () => { |
69 | 69 | await client.emit(testEvent); |
70 | 70 |
|
71 | 71 | // THEN |
72 | | - expect(fs.existsSync(logFilePath)).toBe(true); |
73 | | - const fileContent = fs.readFileSync(logFilePath, 'utf8'); |
74 | | - const parsedContent = JSON.parse(fileContent); |
75 | | - expect(parsedContent).toEqual(testEvent); |
| 72 | + const fileJson = fs.readJSONSync(logFilePath, 'utf8'); |
| 73 | + expect(fileJson).toEqual([testEvent]); |
76 | 74 | }); |
77 | 75 |
|
78 | 76 | test('appends data to a file', async () => { |
@@ -116,11 +114,9 @@ describe('FileTelemetrySink', () => { |
116 | 114 |
|
117 | 115 | // THEN |
118 | 116 | expect(fs.existsSync(logFilePath)).toBe(true); |
119 | | - const fileContent = fs.readFileSync(logFilePath, 'utf8'); |
120 | | - |
121 | | - // The file should contain two JSON objects, each pretty-printed with a newline |
122 | | - const expectedSingleEvent = JSON.stringify(testEvent, null, 2) + '\n'; |
123 | | - expect(fileContent).toBe(expectedSingleEvent + expectedSingleEvent); |
| 117 | + const fileJson = fs.readJSONSync(logFilePath, 'utf8'); |
| 118 | + const expectedJson = [testEvent, testEvent]; |
| 119 | + expect(fileJson).toEqual(expectedJson); |
124 | 120 | }); |
125 | 121 |
|
126 | 122 | test('constructor throws if file already exists', async () => { |
@@ -178,8 +174,8 @@ describe('FileTelemetrySink', () => { |
178 | 174 |
|
179 | 175 | const client = new FileTelemetrySink({ logFilePath, ioHost }); |
180 | 176 |
|
181 | | - // Mock fs.appendFileSync to throw an error |
182 | | - jest.spyOn(fs, 'appendFileSync').mockImplementation(() => { |
| 177 | + // Mock fs.writeJSONSync to throw an error |
| 178 | + jest.spyOn(fs, 'writeJSONSync').mockImplementation(() => { |
183 | 179 | throw new Error('File write error'); |
184 | 180 | }); |
185 | 181 |
|
|
0 commit comments