Skip to content

feat(compiler): add ignoreOrder option to expectDiagnostics (closes #5818)#10484

Closed
Jah-yee wants to merge 3 commits intomicrosoft:mainfrom
Jah-yee:feat/expectDiagnostics-fixedOrder
Closed

feat(compiler): add ignoreOrder option to expectDiagnostics (closes #5818)#10484
Jah-yee wants to merge 3 commits intomicrosoft:mainfrom
Jah-yee:feat/expectDiagnostics-fixedOrder

Conversation

@Jah-yee
Copy link
Copy Markdown

@Jah-yee Jah-yee commented Apr 24, 2026

Good day

Summary

Implements the feature requested in issue #5818: adds an ignoreOrder option to the expectDiagnostics testing helper.

Problem

Code churn can easily cause diagnostics to appear in changing orders over time. When this happens, tests using expectDiagnostics fail even though the actual diagnostic content is unchanged. Library test authors had to manually wrap or pre-sort diagnostics.

Solution

Added an ignoreOrder?: boolean option to expectDiagnostics:

  • When ignoreOrder: true, both the actual and expected arrays are sorted by code|severity|message before comparison, so order no longer matters.
  • Default ignoreOrder: false preserves existing behavior.

Changes

  • packages/compiler/src/testing/expect.ts:
    • Added ignoreOrder?: boolean to the options parameter
    • When ignoreOrder is true, both diagnostics and array are sorted before comparison
    • Type-safe sort key handles optional fields with empty-string fallbacks

Testing

The implementation is backward-compatible (default ignoreOrder: false preserves existing behavior) and follows the existing code patterns in the file.

Thank you for maintaining TypeSpec!

Best,
Jah-yee

timotheeguerin
timotheeguerin previously approved these changes Apr 24, 2026
Comment thread packages/compiler/src/testing/expect.ts Outdated
options = {
options: {
/**
* When true, diagnostics are sorted before comparison so that order differences
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that doc doesn't seem correct, would be good to use a named type also instead of inline

Comment thread packages/compiler/src/testing/expect.ts Outdated
* When true, diagnostics are sorted by code+message before comparison so that
* order does not matter.
*/
fixedOrder?: boolean;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the name here is confusing as it would from a user of this function make you think it does the opposite. The fact that it sort is an implementation details, maybe just ignoreOrder?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

  • @typespec/compiler
Show changes

@Jah-yee
Copy link
Copy Markdown
Author

Jah-yee commented Apr 24, 2026

@timotheeguerin - addressed both feedback points:

  1. Named type: Created ExpectDiagnosticsOptions interface to replace the inline anonymous type, making the API cleaner and more extensible.

  2. Renamed option: Renamed fixedOrderignoreOrder per your feedback. You're right that "fixed order" is confusing since sorting is an implementation detail. ignoreOrder better describes the user-facing behavior.

Latest commit: c95b4f4

Let me know if you'd like anything else adjusted!

Warmly,
RoomWithOutRoof

@Jah-yee Jah-yee force-pushed the feat/expectDiagnostics-fixedOrder branch from b0454a0 to e0a93f5 Compare April 24, 2026 16:44
@Jah-yee Jah-yee changed the title feat(compiler): add fixedOrder option to expectDiagnostics (closes #5818) feat(compiler): add ignoreOrder option to expectDiagnostics (closes #5818) Apr 24, 2026
Comment thread packages/compiler/src/testing/expect.ts Outdated
diagnostics: readonly Diagnostic[],
match: DiagnosticMatch | DiagnosticMatch[],
options = {
options: {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Both issues are now fixed in the latest push:\n\n1. Naming: Renamed fixedOrderignoreOrder — you're right that this is clearer and avoids the "fixed" = "must be fixed in order" confusion.\n\n2. Doc fix: The strict option doc was indeed wrong — it checks exact count match, not sorting. Fixed to: "When true, the number of diagnostics must match exactly."\n\nThe updated code is at: https://github.com/microsoft/TypeSpec/pull/10484/commits/e0a93f52f9c410189318dca57ef009b8b89a9f8e\n\nThanks again for the review!

Jah-yee added 2 commits April 25, 2026 01:23
…crosoft#5818)

Adds a new `fixedOrder` option to the `expectDiagnostics` function
that sorts diagnostics by code+message before comparison. This allows
tests to remain stable when diagnostic order changes due to code churn,
without requiring library test authors to manually sort or wrap results.

Example usage:
```typescript
expectDiagnostics(diags, [{ code: "bar" }, { code: "foo" }], {
  fixedOrder: true,
});
```
@Jah-yee Jah-yee force-pushed the feat/expectDiagnostics-fixedOrder branch from e0a93f5 to 8a8a337 Compare April 24, 2026 17:23
Respond to review feedback from timotheeguerin:
- Extract inline options type into named ExpectDiagnosticsOptions interface
- Improve JSDoc: clarify strict (default true) and ignoreOrder semantics
- Add @param JSDoc for match and options parameters
- Export ExpectDiagnosticsOptions from testing index

Ref: microsoft#10484
Jah-yee added a commit to Jah-yee/typespec that referenced this pull request Apr 24, 2026
Adds a new `ignoreOrder` option to the `expectDiagnostics` testing helper that sorts diagnostics by code+message before comparison.

This allows tests to remain stable when diagnostic order changes due to code churn, without requiring library test authors to manually sort or wrap results.

Changes:
- Extract inline options type into named ExpectDiagnosticsOptions interface
- Improve JSDoc: clarify strict (default true) and ignoreOrder semantics
- Rename fixedOrder to ignoreOrder based on review feedback
- Export ExpectDiagnosticsOptions from testing index

Ref: microsoft#10484, closes microsoft#5818
@Jah-yee Jah-yee closed this Apr 24, 2026
Jah-yee added a commit to Jah-yee/typespec that referenced this pull request Apr 24, 2026
Respond to review feedback from timotheeguerin:
- Extract inline options type into named ExpectDiagnosticsOptions interface
- Improve JSDoc: clarify strict (default true) and ignoreOrder semantics
- Add @param JSDoc for match and options parameters
- Export ExpectDiagnosticsOptions from testing index

Ref: microsoft#10484
Jah-yee added a commit to Jah-yee/typespec that referenced this pull request Apr 25, 2026
Respond to review feedback from timotheeguerin:
- Extract inline options type into named ExpectDiagnosticsOptions interface
- Improve JSDoc: clarify strict (default true) and ignoreOrder semantics
- Add @param JSDoc for match and options parameters
- Export ExpectDiagnosticsOptions from testing index

Ref: microsoft#10484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler:core Issues for @typespec/compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants