-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat(cli): evaluate code snippets in JSDoc and markdown #25220
feat(cli): evaluate code snippets in JSDoc and markdown #25220
Conversation
@magurotuna this looks great, can we use it to test our own declarations files (eg. |
I did (against deno_std as well), and what turned out was some fail because they are not self-contained and make some assumption on the testing environment that is not always the case, or some even hung - which made me feel we need to preserve the existing type-checking only mode in another command 😄 Here's the result of running This says that the test case starting from line 2781 hangs - and actually this test case is waiting for something from stdin, which obviously doesn't occur, thus resulting in unfinished: deno/cli/tsc/dts/lib.deno.ns.d.ts Lines 2781 to 2787 in c29e5b9
And one of the failing test cases is this, where it tries to open deno/cli/tsc/dts/lib.deno.ns.d.ts Lines 2725 to 2728 in c29e5b9
So I think we can conclude that the new doc test feature is working as we expect, although many of the existing example code snippets in JSDoc need to be updated. |
Two blocking concerns, IMO:
|
Yes it does. I added another test case to illustrate it 13235e2. Let's clarify this in the document later.
The existing directive |
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 is a great first pass and we can keep iterating on this one. @magurotuna could you please update the docs page at https://docs.deno.com/runtime/fundamentals/testing/?
Yes! Will do |
) We landed "true" doc testing in denoland/deno#25220. With this change, `deno test --doc` will actually _run_ code snippets written in JSDoc or markdown files, as well as type checking. This commit updates the corresponding doc pages to follow this change.
denoland/deno#25220 completed this work
This commit lets
deno test --doc
command actually evaluate code snippets in JSDoc and markdown files.How it works
Deno.test(...)
We apply some magic at the step 2 - let's say we have the following file named
mod.ts
as an input:This is virtually transformed into:
Note that a new import statement is inserted here to make
add
function available. In a nutshell, all items exported frommod.ts
become available in the generated pseudo file with this automatic import insertion.The intention behind this design is that, from library user's standpoint, it should be very obvious that this
add
function is what this example code is attached to. Also, if there is an explicit import statement likeimport { add } from "./mod.ts"
, this import path./mod.ts
is not helpful for doc readers because they will need to import it in a different way.The automatic import insertion has some edge cases, in particular where there is a local variable in a snippet with the same name as one of the exported items. This case is addressed by employing swc's scope analysis (see test cases for more details).
"type-checking only" mode stays around
This change will likely impact a lot of existing doc tests in the ecosystem because some doc tests rely on the fact that they are not evaluated - some cause side effects if executed, some throw errors at runtime although they do pass the type check, etc.
To help those tests gradually transition to the ones runnable with the new
deno test --doc
, we will keep providing the ability to run type-checking only viadeno check --doc
. Additionally there is a--doc-only
option added to thecheck
subcommand too, which is useful when you want to type-check on code snippets in markdown files, as normaldeno check
command doesn't accept markdown.Demo
deno_test_doc_demo.mp4
Closes #4716