-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #393 from kulshekhar/logging
Logging
- Loading branch information
Showing
26 changed files
with
165 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
/** | ||
* Logger file that enables logging things just once. Does this by traversing the array of previously recorded | ||
* logs to see if the exact same message has already been logged | ||
* @type {any[]} | ||
*/ | ||
|
||
const logs: any[] = []; | ||
let logsFlushed: boolean = false; | ||
|
||
function shouldLog(): boolean { | ||
// If the env variable is set and the logs have not already been flushed, log the line | ||
return process.env.TS_JEST_DEBUG && !logsFlushed; | ||
} | ||
|
||
// Log function. Only logs prior to calls to flushLogs. | ||
export function logOnce(...thingsToLog: any[]) { | ||
if (!shouldLog()) { | ||
return; | ||
} | ||
logs.push(thingsToLog); | ||
} | ||
|
||
// This function JSONifies logs and flushes them to disk. | ||
export function flushLogs() { | ||
if (!shouldLog()) { | ||
return; // only output stuff for the first invocation and if logging is enabled. | ||
} | ||
logsFlushed = true; | ||
const rootPath = path.resolve(__dirname, '../'); | ||
const JSONifiedLogs = logs.map(convertToJSONIfPossible); | ||
const logString = JSONifiedLogs.join('\n'); | ||
const filePath = path.resolve(rootPath, 'debug.txt'); | ||
fs.writeFileSync(filePath, logString); | ||
} | ||
|
||
function includes<T>(array: T[], subject: T) { | ||
return array.indexOf(subject) !== -1; | ||
} | ||
|
||
function convertToJSONIfPossible(object: any): string { | ||
try { | ||
return JSON.stringify(object, null, 2); | ||
} catch { | ||
return object.toString(); // if unable to parse, simply return the string variant | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as fs from 'fs-extra'; | ||
import * as path from 'path'; | ||
import runJest from '../__helpers__/runJest'; | ||
|
||
const debugFilePath = path.resolve( | ||
__dirname, | ||
'../simple/node_modules/ts-jest/debug.txt', | ||
); | ||
|
||
describe('Debug output', () => { | ||
beforeEach(async () => { | ||
return fs.remove(debugFilePath); | ||
}); | ||
|
||
it('should create a debug file with the correct output if the flag is set', async () => { | ||
runJest('../simple', ['--no-cache', '-u'], { | ||
TS_JEST_DEBUG: 'true', | ||
}); | ||
const logFile = await fs.readFile(debugFilePath, 'utf-8'); | ||
|
||
expect(logFile).not.toBeNull(); | ||
}); | ||
|
||
it('Should not create a file if the debug flag is not set', async () => { | ||
runJest('../simple', ['--no-cache', '-u']); | ||
expect.assertions(1); // To make sure we actually do assert the promise on the line below | ||
await expect(fs.readFile(debugFilePath, 'utf-8')).rejects.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.