node icon indicating copy to clipboard operation
node copied to clipboard

test_runner: add inline snapshot assertion

Open rluvaton opened this issue 1 year ago β€’ 13 comments

TODO

  • [x] Add tests
  • [x] Add documentation

rluvaton avatar Aug 21 '24 12:08 rluvaton

Review requested:

  • [ ] @nodejs/test_runner

nodejs-github-bot avatar Aug 21 '24 12:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61326/

nodejs-github-bot avatar Aug 21 '24 17:08 nodejs-github-bot

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 87.34%. Comparing base (cc26951) to head (3c4209d). Report is 592 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #54480   +/-   ##
=======================================
  Coverage   87.34%   87.34%           
=======================================
  Files         649      649           
  Lines      182544   182541    -3     
  Branches    35030    35033    +3     
=======================================
+ Hits       159445   159449    +4     
+ Misses      16372    16364    -8     
- Partials     6727     6728    +1     
Files with missing lines Coverage Ξ”
lib/internal/test_runner/snapshot.js 100.00% <100.00%> (ΓΈ)
lib/internal/test_runner/test.js 98.36% <100.00%> (+<0.01%) :arrow_up:

... and 20 files with indirect coverage changes

codecov[bot] avatar Aug 21 '24 18:08 codecov[bot]

CI: https://ci.nodejs.org/job/node-test-pull-request/61332/

nodejs-github-bot avatar Aug 21 '24 20:08 nodejs-github-bot

I'm curious how valuable this type of assertion is given the fact that there is no update functionality. My understanding is that runners like Jest will rewrite the source code with the snapshot value. Without that aspect, I'm not sure what the advantage is over a regular assertion.

Somewhat related to this - I think it would be useful to expose an API for adding (and possibly removing) functions to t.assert. Snapshot testing was an internal proof of concept for that. I plan to continue work on that API after I'm finished with my planned changes for Node v23, but if someone else wants to do it first, that would be great. The changes in this PR to lib/internal/test_runner/test.js show how relatively simple it could be.

cjihrig avatar Aug 21 '24 21:08 cjihrig

I'm curious how valuable this type of assertion is given the fact that there is no update functionality. My understanding is that runners like Jest will rewrite the source code with the snapshot value. Without that aspect, I'm not sure what the advantage is over a regular assertion.

In my opinion it would be very useful, my use case is snapshot testing some error output that printed to console and I have default serializer to convert the ANSI codes for example jest-serializer-ansi-escapes

Somewhat related to this - I think it would be useful to expose an API for adding (and possibly removing) functions to t.assert. Snapshot testing was an internal proof of concept for that. I plan to continue work on that API after I'm finished with my planned changes for Node v23, but if someone else wants to do it first, that would be great. The changes in this PR to lib/internal/test_runner/test.js show how relatively simple it could be.

I agree, although personally, I prefer the expect syntax so I mostly use this except for snapshot testing, in which I use the node:assert package

I want to add property matchers to snapshots like in jest to support dynamic data which is extremely useful

rluvaton avatar Aug 22 '24 07:08 rluvaton

CI: https://ci.nodejs.org/job/node-test-pull-request/61338/

nodejs-github-bot avatar Aug 22 '24 07:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61340/

nodejs-github-bot avatar Aug 22 '24 09:08 nodejs-github-bot

I agree, although personally, I prefer the expect syntax so I mostly use this except for snapshot testing, in which I use the node:assert package

Yea, that's the goal of making t.assert customizable by users. You can already use chai or expect for assertions. This would make them usable on test contexts as well. Also, snapshots are not part of node:assert πŸ˜„

cjihrig avatar Aug 22 '24 13:08 cjihrig

@cjihrig are you blocking on this?

rluvaton avatar Aug 24 '24 17:08 rluvaton

He's not (afaict) but he's not convinced that this feature as-is is useful enough and neither am I (given it's much worse dx than the userland alternatives that rewrite your code)

benjamingr avatar Aug 24 '24 17:08 benjamingr

Neither am I (given it's much worse dx than the userland alternatives that rewrite your code)

Do you mean in jest?

Using inline snapshot we can use string manipulation on the output which we can't do in the regular snapshot, like creating dynamic snapshot text that depends on the process ID that runs it or (in my use case) ignoring the random port that the server picked (when using port 0)

rluvaton avatar Aug 24 '24 18:08 rluvaton

I'm not blocking, I just foresee people opening issues over the lack of update functionality, even when --test-update-snapshots is passed. Without update functionality, this is essentially only assert.strictEqual(serializers(actual), expected).

I want to add property matchers to snapshots

I realized I never commented on this bit. I think that's a useful feature. I'm curious what your API design would look like since it is based on expect in Jest.

cjihrig avatar Aug 24 '24 19:08 cjihrig

This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open.

github-actions[bot] avatar Sep 19 '24 18:09 github-actions[bot]

Closing this because it has stalled. Feel free to reopen if this issue/PR is still relevant, or to ping the collaborator who labelled it stalled if you have any questions.

github-actions[bot] avatar Oct 20 '24 00:10 github-actions[bot]