-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(errors): validate error object shapes and unique codes @W-148412…
…23 (#3949) * test(errors): validate error object shapes and unique codes * fix(typo): fix regex Co-authored-by: Eugene Kashida <[email protected]> * chore: add missing copyright headers --------- Co-authored-by: Eugene Kashida <[email protected]>
- Loading branch information
Showing
4 changed files
with
61 additions
and
12 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/@lwc/errors/src/compiler/__tests__/error-info.spec.ts
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,50 @@ | ||
/* | ||
* Copyright (c) 2024, Salesforce, Inc. | ||
* All rights reserved. | ||
* SPDX-License-Identifier: MIT | ||
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT | ||
*/ | ||
import * as errorInfo from '../error-info'; | ||
|
||
// All exported objects are maps of label/error info, except for GENERIC_COMPILER_ERROR, | ||
// which is a top-level error info object | ||
const { GENERIC_COMPILER_ERROR, ...errors } = errorInfo; | ||
|
||
const errorInfoMatcher = { | ||
code: expect.any(Number), | ||
message: expect.any(String), | ||
url: expect.any(String), | ||
// Technically not *any* number, but jest doesn't have oneOf | ||
level: expect.any(Number), | ||
}; | ||
|
||
it('GENERIC_COMPILER_ERROR should be an error info object', () => { | ||
expect(GENERIC_COMPILER_ERROR).toEqual(errorInfoMatcher); | ||
}); | ||
|
||
describe.each(Object.entries(errors))('%s errors', (_key, map) => { | ||
it('labels should all be UPPER_SNAKE_CASE', () => { | ||
Object.keys(map).forEach((label) => { | ||
expect(label).toMatch(/^[A-Z]+(?:_[A-Z]+?)*?$/); | ||
}); | ||
}); | ||
it.each(Object.entries(map))('%s should be an error info object', (_label, info) => { | ||
expect(info).toEqual(errorInfoMatcher); | ||
}); | ||
}); | ||
|
||
it('error codes are unique', () => { | ||
// Map of error codes to the errors that use them | ||
const seen = new Map([[GENERIC_COMPILER_ERROR.code, ['GENERIC_COMPILER_ERROR']]]); | ||
Object.entries(errors).forEach(([key, map]) => { | ||
Object.entries(map).forEach(([label, info]) => { | ||
const path = `${key}.${label}`; | ||
const prev = seen.get(info.code) ?? []; | ||
seen.set(info.code, [...prev, path]); | ||
}); | ||
}); | ||
// This assertion prints errors that use the same code for easier debugging | ||
for (const arr of seen.values()) { | ||
expect(arr).toHaveLength(1); | ||
} | ||
}); |
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