-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
feat(test): Add "name", "origin" and "parent" to "Deno.TestContext" #14007
Conversation
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.
LGTM! Seems reasonable to add this name
property
@dsherret I updated the PR but it was delayed due to github status issue. sorry 😢 |
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.
The new name
property seems nice, but I'm not sure this snapshotting feature should be in the CLI. It seems like something a testing framework (or deno_std) could implement themselves. cc @bartlomieju
3df94ba
to
3f7d86c
Compare
And I forgot to push the
I think the second one is better because handling snapshot testing in the core seems not ideal. |
I also agree that the snapshotting feature should not be part of CLI. |
@kitsonk I'm confused. Are you for adding this functionality to CLI directly or against? |
Not. Sorry... edited previous comment. |
Okay. I agree that snapshotting feature should not be part of CLI. deno test --allow-read --allow-write ./snapshot.test.ts -- --update and check |
@bartlomieju Is there any way to cleanup after tests are done? I cannot find the way to writing snapshot after tests without the |
Hope you don't mind me responding to this @hyp3rflow - feel free to add to/correct my reply as necessary :D As I understand it, the To illustrate this, I implemented a basic test to compare the performance of using a const NUMBER_OF_SNAPSHOTS = 100;
const obj: Record<string, Array<string>> = {};
for (let i = 0; i < NUMBER_OF_SNAPSHOTS; i++) {
obj[`key${i+1}`] = new Array(100).fill(new Array(100).fill('a').join(''));
}
Deno.test(async function noTeardown() {
await Deno.writeTextFile('test1.json', JSON.stringify(obj, null, 2));
const start = Date.now();
for (let i = 0; i < NUMBER_OF_SNAPSHOTS; i++) {
const snapshots: Record<string, Array<string>> = JSON.parse(await Deno.readTextFile('test1.json'));
snapshots[`key${i+1}`] = new Array(100).fill(new Array(100).fill('a').join(''));
await Deno.writeTextFile('test1.json', JSON.stringify(snapshots, null, 2));
}
const stop = Date.now();
await Deno.writeTextFile('result1.txt', `${stop-start}ms`);
});
Deno.test(async function withTeardown() {
await Deno.writeTextFile('test2.json', JSON.stringify(obj, null, 2));
const start = Date.now();
const snapshots: Record<string, Array<string>> = JSON.parse(await Deno.readTextFile('test2.json'));
for (let i = 0; i < NUMBER_OF_SNAPSHOTS; i++) {
snapshots[`key${i+1}`] = new Array(100).fill(new Array(100).fill('a').join(''));
}
await Deno.writeTextFile('test2.json', JSON.stringify(snapshots, null, 2));
const stop = Date.now();
await Deno.writeTextFile('result2.txt', `${stop-start}ms`);
}); These are the results (AMD Ryzen 5 3600 and high-ish end SSD): I put this together in a couple of minutes just to validate my intuition so let me know if you think this is not a valid test or if there's any mistakes! A cleanup function (e.g. Further to the above, I also feel that setup/teardown functions are a fairly standard and uncontroversial part of most testing frameworks (e.g. mocha, jest, jasmine, karma etc all offer before/after each/all functions). These could of course be implemented in a 3rd party testing library but this would probably need to duplicate a lot of the work already done as part of Denos test implementation in managing and running tests/test steps. Could you elaborate on why setup/cleanup functions were rejected? I tired to search the GitHub issues but couldn't find the discussion! :P |
@bcheidemann that's okay, I don't mind :) I'm thanks to you to reply elaborate comments related to this API. |
Well, I think we can use unload event instead of teardown API here. @bcheidemann I pushed commit on |
If you could get away with |
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.
LGTM approved the wrong 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.
Sorry, I approved the wrong PR when attempting to retry approving another PR due to the GH outage. I have yet to review this.
@dsherret okay, I will re-request your review :) |
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 seems more reasonable, but origin
can already be retrieved via import.meta.url
so I don't think we need it. We'll need to add tests for the name
and parent
functionality.
@dsherret where can I write the test for name, origin, parentContext? |
@hyp3rflow sorry for my delay. It can be added to cli/tests/unit/testing_tests.ts or some similar file. Basically, write some tests and then assert what's in the context. |
@dsherret that's okay! I added some tests for |
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.
LGTM. Thanks @hyp3rflow!
/** | ||
* File Uri of the current test code. | ||
*/ | ||
origin: string; |
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.
Considering that import.meta.url
is a string
, I think it makes sense for this to be a string as well.
@kitsonk @bartlomieju could you review this PR? I want to implement assertSnapshot after this PR merged. Thanks! 🙂 |
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.
LGTM
This PR includes features for implementing snapshot test feature (assertSnapshot) in
deno_std
.import.meta.url
orDeno.mainModule
to get the path of snapshot).Teardown API to call function after the test finished.Add update flag on cli/test for snapshot update mode.related comment is here: #3635 (comment)
I wrote simple test for this code, but I am not sure where I have to put this test file. please let me know where to put and how to run this test in the project structure. thanks!