-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
assert: add snapshot assertion #44095
Conversation
CC @nodejs/assert @nodejs/test_runner |
another example of an (internal) implentation for this: https://github.com/nodejs/node-core-test/blob/main/test/message.js, besides |
I think it's useful to add snapshot tests but I would expect a simpler API. As such, I would expect no transforms. This should be done by the users before passing any value to be compared. That way we are also aligned with our other assertions that are kept super simple. Adding a functionality to let the user decide where to put the snapshot is also nice but I would again suggest to go for the simpler implementation: by default, save the snapshot in a file and let the user define the snapshot optionally. That way it's possible for them to receive the snapshot from where ever they want to save it. The overall API would look like: assert.snapshot(input[, snapshot]) Running it once is going to serialize the input with If the user would want to transform something or use a different spot to save the snapshot they can: const transformedInput = input.toLowerCase()
const snapshot = await createReadStream(path)
assert.snapshot(transformedInput, snapshot) The added benefit is not only the simpler API but also that there's a clear error to the user in case it's not possible to read the read stream (or what ever they use to receive the snapshot). |
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 like this and the idea.
I like Ruben's suggested API (no classes, single function, less options) better.
(Docs and other stuff is obviously still missing)
+1 to supporting this with a simpler API. Could you please add tests to verify:
|
yeah, that sounds right - probably most of the transforms I wrote should go somewhere along
in the API I have implemented - all options are optional, including
this accepts an option specifying where to read the snapshot from, what about an option specifying where to write to?
if transforms are done in userland why shouldn't serialization be performed there as well?
yes, that is the case in my implementation as well. probably because ALL of my tests passe a |
Yes, and no options.
It has no option where to read from the snapshot, only the actual snapshot content (e.g., an object or a string). That way reading and writing is up to the user. Inline snapshots become easy that way.
The first argument is the actual input (e.g., a complex object). This has to be serialized on our side or we would only be able to accept strings as input. That does not seem ideal from the usability side. If we serialize the input, it is consistent to also serialize the snapshot, if provided. |
in case the snapshot is known in advance (a.k.a inline snapshot) - running on the other hand, I can see use cases for extending the API and providing |
We can start with |
That will prevent even basic usecases such as specifying a location for the snapshot file. |
That is correct. The main functionality should however already be implemented. Let's iterate upon the implemention in smaller steps. |
+1 to not having any options in the initial implementation, with good defaults we have a useful platform for iteration |
@BridgeAR @benjamingr @cjihrig @mscdex @juliangruber |
Looks mostly good, why the experimental warning though? I think making this experimental (in the docs) should be enough. |
doc/api/errors.md
Outdated
### `ERR_ASSERT_SNAPSHOT_NOT_SUPPORTED` | ||
|
||
An attempt was maid to use `assert.snapshot()` in an environment that | ||
does not support snapshots, such as REPL, or when using `node --eval` |
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.
Missing "." and "the"?
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 think this is a VERY tricky feature to add. There are many snapshot heuristics and no one cohesive userland solution, and node shouldn't be opinionated here.
What happens with recursion? What happens with custom values, like jsx elements? It's very unclear to me from this PR how this feature works right not.
doc/api/assert.md
Outdated
|
||
* `value` {string} the value to snapshot | ||
* `name` {string} the name of snapshot. | ||
in case order of snapshots is non-deterministic, |
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.
why would ordering be non-determinstic??
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.
If you run concurrent tests for example?
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.
oof, that's a great reason to never do that :-/
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.
maybe we should just require the name then, rather than opening the footgun?
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.
maybe in a further iteration we can use asyncLocalStorage
to automatically use the test name when running inside node test runner, but that should not happen in this iteration anyway
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.
@benjamingr concurrency can optionally also be per test - and assert.snapshot
should work independently of how node:test
test works
just as an example, this will break with any other test runner or program that runs things in parallel:
describre({ concurrency: true }, () => {
it('1', async () => {
await setTimeout(random());
assert.snapshot(thing);
});
it('2', async () => {
await setTimeout(random());
assert.snapshot(otherThing);
});
});
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.
Ok, I'm not sure I agree how common/feasible of a footgun it is and it feels to me people would have to work pretty hard to run into it.
That said if the smallest unit of concurrency is the test (and not the file) I think appending the test name to the snapshot could be an easy fix then?
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.
sure - or, requiring users of this API to provide a name, and then we could make it be "the test name" in a followon.
The simplest API - which is appropriate for a new feature - is when everything is explicit and required, and nothing is implicit or optional.
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.
There were objections raised to that above I think? I think using the test name is a good workaround to keep everyone content with the API.
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.
There's a lot of comments; can you link to it? I'd love to have those who objected explain why an implicit optional name is simpler than an explicit required one.
const { dir, name } = path.parse(process.mainModule.filename); | ||
return path.join(dir, `${name}.snapshot`); | ||
} | ||
if (!process.argv[1]) { |
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.
Is this to check if we're in the REPL? wouldn't it be better to just check if there is no process.mainModule?.filename
?
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.
esm does not have process.mainModule
either, so argv[1]
is used instead
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 haven't read the code, but reminder that process.argv[1] == false
when doing stdin eval and --eval
, not just in REPL.
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.
Yes, it is documented to not be supported at this stage.
In a further stage we can support the source and target options that were in the original commit
} else if (snapshot.has(name)) { | ||
const expected = snapshot.get(name); | ||
// eslint-disable-next-line no-restricted-syntax | ||
assert.strictEqual(value, expected); |
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.
It's kind of a bummer that this does string comparison and not deepStrictEqual
or something similar with better dx here but I guess since it's util.inspect
it's customizable. I think this can be revisited later/in a future PR.
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.
Left some comments mostly lgtm
ping @benjamingr this needs a fresh approval |
Landed in 8f9d1ab |
PR-URL: #44095 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
PR-URL: #44095 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
@MoLow Please add the |
PR-URL: nodejs#44095 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
this is a proposal to add to
node:assert
a new classassisting with snapshot assertion (e.g compare a values with a snapshot saved to a file (/any other WritableStream))
as a demonstration how it can be used, I migrated some of the
test/message
files to use this, see this diff to compare how the current python implementation with this new class