-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logs): enhance unsupported type logs (#513)
* feat(logs): enhance unsupported type logs by adding source file where the mock is created and source file where the type is failing * feat(logs): use template literals instead of string composition
- Loading branch information
Showing
16 changed files
with
207 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import { Format, TransformableInfo } from 'logform'; | ||
import { TransformableInfo } from 'logform'; | ||
import * as winston from 'winston'; | ||
import { FileTransportInstance } from 'winston/lib/winston/transports'; | ||
|
||
let winstonFileLogger: FileTransportInstance; | ||
|
||
export function FileLogger(): FileTransportInstance { | ||
const customFormat: Format = winston.format.printf( | ||
(info: TransformableInfo) => | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
`${info.timestamp} - ${info.level}: ${info.message}` | ||
return ( | ||
winstonFileLogger || | ||
(winstonFileLogger = new winston.transports.File({ | ||
filename: 'tsAutoMock.log', | ||
options: { flags: 'w' }, | ||
level: 'error', | ||
format: winston.format.combine( | ||
winston.format((info: TransformableInfo) => { | ||
info.level = info.level.toUpperCase(); | ||
return info; | ||
})(), | ||
winston.format.simple(), | ||
winston.format.timestamp(), | ||
winston.format.printf( | ||
(info: TransformableInfo) => | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions | ||
`${info.timestamp} - ${info.level}: ${info.message}` | ||
) | ||
), | ||
})) | ||
); | ||
|
||
return new winston.transports.File({ | ||
filename: 'tsAutoMock.log', | ||
level: 'error', | ||
format: winston.format.combine( | ||
winston.format((info: TransformableInfo) => { | ||
info.level = info.level.toUpperCase(); | ||
return info; | ||
})(), | ||
winston.format.simple(), | ||
winston.format.timestamp(), | ||
customFormat | ||
), | ||
}); | ||
} |
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,11 @@ | ||
import ts from 'typescript'; | ||
|
||
let currentCreateMock: ts.Node; | ||
|
||
export const GetCurrentCreateMock: () => ts.Node = () => currentCreateMock; | ||
|
||
export const SetCurrentCreateMock: (node: ts.Node) => void = ( | ||
node: ts.Node | ||
) => { | ||
currentCreateMock = node; | ||
}; |
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,11 @@ | ||
export type ConditionalType<T> = T extends string | ||
? 'string' | ||
: T extends number | ||
? 'number' | ||
: T extends boolean | ||
? 'boolean' | ||
: T extends undefined | ||
? 'undefined' | ||
: T extends Function | ||
? 'function' | ||
: 'object'; |
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,27 @@ | ||
import { createMock } from 'ts-auto-mock'; | ||
import { ConditionalType } from './conditionalType'; | ||
import { getLogsByCreateMockFileName, UnsupportedTypeLog } from './utils/log'; | ||
|
||
describe('Warning', () => { | ||
it('should log an unsupported type warning', () => { | ||
const logs: UnsupportedTypeLog[] = getLogsByCreateMockFileName( | ||
'createMock.warning.test.ts' | ||
); | ||
interface InterfaceWithConditionalType { | ||
conditional: ConditionalType<string>; | ||
} | ||
|
||
createMock<InterfaceWithConditionalType>(); | ||
expect(logs.length).toBe(1); | ||
|
||
expect(logs[0].header).toContain( | ||
'WARNING: Transformer - Not supported type: ConditionalType - it will convert to null' | ||
); | ||
expect(logs[0].created).toMatch( | ||
/created file:\/\/.*createMock.warning\.test\.ts:[0-9]*:[0-9]*/ | ||
); | ||
expect(logs[0].usedBy).toMatch( | ||
/used by file:\/\/.*conditionalType\.ts:[0-9]*:[0-9]*/ | ||
); | ||
}); | ||
}); |
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,27 @@ | ||
import { createMockList } from 'ts-auto-mock'; | ||
import { ConditionalType } from './conditionalType'; | ||
import { getLogsByCreateMockFileName, UnsupportedTypeLog } from './utils/log'; | ||
|
||
describe('Warning', () => { | ||
it('should log an unsupported type warning', () => { | ||
const logs: UnsupportedTypeLog[] = getLogsByCreateMockFileName( | ||
'createMockList.warning.test.ts' | ||
); | ||
interface InterfaceWithConditionalType { | ||
conditional: ConditionalType<string>; | ||
} | ||
|
||
createMockList<InterfaceWithConditionalType>(2); | ||
expect(logs.length).toBe(1); | ||
|
||
expect(logs[0].header).toContain( | ||
'WARNING: Transformer - Not supported type: ConditionalType - it will convert to null' | ||
); | ||
expect(logs[0].created).toMatch( | ||
/created file:\/\/.*createMockList.warning\.test\.ts:[0-9]*:[0-9]*/ | ||
); | ||
expect(logs[0].usedBy).toMatch( | ||
/used by file:\/\/.*conditionalType\.ts:[0-9]*:[0-9]*/ | ||
); | ||
}); | ||
}); |
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,5 @@ | ||
{ | ||
"spec_dir": "./test/logs", | ||
"spec_files": ["**/*test.ts"], | ||
"helpers": ["../reporter.js"] | ||
} |
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,12 @@ | ||
{ | ||
"extends": "../tsconfig/tsconfig.json", | ||
"compilerOptions": { | ||
"plugins": [ | ||
{ | ||
"transform": "./dist/transformer", | ||
"debug": "file", | ||
"cacheBetweenTests": false | ||
} | ||
] | ||
} | ||
} |
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 fs from 'fs'; | ||
|
||
export interface UnsupportedTypeLog { | ||
header: string; | ||
created: string; | ||
usedBy: string; | ||
} | ||
|
||
export const getLogsByCreateMockFileName: ( | ||
fileName: string | ||
) => UnsupportedTypeLog[] = (fileName: string) => { | ||
const logErrorFile: string = fs.readFileSync('./tsAutoMock.log', 'utf-8'); | ||
const logs: UnsupportedTypeLog[] = []; | ||
const lines: string[] = logErrorFile | ||
.split('\n') | ||
.filter((line: string) => !!line); | ||
|
||
for (let i: number = 0; i < lines.length; i += 3) { | ||
logs.push({ | ||
header: lines[i], | ||
created: lines[i + 1], | ||
usedBy: lines[i + 2], | ||
}); | ||
} | ||
|
||
return logs.filter( | ||
(log: UnsupportedTypeLog) => log.created.indexOf(fileName) > -1 | ||
); | ||
}; |