-
Notifications
You must be signed in to change notification settings - Fork 38
Handle 'true'/'false' stringified boolean options and better config error handling #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,118 @@ | ||
import { expect, jest } from '@jest/globals'; | ||
import logger from '../../transforms/helpers/log-helper'; | ||
|
||
/** | ||
* Spies on all logger log levels for messages matching those passed in the | ||
* config. | ||
* | ||
* @param callback The callback expected to trigger (or not) the logs. | ||
* @param config An optional object with an array of expected messages for each | ||
* log level. If no array is passed, no messages will be expected for that | ||
* level. If no object is passed, the function will expect that there are no | ||
* logs. | ||
*/ | ||
export function expectLogs( | ||
callback: () => void, | ||
{ | ||
info = [], | ||
warn = [], | ||
error = [], | ||
}: { | ||
info?: string[]; | ||
warn?: string[]; | ||
error?: string[]; | ||
} = {} | ||
): void { | ||
const infoConfig = { | ||
level: 'info' as const, | ||
expectedMessages: info, | ||
restoreAllMocks: false, | ||
}; | ||
const warnConfig = { | ||
level: 'warn' as const, | ||
expectedMessages: warn, | ||
restoreAllMocks: false, | ||
}; | ||
const errorConfig = { | ||
level: 'error' as const, | ||
expectedMessages: error, | ||
restoreAllMocks: true, | ||
}; | ||
|
||
expectLogLevel(() => { | ||
expectLogLevel(() => { | ||
expectLogLevel(callback, infoConfig); | ||
}, warnConfig); | ||
}, errorConfig); | ||
|
||
jest.restoreAllMocks(); | ||
} | ||
|
||
/** | ||
* Spies on the logger for messages matching those passed in the config. | ||
* | ||
* @param callback The callback expected to trigger (or not) the logs. | ||
* @param config An optional object with an specified log `level`, an array of | ||
* `expectedMessages` for that log level, and an option to run | ||
* `jest.restoreAllMocks()` after the callback and expectations are complete. | ||
* If no object is passed, will default to spying on the `'error'` log level, | ||
* expect that no messages are sent, and will restore all mocks after the test. | ||
*/ | ||
function expectLogLevel( | ||
callback: () => void, | ||
{ | ||
level = 'error', | ||
expectedMessages = [], | ||
restoreAllMocks = true, | ||
}: { | ||
level?: 'info' | 'warn' | 'error'; | ||
expectedMessages?: string[]; | ||
restoreAllMocks?: boolean; | ||
} = {} | ||
): void { | ||
const spy = jest.spyOn(logger, level); | ||
|
||
callback(); | ||
|
||
if (expectedMessages.length > 0) { | ||
expect(spy).toHaveBeenCalledTimes(expectedMessages.length); | ||
for (const [index, expectedError] of expectedMessages.entries()) { | ||
expect(spy).toHaveBeenNthCalledWith( | ||
index + 1, | ||
expect.stringMatching(expectedError) | ||
); | ||
} | ||
} else { | ||
expect(spy).not.toHaveBeenCalled(); | ||
} | ||
|
||
if (restoreAllMocks) { | ||
jest.restoreAllMocks(); | ||
} | ||
} | ||
|
||
/** | ||
* Makes a regexp pattern to match logs. String arguments passed to | ||
* `makeLogMatcher` will be escaped then merged together into a regexp that will | ||
* match partial lines of multi-line logs when paired with Jest | ||
* `expect.stringMatching`. | ||
* | ||
* @example | ||
* ``` | ||
* const expected = makeLogMatcher('Line 1', 'Line 2', '3') | ||
* //=> 'Line 1[\S\s]*Line 2[\S\s]*3' | ||
* | ||
* expect('Line 1\nLine 2\nLine 3').toEqual(expect.stringMatching(expected)); | ||
* //=> passes | ||
* ``` | ||
*/ | ||
export function makeLogMatcher(...parts: string[]): string { | ||
return parts.map(escapeRegExp).join('[\\S\\s]*'); | ||
} | ||
|
||
/** | ||
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
*/ | ||
function escapeRegExp(string: string): string { | ||
return string.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&'); // $& means the whole matched string | ||
} |
This file contains hidden or 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,254 @@ | ||
import { describe, expect, test } from '@jest/globals'; | ||
import { DEFAULT_OPTIONS, parseConfig } from '../transforms/helpers/options'; | ||
import { expectLogs, makeLogMatcher } from './helpers/expect-logs'; | ||
|
||
describe('options', () => { | ||
describe('parseConfig', () => { | ||
test('it parses an empty config', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', {}); | ||
expect(config).toStrictEqual({}); | ||
}); | ||
}); | ||
|
||
test('it parses the DEFAULT_OPTIONS', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', DEFAULT_OPTIONS); | ||
expect(config).toStrictEqual(DEFAULT_OPTIONS); | ||
}); | ||
}); | ||
|
||
describe('decorators', () => { | ||
test('it parses `{ decorators: true }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { decorators: true }); | ||
expect(config).toStrictEqual({ | ||
decorators: { inObjectLiterals: [] }, | ||
}); | ||
}); | ||
}); | ||
|
||
test('it parses `{ decorators: "true" }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { decorators: 'true' }); | ||
expect(config).toStrictEqual({ | ||
decorators: { inObjectLiterals: [] }, | ||
}); | ||
}); | ||
}); | ||
|
||
test('it parses `{ decorators: false }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { decorators: false }); | ||
expect(config).toStrictEqual({ decorators: false }); | ||
}); | ||
}); | ||
|
||
test('it parses `{ decorators: "false" }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { decorators: 'false' }); | ||
expect(config).toStrictEqual({ decorators: false }); | ||
}); | ||
}); | ||
|
||
test('it parses DecoratorOptions.inObjectLiterals with array of strings', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { | ||
decorators: { inObjectLiterals: ['one', 'two', 'three'] }, | ||
}); | ||
expect(config).toStrictEqual({ | ||
decorators: { inObjectLiterals: ['one', 'two', 'three'] }, | ||
}); | ||
}); | ||
}); | ||
|
||
test('it parses DecoratorOptions.inObjectLiterals with string of strings', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { | ||
decorators: { inObjectLiterals: 'one,two , three' }, | ||
}); | ||
expect(config).toStrictEqual({ | ||
decorators: { inObjectLiterals: ['one', 'two', 'three'] }, | ||
}); | ||
}); | ||
}); | ||
|
||
test('it logs an error for invalid `decorators` config', () => { | ||
expectLogs( | ||
() => { | ||
const config = parseConfig('test', { decorators: 'oops' }); | ||
expect(config).toStrictEqual({}); | ||
}, | ||
{ | ||
error: [ | ||
makeLogMatcher( | ||
'[test]: CONFIG ERROR:', | ||
"[decorators] Expected DecoratorOptions object or boolean, received 'oops'" | ||
), | ||
], | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe.each(['classFields', 'classicDecorator', 'partialTransforms'])( | ||
'%s (StringBooleanSchema)', | ||
(fieldName) => { | ||
test(`it parses \`{ ${fieldName}: true }\``, () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { [fieldName]: true }); | ||
expect(config).toStrictEqual({ [fieldName]: true }); | ||
}); | ||
}); | ||
|
||
test(`it parses \`{ ${fieldName}: "true" }\``, () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { [fieldName]: 'true' }); | ||
expect(config).toStrictEqual({ [fieldName]: true }); | ||
}); | ||
}); | ||
|
||
test(`it parses \`{ ${fieldName}: false }\``, () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { [fieldName]: false }); | ||
expect(config).toStrictEqual({ [fieldName]: false }); | ||
}); | ||
}); | ||
|
||
test(`it parses \`{ ${fieldName}: "false" }\``, () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { [fieldName]: 'false' }); | ||
expect(config).toStrictEqual({ [fieldName]: false }); | ||
}); | ||
}); | ||
|
||
test(`it logs an error for invalid \`${fieldName}\` config`, () => { | ||
expectLogs( | ||
() => { | ||
const config = parseConfig('test', { [fieldName]: 'oops' }); | ||
expect(config).toStrictEqual({}); | ||
}, | ||
{ | ||
error: [ | ||
makeLogMatcher( | ||
'[test]: CONFIG ERROR:', | ||
`[${fieldName}] Expected boolean, received string` | ||
), | ||
], | ||
} | ||
); | ||
}); | ||
} | ||
); | ||
|
||
describe('quote', () => { | ||
test('it parses `{ quote: "single" }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { quote: 'single' }); | ||
expect(config).toStrictEqual({ quote: 'single' }); | ||
}); | ||
}); | ||
|
||
test('it parses `{ quote: "double" }`', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { quote: 'double' }); | ||
expect(config).toStrictEqual({ quote: 'double' }); | ||
}); | ||
}); | ||
|
||
test('it logs an error for invalid `quote` config', () => { | ||
expectLogs( | ||
() => { | ||
const config = parseConfig('test', { quote: 'oops' }); | ||
expect(config).toStrictEqual({}); | ||
}, | ||
{ | ||
error: [ | ||
makeLogMatcher( | ||
'[test]: CONFIG ERROR:', | ||
"[quote] Expected 'single' or 'double', received 'oops" | ||
), | ||
], | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('ignoreLeakingState', () => { | ||
test('it parses `ignoreLeakingState` with an empty array', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { ignoreLeakingState: [] }); | ||
expect(config).toStrictEqual({ ignoreLeakingState: [] }); | ||
}); | ||
}); | ||
|
||
test('it parses `ignoreLeakingState` with array of strings', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { | ||
ignoreLeakingState: ['one', 'two', 'three'], | ||
}); | ||
expect(config).toStrictEqual({ | ||
ignoreLeakingState: ['one', 'two', 'three'], | ||
}); | ||
}); | ||
}); | ||
|
||
test('it parses `ignoreLeakingState` with string of strings', () => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { | ||
ignoreLeakingState: 'one,two , three', | ||
}); | ||
expect(config).toStrictEqual({ | ||
ignoreLeakingState: ['one', 'two', 'three'], | ||
}); | ||
}); | ||
}); | ||
|
||
test('it logs an error for invalid `ignoreLeakingState` config', () => { | ||
expectLogs( | ||
() => { | ||
const config = parseConfig('test', { ignoreLeakingState: false }); | ||
expect(config).toStrictEqual({}); | ||
}, | ||
{ | ||
error: [ | ||
makeLogMatcher( | ||
'[test]: CONFIG ERROR:', | ||
'[ignoreLeakingState] Expected array of strings or comma-separated string, received false' | ||
), | ||
], | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('type', () => { | ||
test.each(['services', 'routes', 'components', 'controllers'])( | ||
'it parses `{ type: "%s" }`', | ||
(type) => { | ||
expectLogs(() => { | ||
const config = parseConfig('test', { type }); | ||
expect(config).toStrictEqual({ type }); | ||
}); | ||
} | ||
); | ||
|
||
test('it logs an error for invalid `type` config', () => { | ||
expectLogs( | ||
() => { | ||
const config = parseConfig('test', { type: 'oops' }); | ||
expect(config).toStrictEqual({}); | ||
}, | ||
{ | ||
error: [ | ||
makeLogMatcher( | ||
'[test]: CONFIG ERROR:', | ||
"[type] Expected 'services', 'routes', 'components', or 'controllers', received 'oops" | ||
), | ||
], | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm loving all these new tests. Great job!