-
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: deno test --docs support #7916
Conversation
Update fork
Update fork
Formatted
* fix clippy errors * break extract_jsdoc_examples to smaller fns * fix error when ran without arguments
* test: extract_jsdoc_examples * add more tests * formatting
update fork
pub fn is_supported_doctest(path: &Path) -> bool { | ||
let valid_ext = ["ts", "tsx", "js", "jsx"]; | ||
path | ||
.extension() | ||
.and_then(OsStr::to_str) | ||
.map(|ext| valid_ext.contains(&ext) && !is_supported(path)) | ||
.unwrap_or(false) | ||
} |
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.
Not really sure what's going on here, @iykekings please explain
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.
This is like opposite of is_supported for normal test files. It ensures doc tests are not run on test files and also that the file format is of normal expected type
fn extract_test_tag(input: &str) -> Option<&str> { | ||
TEST_TAG_PATTERN | ||
.captures(input) | ||
.and_then(|m| m.get(1).map(|c| c.as_str())) | ||
} | ||
|
||
fn extract_caption(input: &str) -> Option<&str> { | ||
CAPTION_PATTERN | ||
.captures(input) | ||
.and_then(|m| m.get(1).map(|c| c.as_str())) | ||
} | ||
|
||
fn extract_imports(input: &str) -> Vec<&str> { | ||
IMPORT_PATTERN | ||
.captures_iter(input) | ||
.filter_map(|caps| caps.get(0).map(|m| m.as_str())) | ||
.collect() | ||
} | ||
|
||
fn has_await(input: &str) -> bool { | ||
AWAIT_PATTERN.find(input).is_some() | ||
} |
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.
Those utils seems a little brittle 😬 we should probably leverage SWC for this purpose, but I guess it's alright for first pass
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.
Yeah I think so too, will have to parse each of the examples with swc
This PR requires more work before it can be landed; mainly it needs to use SWC for parsing instead of regexes. I'm removing this PR from |
I'm just not sure about auto-importing |
@wperron I've already requested to remove this feature, I think it's not included in the PR anymore? |
Closing as stale because of numerous changes to test runner since this PR was open. Thank you @iykekings this PR was a great base for @caspervonb, who's working on a new version with simpler API. |
Thanks a lot @bartlomieju |
Closed #6124 in favour of this.
This PR adds --docs to deno test sub-command for running tests on JSDoc @example blocks.
To use:
have code blocks in @example sections with three back-ticks as in markdown code blocks
deno test --docs runs all non-test files
Imports are done relative to root path, eg.
src/linkedlist.ts
All functions exported from https://deno.land/std/testing/asserts.ts can be used without importing them in an
assert
object. (assert.assert
,assert.assertEquals
,assert.assertNotEquals
etc.)await
keyword can be used without wrapping in async function. NB: This is not due to TLA, but await will be detected in the example and the generated test is wrapped in an async function.supports caption tag on @example block and used name of the test, by default test result is printed like this test src/linkedlist.ts - (line 160) ... ok (0ms), but with caption, like in the example above it will be test src/linkedlist.ts - Linkedlists.compareWith (line 160) ... ok (0ms)
Supports multiple @example blocks in one JSDoc comment
To ignore test:
wrap @example code block with back-ticks
Tag the three opening back-ticks with ignore, and it will be picked up but not tested, will be recorded by Deno test as ignored.
Tag the three opening back-ticks with text, and it will be seen as normal text
This is inspired by rustdoc --test
Example output
Resolves #4716