Skip to content
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

test(sanity): add toMatchEmissions matcher #8519

Merged
merged 1 commit into from
Feb 6, 2025
Merged

Conversation

juice49
Copy link
Contributor

@juice49 juice49 commented Feb 5, 2025

Description

This branch adds a toMatchEmissions matcher to Vitest. This can be used to assert a specific value is emitted for each value that is piped to a given observable.

function uppercase(): OperatorFunction<string, string> {
  return map((string) => string.toUpperCase())
}

await expect(uppercase).toMatchEmissions([
  ['a', 'A'],
  ['foo', 'FOO'],
])
function countSheep(): OperatorFunction<string, number> {
  return pipe(
    filter((maybeSheep) => maybeSheep === 'sheep'),
    scan((sum) => sum + 1, 0),
    startWith(0),
  )
}

await expect(countSheep).toMatchEmissions([
  ['aardvark', 0],
  ['capybara', NO_EMISSION],
  ['sheep', 1],
  ['sheep', 2],
  ['gila monster', NO_EMISSION],
  ['sheep', 3],
])

You can see how this is used in practice by taking a look at the next commit in this Graphite stack.

What to review

  • Does this seem sensible? Useful to other folks? Is there an alternative you'd suggest?

Testing

  • Try running a Vitest test with the example assertions listed above.

Copy link

vercel bot commented Feb 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
page-building-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 6, 2025 1:32pm
performance-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 6, 2025 1:32pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 6, 2025 1:32pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Feb 6, 2025 1:32pm
test-next-studio ⬜️ Ignored (Inspect) Feb 6, 2025 1:32pm

Copy link
Contributor Author

juice49 commented Feb 5, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

socket-security bot commented Feb 5, 2025

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/@portabletext/[email protected] None 0 334 kB sanity-io
npm/[email protected] environment Transitive: filesystem +6 2.3 MB probablyup

View full report↗︎

Copy link
Contributor

github-actions bot commented Feb 5, 2025

No changes to documentation

Copy link
Contributor

github-actions bot commented Feb 5, 2025

Component Testing Report Updated Feb 6, 2025 1:35 PM (UTC)

❌ Failed Tests (1) -- expand for details
File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 1m 7s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 13s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ❌ Failed (Inspect) 1m 20s 5 0 1
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 52s 11 7 0
formBuilder/inputs/PortableText/copyPaste/CopyPasteFields.spec.tsx ✅ Passed (Inspect) 0s 0 12 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/DisableFocusAndUnset.spec.tsx ✅ Passed (Inspect) 14s 3 0 0
formBuilder/inputs/PortableText/DragAndDrop.spec.tsx ✅ Passed (Inspect) 28s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 1m 8s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 31s 21 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 2m 3s 21 0 0
formBuilder/inputs/PortableText/PresenceCursors.spec.tsx ✅ Passed (Inspect) 13s 3 9 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 26s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 1m 44s 21 0 0
formBuilder/tree-editing/TreeEditing.spec.tsx ✅ Passed (Inspect) 0s 0 3 0
formBuilder/tree-editing/TreeEditingNestedObjects.spec.tsx ✅ Passed (Inspect) 0s 0 3 0

Copy link
Contributor

github-actions bot commented Feb 5, 2025

⚡️ Editor Performance Report

Updated Thu, 06 Feb 2025 13:36:52 GMT

Benchmark reference
latency of sanity@latest
experiment
latency of this branch
Δ (%)
latency difference
article (title) 25.6 efps (39ms) 25.3 efps (40ms) +1ms (+1.3%)
article (body) 78.1 efps (13ms) 72.2 efps (14ms) +1ms (-/-%)
article (string inside object) 26.3 efps (38ms) 25.6 efps (39ms) +1ms (+2.6%)
article (string inside array) 23.8 efps (42ms) 22.7 efps (44ms) +2ms (+4.8%)
recipe (name) 50.0 efps (20ms) 50.0 efps (20ms) +0ms (-/-%)
recipe (description) 55.6 efps (18ms) 52.6 efps (19ms) +1ms (+5.6%)
recipe (instructions) 99.9+ efps (5ms) 99.9+ efps (5ms) +0ms (-/-%)
synthetic (title) 18.2 efps (55ms) 17.2 efps (58ms) +3ms (+5.5%)
synthetic (string inside object) 19.4 efps (52ms) 18.9 efps (53ms) +2ms (+2.9%)

efps — editor "frames per second". The number of updates assumed to be possible within a second.

Derived from input latency. efps = 1000 / input_latency

Detailed information

🏠 Reference result

The performance result of sanity@latest

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 39ms 57ms 84ms 667ms 1173ms 12.0s
article (body) 13ms 15ms 18ms 156ms 180ms 5.0s
article (string inside object) 38ms 39ms 41ms 194ms 313ms 6.7s
article (string inside array) 42ms 45ms 52ms 85ms 373ms 7.0s
recipe (name) 20ms 21ms 24ms 58ms 0ms 6.7s
recipe (description) 18ms 19ms 22ms 35ms 0ms 4.6s
recipe (instructions) 5ms 7ms 8ms 17ms 0ms 3.1s
synthetic (title) 55ms 60ms 64ms 310ms 1133ms 13.4s
synthetic (string inside object) 52ms 54ms 63ms 438ms 1108ms 8.4s

🧪 Experiment result

The performance result of this branch

Benchmark latency p75 p90 p99 blocking time test duration
article (title) 40ms 43ms 59ms 353ms 620ms 10.0s
article (body) 14ms 16ms 17ms 176ms 203ms 5.2s
article (string inside object) 39ms 43ms 47ms 167ms 146ms 6.7s
article (string inside array) 44ms 45ms 49ms 149ms 198ms 6.8s
recipe (name) 20ms 22ms 27ms 40ms 0ms 7.3s
recipe (description) 19ms 19ms 21ms 47ms 0ms 4.6s
recipe (instructions) 5ms 6ms 8ms 16ms 0ms 3.1s
synthetic (title) 58ms 61ms 67ms 392ms 1185ms 13.9s
synthetic (string inside object) 53ms 57ms 93ms 470ms 1690ms 9.2s

📚 Glossary

column definitions

  • benchmark — the name of the test, e.g. "article", followed by the label of the field being measured, e.g. "(title)".
  • latency — the time between when a key was pressed and when it was rendered. derived from a set of samples. the median (p50) is shown to show the most common latency.
  • p75 — the 75th percentile of the input latency in the test run. 75% of the sampled inputs in this benchmark were processed faster than this value. this provides insight into the upper range of typical performance.
  • p90 — the 90th percentile of the input latency in the test run. 90% of the sampled inputs were faster than this. this metric helps identify slower interactions that occurred less frequently during the benchmark.
  • p99 — the 99th percentile of the input latency in the test run. only 1% of sampled inputs were slower than this. this represents the worst-case scenarios encountered during the benchmark, useful for identifying potential performance outliers.
  • blocking time — the total time during which the main thread was blocked, preventing user input and UI updates. this metric helps identify performance bottlenecks that may cause the interface to feel unresponsive.
  • test duration — how long the test run took to complete.

@juice49 juice49 force-pushed the feat/test-rxjs-emissions branch from d34af75 to 7c02363 Compare February 5, 2025 22:19
Copy link
Contributor

github-actions bot commented Feb 5, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 42.38% 53684 / 126655
🔵 Statements 42.38% 53684 / 126655
🔵 Functions 47.87% 2766 / 5777
🔵 Branches 78.76% 10260 / 13026
File CoverageNo changed files found.
Generated in workflow #29935 for commit c135ab5 by the Vitest Coverage Report Action

jordanl17
jordanl17 previously approved these changes Feb 6, 2025
Copy link
Member

@jordanl17 jordanl17 left a comment

Choose a reason for hiding this comment

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

Great idea

Copy link
Member

Choose a reason for hiding this comment

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

Q: Should we add some tests for this test... inception, but perhaps just the ones you included in the description of the PR could we added alongside this. Just given that this isn't being used anywhere yet, that feels like it would be quite good. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wondered that, too. I agree it's probably a good idea!

export async function toMatchEmissions(
this: MatcherState,
createOperator: () => OperatorFunction<unknown, unknown>,
snapshots: [A: unknown, B: unknown][],
Copy link
Member

Choose a reason for hiding this comment

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

COULD: could rename these types to:

Suggested change
snapshots: [A: unknown, B: unknown][],
snapshots: [value: unknown, emission: unknown][],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I actually did use those names initially, but changed them to become consistent with RxJS' types where you see things like UnaryFunction<A, B>.

@juice49 juice49 enabled auto-merge February 6, 2025 13:20
Copy link
Member

@jordanl17 jordanl17 left a comment

Choose a reason for hiding this comment

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

Nice one!

@juice49 juice49 added this pull request to the merge queue Feb 6, 2025
Merged via the queue into next with commit be0fcc9 Feb 6, 2025
63 checks passed
@juice49 juice49 deleted the feat/test-rxjs-emissions branch February 6, 2025 15:51
juice49 added a commit that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants