diff options
author | Yusuke Tanaka <yusuktan@maguro.dev> | 2024-09-18 13:35:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-17 21:35:48 -0700 |
commit | d5c00ef50e6519fccde54a577e038f0ebb1282e9 (patch) | |
tree | 9429c5f09c6969fd8236041f48b354a9b0841e1f /tests | |
parent | 37315917625179063cb5653e2edd4ee0e5de99c5 (diff) |
feat(cli): evaluate code snippets in JSDoc and markdown (#25220)
This commit lets `deno test --doc` command actually evaluate code snippets in
JSDoc and markdown files.
## How it works
1. Extract code snippets from JSDoc or code fences
2. Convert them into pseudo files by wrapping them in `Deno.test(...)`
3. Register the pseudo files as in-memory files
4. Run type-check and evaluation
We apply some magic at the step 2 - let's say we have the following file named
`mod.ts` as an input:
````ts
/**
* ```ts
* import { assertEquals } from "jsr:@std/assert/equals";
*
* assertEquals(add(1, 2), 3);
* ```
*/
export function add(a: number, b: number) {
return a + b;
}
````
This is virtually transformed into:
```ts
import { assertEquals } from "jsr:@std/assert/equals";
import { add } from "files:///path/to/mod.ts";
Deno.test("mod.ts$2-7.ts", async () => {
assertEquals(add(1, 2), 3);
});
```
Note that a new import statement is inserted here to make `add` function
available. In a nutshell, all items exported from `mod.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 like
`import { 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 via `deno check --doc`. Additionally there is a `--doc-only`
option added to the `check` subcommand too, which is useful when you want to
type-check on code snippets in markdown files, as normal `deno check` command
doesn't accept markdown.
## Demo
https://github.com/user-attachments/assets/47e9af73-d16e-472d-b09e-1853b9e8f5ce
---
Closes #4716
Diffstat (limited to 'tests')
44 files changed, 544 insertions, 32 deletions
diff --git a/tests/integration/check_tests.rs b/tests/integration/check_tests.rs index 1ccec41eb..121dcb837 100644 --- a/tests/integration/check_tests.rs +++ b/tests/integration/check_tests.rs @@ -185,8 +185,8 @@ fn reload_flag() { fn typecheck_declarations_ns() { let context = TestContextBuilder::for_jsr().build(); let args = vec![ - "test".to_string(), - "--doc".to_string(), + "check".to_string(), + "--doc-only".to_string(), util::root_path() .join("cli/tsc/dts/lib.deno.ns.d.ts") .to_string_lossy() @@ -208,8 +208,8 @@ fn typecheck_declarations_ns() { fn typecheck_declarations_unstable() { let context = TestContext::default(); let args = vec![ - "test".to_string(), - "--doc".to_string(), + "check".to_string(), + "--doc-only".to_string(), util::root_path() .join("cli/tsc/dts/lib.deno.unstable.d.ts") .to_string_lossy() diff --git a/tests/integration/watcher_tests.rs b/tests/integration/watcher_tests.rs index 27c59a27d..56686cd14 100644 --- a/tests/integration/watcher_tests.rs +++ b/tests/integration/watcher_tests.rs @@ -1022,6 +1022,8 @@ async fn test_watch_doc() { let mut child = util::deno_cmd() .current_dir(t.path()) .arg("test") + .arg("--config") + .arg(util::deno_config_path()) .arg("--watch") .arg("--doc") .arg(t.path()) @@ -1039,26 +1041,110 @@ async fn test_watch_doc() { wait_contains("Test finished", &mut stderr_lines).await; let foo_file = t.path().join("foo.ts"); + let foo_file_url = foo_file.url_file(); foo_file.write( r#" - export default function foo() {} + export function add(a: number, b: number) { + return a + b; + } + "#, + ); + + wait_contains("ok | 0 passed | 0 failed", &mut stdout_lines).await; + wait_contains("Test finished", &mut stderr_lines).await; + + // Trigger a type error + foo_file.write( + r#" + /** + * ```ts + * const sum: string = add(1, 2); + * ``` + */ + export function add(a: number, b: number) { + return a + b; + } "#, ); + assert_eq!( + skip_restarting_line(&mut stderr_lines).await, + format!("Check {foo_file_url}$3-6.ts") + ); + assert_eq!( + next_line(&mut stderr_lines).await.unwrap(), + "error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'." + ); + assert_eq!( + next_line(&mut stderr_lines).await.unwrap(), + " const sum: string = add(1, 2);" + ); + assert_eq!(next_line(&mut stderr_lines).await.unwrap(), " ~~~"); + assert_eq!( + next_line(&mut stderr_lines).await.unwrap(), + format!(" at {foo_file_url}$3-6.ts:3:11") + ); + wait_contains("Test failed", &mut stderr_lines).await; + + // Trigger a runtime error foo_file.write( r#" /** * ```ts - * import foo from "./foo.ts"; + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 4); * ``` */ - export default function foo() {} + export function add(a: number, b: number) { + return a + b; + } "#, ); - // We only need to scan for a Check file://.../foo.ts$3-6 line that - // corresponds to the documentation block being type-checked. - assert_contains!(skip_restarting_line(&mut stderr_lines).await, "foo.ts$3-6"); + wait_contains("running 1 test from", &mut stdout_lines).await; + assert_contains!( + next_line(&mut stdout_lines).await.unwrap(), + &format!("{foo_file_url}$3-8.ts ... FAILED") + ); + wait_contains("ERRORS", &mut stdout_lines).await; + wait_contains( + "error: AssertionError: Values are not equal.", + &mut stdout_lines, + ) + .await; + wait_contains("- 3", &mut stdout_lines).await; + wait_contains("+ 4", &mut stdout_lines).await; + wait_contains("FAILURES", &mut stdout_lines).await; + wait_contains("FAILED | 0 passed | 1 failed", &mut stdout_lines).await; + + wait_contains("Test failed", &mut stderr_lines).await; + + // Fix the runtime error + foo_file.write( + r#" + /** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + */ + export function add(a: number, b: number) { + return a + b; + } + "#, + ); + + wait_contains("running 1 test from", &mut stdout_lines).await; + assert_contains!( + next_line(&mut stdout_lines).await.unwrap(), + &format!("{foo_file_url}$3-8.ts ... ok") + ); + wait_contains("ok | 1 passed | 0 failed", &mut stdout_lines).await; + + wait_contains("Test finished", &mut stderr_lines).await; + check_alive_then_kill(child); } diff --git a/tests/specs/check/typecheck_doc_duplicate_identifiers/__test__.jsonc b/tests/specs/check/typecheck_doc_duplicate_identifiers/__test__.jsonc new file mode 100644 index 000000000..8596142dd --- /dev/null +++ b/tests/specs/check/typecheck_doc_duplicate_identifiers/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "check --doc --config ../../../config/deno.json mod.ts", + "exitCode": 0, + "output": "mod.out" +} diff --git a/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.out b/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.out new file mode 100644 index 000000000..d01daafa5 --- /dev/null +++ b/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.out @@ -0,0 +1,2 @@ +Check [WILDCARD]/mod.ts +Check [WILDCARD]/mod.ts$2-8.ts diff --git a/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.ts b/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.ts new file mode 100644 index 000000000..576f70240 --- /dev/null +++ b/tests/specs/check/typecheck_doc_duplicate_identifiers/mod.ts @@ -0,0 +1,13 @@ +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * const foo = createFoo(3); + * assertEquals(foo, 9); + * ``` + */ +export function createFoo(x: number): number { + return x * x; +} + +export const foo = 42; diff --git a/tests/specs/check/typecheck_doc_failure/__test__.jsonc b/tests/specs/check/typecheck_doc_failure/__test__.jsonc new file mode 100644 index 000000000..5d95f2666 --- /dev/null +++ b/tests/specs/check/typecheck_doc_failure/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "check --doc mod.ts", + "exitCode": 1, + "output": "mod.out" +} diff --git a/tests/specs/check/typecheck_doc_failure/mod.out b/tests/specs/check/typecheck_doc_failure/mod.out new file mode 100644 index 000000000..61fd5499e --- /dev/null +++ b/tests/specs/check/typecheck_doc_failure/mod.out @@ -0,0 +1,6 @@ +Check [WILDCARD]/mod.ts +Check [WILDCARD]/mod.ts$2-5.ts +error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. +const sum: string = add(1, 2); + ~~~ + at [WILDCARD]/mod.ts$2-5.ts:2:7 diff --git a/tests/specs/check/typecheck_doc_failure/mod.ts b/tests/specs/check/typecheck_doc_failure/mod.ts new file mode 100644 index 000000000..281d7f41b --- /dev/null +++ b/tests/specs/check/typecheck_doc_failure/mod.ts @@ -0,0 +1,8 @@ +/** + * ```ts + * const sum: string = add(1, 2); + * ``` + */ +export function add(a: number, b: number): number { + return a + b; +} diff --git a/tests/specs/check/typecheck_doc_in_markdown/__test__.jsonc b/tests/specs/check/typecheck_doc_in_markdown/__test__.jsonc new file mode 100644 index 000000000..00f98c4d0 --- /dev/null +++ b/tests/specs/check/typecheck_doc_in_markdown/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "check --doc-only markdown.md", + "exitCode": 1, + "output": "markdown.out" +} diff --git a/tests/specs/check/typecheck_doc_in_markdown/markdown.md b/tests/specs/check/typecheck_doc_in_markdown/markdown.md new file mode 100644 index 000000000..d18dbd108 --- /dev/null +++ b/tests/specs/check/typecheck_doc_in_markdown/markdown.md @@ -0,0 +1,31 @@ +# Documentation + +The following block does not have a language attribute and should be ignored: + +``` +This is a fenced block without attributes, it's invalid and it should be ignored. +``` + +The following block should be given a js extension on extraction: + +```js +console.log("js"); +``` + +The following block should be given a ts extension on extraction: + +```ts +console.log("ts"); +``` + +The following example contains the ignore attribute and will be ignored: + +```ts ignore +const value: Invalid = "ignored"; +``` + +The following example will trigger the type-checker to fail: + +```ts +const a: string = 42; +``` diff --git a/tests/specs/check/typecheck_doc_in_markdown/markdown.out b/tests/specs/check/typecheck_doc_in_markdown/markdown.out new file mode 100644 index 000000000..acc05dc81 --- /dev/null +++ b/tests/specs/check/typecheck_doc_in_markdown/markdown.out @@ -0,0 +1,7 @@ +Check [WILDCARD]/markdown.md$11-14.js +Check [WILDCARD]/markdown.md$17-20.ts +Check [WILDCARD]/markdown.md$29-32.ts +error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. +const a: string = 42; + ^ + at [WILDCARD]/markdown.md$29-32.ts:1:7 diff --git a/tests/specs/check/typecheck_doc_success/__test__.jsonc b/tests/specs/check/typecheck_doc_success/__test__.jsonc new file mode 100644 index 000000000..24fee3f2c --- /dev/null +++ b/tests/specs/check/typecheck_doc_success/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "check --doc mod.ts", + "exitCode": 0, + "output": "mod.out" +} diff --git a/tests/specs/check/typecheck_doc_success/mod.out b/tests/specs/check/typecheck_doc_success/mod.out new file mode 100644 index 000000000..8658af4f8 --- /dev/null +++ b/tests/specs/check/typecheck_doc_success/mod.out @@ -0,0 +1,2 @@ +Check [WILDCARD]/tests/specs/check/typecheck_doc_success/mod.ts +Check [WILDCARD]/tests/specs/check/typecheck_doc_success/mod.ts$2-5.ts diff --git a/tests/specs/check/typecheck_doc_success/mod.ts b/tests/specs/check/typecheck_doc_success/mod.ts new file mode 100644 index 000000000..793be2711 --- /dev/null +++ b/tests/specs/check/typecheck_doc_success/mod.ts @@ -0,0 +1,12 @@ +/** + * ```ts + * const sum: number = add(1, 2); + * ``` + * + * ```mts ignore + * const sum: boolean = add(3, 4); + * ``` + */ +export function add(a: number, b: number): number { + return a + b; +} diff --git a/tests/specs/test/doc/main.out b/tests/specs/test/doc/main.out index b55989f96..a25582370 100644 --- a/tests/specs/test/doc/main.out +++ b/tests/specs/test/doc/main.out @@ -4,6 +4,6 @@ Check [WILDCARD]/main.ts$14-17.ts Check [WILDCARD]/main.ts$18-21.tsx Check [WILDCARD]/main.ts$30-35.ts error: TS2367 [ERROR]: This comparison appears to be unintentional because the types 'string' and 'number' have no overlap. -console.assert(check() == 42); - ~~~~~~~~~~~~~ - at [WILDCARD]/main.ts$30-35.ts:3:16 + console.assert(check() == 42); + ~~~~~~~~~~~~~ + at [WILDCARD]/main.ts$30-35.ts:3:20 diff --git a/tests/specs/test/doc_duplicate_identifier/__test__.jsonc b/tests/specs/test/doc_duplicate_identifier/__test__.jsonc new file mode 100644 index 000000000..2a8e6aafc --- /dev/null +++ b/tests/specs/test/doc_duplicate_identifier/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --config ../../../config/deno.json main.ts", + "exitCode": 0, + "output": "main.out" +} diff --git a/tests/specs/test/doc_duplicate_identifier/main.out b/tests/specs/test/doc_duplicate_identifier/main.out new file mode 100644 index 000000000..9196405a6 --- /dev/null +++ b/tests/specs/test/doc_duplicate_identifier/main.out @@ -0,0 +1,11 @@ +Check [WILDCARD]/main.ts +Check [WILDCARD]/main.ts$11-19.ts +Check [WILDCARD]/main.ts$25-30.ts +running 0 tests from ./main.ts +running 1 test from ./main.ts$11-19.ts +[WILDCARD]/main.ts$11-19.ts ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$25-30.ts +[WILDCARD]/main.ts$25-30.ts ... ok ([WILDCARD]ms) + +ok | 2 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_duplicate_identifier/main.ts b/tests/specs/test/doc_duplicate_identifier/main.ts new file mode 100644 index 000000000..df78294d0 --- /dev/null +++ b/tests/specs/test/doc_duplicate_identifier/main.ts @@ -0,0 +1,33 @@ +// `deno test --doc` tries to convert the example code snippets into pseudo +// test files in a way that all the exported items are available without +// explicit import statements. Therefore, in the test code, you don't have to +// write like `import { add } from "./main.ts";`. +// However, this automatic import resolution might conflict with other +// explicitly declared identifiers in the test code you write. This spec test +// makes sure that such cases will not cause any issues - explicit identifiers +// take precedence. + +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * import { getModuleName, createFoo } from "./mod.ts"; + * + * const foo = createFoo(); + * assertEquals(getModuleName(), "mod.ts"); + * assertEquals(add(1, 2), foo()); + * ``` + */ +export function add(a: number, b: number): number { + return a + b; +} + +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(getModuleName(), "main.ts"); + * ``` + */ +export const getModuleName = () => "main.ts"; + +export let foo = 1234; diff --git a/tests/specs/test/doc_duplicate_identifier/mod.ts b/tests/specs/test/doc_duplicate_identifier/mod.ts new file mode 100644 index 000000000..c613a99ad --- /dev/null +++ b/tests/specs/test/doc_duplicate_identifier/mod.ts @@ -0,0 +1,7 @@ +export function getModuleName() { + return "mod.ts"; +} + +export const createFoo = () => { + return () => 3; +}; diff --git a/tests/specs/test/doc_failure/__test__.jsonc b/tests/specs/test/doc_failure/__test__.jsonc new file mode 100644 index 000000000..79f16ad8d --- /dev/null +++ b/tests/specs/test/doc_failure/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --config ../../../config/deno.json main.ts", + "exitCode": 1, + "output": "main.out" +} diff --git a/tests/specs/test/doc_failure/main.out b/tests/specs/test/doc_failure/main.out new file mode 100644 index 000000000..01b03297f --- /dev/null +++ b/tests/specs/test/doc_failure/main.out @@ -0,0 +1,60 @@ +Check [WILDCARD]/main.ts +Check [WILDCARD]/main.ts$2-9.ts +Check [WILDCARD]/main.ts$13-18.ts +Check [WILDCARD]/main.ts$24-29.ts +running 0 tests from ./main.ts +running 1 test from ./main.ts$2-9.ts +[WILDCARD]/main.ts$2-9.ts ... FAILED ([WILDCARD]ms) +running 1 test from ./main.ts$13-18.ts +[WILDCARD]/main.ts$13-18.ts ... FAILED ([WILDCARD]ms) +running 1 test from ./main.ts$24-29.ts +[WILDCARD]/main.ts$24-29.ts ... FAILED ([WILDCARD]ms) + + ERRORS + +[WILDCARD]/main.ts$13-18.ts => ./main.ts$13-18.ts:3:6 +error: AssertionError: Values are not equal. + + + [Diff] Actual / Expected + + +- 3 ++ 4 + + throw new AssertionError(message); + ^ + at assertEquals ([WILDCARD]/std/assert/equals.ts:[WILDCARD]) + at [WILDCARD]/main.ts$13-18.ts:4:5 + +[WILDCARD]/main.ts$2-9.ts => ./main.ts$2-9.ts:3:6 +error: AssertionError: Expected actual: "2.5e+0" to be close to "2": delta "5e-1" is greater than "2e-7". + throw new AssertionError( + ^ + at assertAlmostEquals ([WILDCARD]/std/assert/almost_equals.ts:[WILDCARD]) + at [WILDCARD]/main.ts$2-9.ts:6:5 + +[WILDCARD]/main.ts$24-29.ts => ./main.ts$24-29.ts:3:6 +error: AssertionError: Values are not equal. + + + [Diff] Actual / Expected + + +- 4 ++ 3 + + throw new AssertionError(message); + ^ + at assertEquals ([WILDCARD]/std/assert/equals.ts:[WILDCARD]) + at [WILDCARD]/main.ts$24-29.ts:4:5 + + FAILURES + +[WILDCARD]/main.ts$13-18.ts => ./main.ts$13-18.ts:3:6 +[WILDCARD]/main.ts$2-9.ts => ./main.ts$2-9.ts:3:6 +[WILDCARD]/main.ts$24-29.ts => ./main.ts$24-29.ts:3:6 + +FAILED | 0 passed | 3 failed ([WILDCARD]ms) + +error: Test failed diff --git a/tests/specs/test/doc_failure/main.ts b/tests/specs/test/doc_failure/main.ts new file mode 100644 index 000000000..7ec678c03 --- /dev/null +++ b/tests/specs/test/doc_failure/main.ts @@ -0,0 +1,32 @@ +/** + * ```ts + * import { assertAlmostEquals } from "@std/assert/almost-equals"; + * + * const x = sub(3, 1); + * const y = div(5, x); + * assertAlmostEquals(y, 2.0); // throws + * ``` + * @module doc + */ + +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(div(6, 2), 4); // throws + * ``` + */ +export function div(a: number, b: number): number { + return a / b; +} + +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(sub(6, 2), 3); // throws + * ``` + */ +const sub = (a: number, b: number): number => a - b; + +export { sub }; diff --git a/tests/specs/test/doc_only/__test__.jsonc b/tests/specs/test/doc_only/__test__.jsonc index 077b733a3..f40260ae7 100644 --- a/tests/specs/test/doc_only/__test__.jsonc +++ b/tests/specs/test/doc_only/__test__.jsonc @@ -1,5 +1,5 @@ { - "args": "test --doc --allow-all doc_only", + "args": "test --doc --config ../../../config/deno.json doc_only", "exitCode": 0, "output": "main.out" } diff --git a/tests/specs/test/doc_only/doc_only/mod.ts b/tests/specs/test/doc_only/doc_only/mod.ts index 467d850a2..a389302ce 100644 --- a/tests/specs/test/doc_only/doc_only/mod.ts +++ b/tests/specs/test/doc_only/doc_only/mod.ts @@ -1,6 +1,8 @@ /** * ```ts - * import "./mod.ts"; + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(42, 40 + 2); * ``` */ Deno.test("unreachable", function () { diff --git a/tests/specs/test/doc_only/main.out b/tests/specs/test/doc_only/main.out index a2eff5e89..634bd7636 100644 --- a/tests/specs/test/doc_only/main.out +++ b/tests/specs/test/doc_only/main.out @@ -1,4 +1,6 @@ -Check [WILDCARD]/doc_only/mod.ts$2-5.ts +Check [WILDCARD]/doc_only/mod.ts$2-7.ts +running 1 test from ./doc_only/mod.ts$2-7.ts +[WILDCARD]/doc_only/mod.ts$2-7.ts ... ok ([WILDCARD]ms) -ok | 0 passed | 0 failed ([WILDCARD]) +ok | 1 passed | 0 failed ([WILDCARD]ms) diff --git a/tests/specs/test/doc_permission_respected/__test__.jsonc b/tests/specs/test/doc_permission_respected/__test__.jsonc new file mode 100644 index 000000000..43c291084 --- /dev/null +++ b/tests/specs/test/doc_permission_respected/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --allow-env=PATH --reload main.ts", + "exitCode": 1, + "output": "main.out" +} diff --git a/tests/specs/test/doc_permission_respected/main.out b/tests/specs/test/doc_permission_respected/main.out new file mode 100644 index 000000000..928d4f3ce --- /dev/null +++ b/tests/specs/test/doc_permission_respected/main.out @@ -0,0 +1,25 @@ +Check [WILDCARD]/main.ts +Check [WILDCARD]/main.ts$3-6.ts +Check [WILDCARD]/main.ts$8-11.ts +running 0 tests from ./main.ts +running 1 test from ./main.ts$3-6.ts +[WILDCARD]/main.ts$3-6.ts ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$8-11.ts +[WILDCARD]/main.ts$8-11.ts ... FAILED ([WILDCARD]ms) + + ERRORS + +[WILDCARD]/main.ts$8-11.ts => ./main.ts$8-11.ts:1:6 +error: NotCapable: Requires env access to "USER", run again with the --allow-env flag + const _user = Deno.env.get("USER"); + ^ + at Object.getEnv [as get] ([WILDCARD]) + at [WILDCARD]/main.ts$8-11.ts:2:28 + + FAILURES + +[WILDCARD]/main.ts$8-11.ts => ./main.ts$8-11.ts:1:6 + +FAILED | 1 passed | 1 failed ([WILDCARD]ms) + +error: Test failed diff --git a/tests/specs/test/doc_permission_respected/main.ts b/tests/specs/test/doc_permission_respected/main.ts new file mode 100644 index 000000000..fdc7743a8 --- /dev/null +++ b/tests/specs/test/doc_permission_respected/main.ts @@ -0,0 +1,12 @@ +/** + * This should succeed because we pass `--allow-env=PATH` + * ```ts + * const _path = Deno.env.get("PATH"); + * ``` + * + * This should fail because we don't allow for env access to `USER` + * ```ts + * const _user = Deno.env.get("USER"); + * ``` + * @module doc + */ diff --git a/tests/specs/test/doc_success/__test__.jsonc b/tests/specs/test/doc_success/__test__.jsonc new file mode 100644 index 000000000..2a8e6aafc --- /dev/null +++ b/tests/specs/test/doc_success/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --config ../../../config/deno.json main.ts", + "exitCode": 0, + "output": "main.out" +} diff --git a/tests/specs/test/doc_success/main.out b/tests/specs/test/doc_success/main.out new file mode 100644 index 000000000..cf1e69b67 --- /dev/null +++ b/tests/specs/test/doc_success/main.out @@ -0,0 +1,19 @@ +Check [WILDCARD]/main.ts$8-13.js +Check [WILDCARD]/main.ts$14-19.jsx +Check [WILDCARD]/main.ts$20-25.ts +Check [WILDCARD]/main.ts$26-31.tsx +Check [WILDCARD]/main.ts$42-47.ts +running 0 tests from ./main.ts +running 1 test from ./main.ts$8-13.js +[WILDCARD]/main.ts$8-13.js ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$14-19.jsx +[WILDCARD]/main.ts$14-19.jsx ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$20-25.ts +[WILDCARD]/main.ts$20-25.ts ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$26-31.tsx +[WILDCARD]/main.ts$26-31.tsx ... ok ([WILDCARD]ms) +running 1 test from ./main.ts$42-47.ts +[WILDCARD]/main.ts$42-47.ts ... ok ([WILDCARD]ms) + +ok | 5 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_success/main.ts b/tests/specs/test/doc_success/main.ts new file mode 100644 index 000000000..6ab339ca4 --- /dev/null +++ b/tests/specs/test/doc_success/main.ts @@ -0,0 +1,50 @@ +/** + * ``` + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * ```js + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * ```jsx + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * ```tsx + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * ```text + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + * + * @module doc + */ + +/** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * + * assertEquals(add(1, 2), 3); + * ``` + */ +export function add(a: number, b: number): number { + return a + b; +} diff --git a/tests/specs/test/doc_ts_declare_global/__test__.jsonc b/tests/specs/test/doc_ts_declare_global/__test__.jsonc new file mode 100644 index 000000000..db1e607aa --- /dev/null +++ b/tests/specs/test/doc_ts_declare_global/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --config ../../../config/deno.json lib.d.ts", + "exitCode": 0, + "output": "lib.d.ts.out" +} diff --git a/tests/specs/test/doc_ts_declare_global/lib.d.ts b/tests/specs/test/doc_ts_declare_global/lib.d.ts new file mode 100644 index 000000000..a5f442910 --- /dev/null +++ b/tests/specs/test/doc_ts_declare_global/lib.d.ts @@ -0,0 +1,13 @@ +export {}; + +declare global { + /** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * import "./mod.js"; + * + * assertEquals(myFunction(1, 2), 3); + * ``` + */ + export function myFunction(a: number, b: number): number; +} diff --git a/tests/specs/test/doc_ts_declare_global/lib.d.ts.out b/tests/specs/test/doc_ts_declare_global/lib.d.ts.out new file mode 100644 index 000000000..2d6d8dbc8 --- /dev/null +++ b/tests/specs/test/doc_ts_declare_global/lib.d.ts.out @@ -0,0 +1,6 @@ +Check [WILDCARD]/lib$d$ts$5-11.ts +running 1 test from ./lib$d$ts$5-11.ts +[WILDCARD]/lib$d$ts$5-11.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_ts_declare_global/mod.js b/tests/specs/test/doc_ts_declare_global/mod.js new file mode 100644 index 000000000..1b378d2a7 --- /dev/null +++ b/tests/specs/test/doc_ts_declare_global/mod.js @@ -0,0 +1 @@ +globalThis.myFunction = (a, b) => a + b; diff --git a/tests/specs/test/doc_ts_namespace_decl/__test__.jsonc b/tests/specs/test/doc_ts_namespace_decl/__test__.jsonc new file mode 100644 index 000000000..db1e607aa --- /dev/null +++ b/tests/specs/test/doc_ts_namespace_decl/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc --config ../../../config/deno.json lib.d.ts", + "exitCode": 0, + "output": "lib.d.ts.out" +} diff --git a/tests/specs/test/doc_ts_namespace_decl/lib.d.ts b/tests/specs/test/doc_ts_namespace_decl/lib.d.ts new file mode 100644 index 000000000..e7c81cb5f --- /dev/null +++ b/tests/specs/test/doc_ts_namespace_decl/lib.d.ts @@ -0,0 +1,11 @@ +declare namespace MyNamespace { + /** + * ```ts + * import { assertEquals } from "@std/assert/equals"; + * import "./mod.js"; + * + * assertEquals(MyNamespace.add(1, 2), 3); + * ``` + */ + export function add(a: number, b: number): number; +} diff --git a/tests/specs/test/doc_ts_namespace_decl/lib.d.ts.out b/tests/specs/test/doc_ts_namespace_decl/lib.d.ts.out new file mode 100644 index 000000000..2c9e71dc4 --- /dev/null +++ b/tests/specs/test/doc_ts_namespace_decl/lib.d.ts.out @@ -0,0 +1,6 @@ +Check [WILDCARD]/lib$d$ts$3-9.ts +running 1 test from ./lib$d$ts$3-9.ts +[WILDCARD]/lib$d$ts$3-9.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_ts_namespace_decl/mod.js b/tests/specs/test/doc_ts_namespace_decl/mod.js new file mode 100644 index 000000000..6a96c342f --- /dev/null +++ b/tests/specs/test/doc_ts_namespace_decl/mod.js @@ -0,0 +1,5 @@ +globalThis.MyNamespace = { + add(a, b) { + return a + b; + }, +}; diff --git a/tests/specs/test/markdown/main.out b/tests/specs/test/markdown/main.out index 30327c72f..bdbd4325f 100644 --- a/tests/specs/test/markdown/main.out +++ b/tests/specs/test/markdown/main.out @@ -2,6 +2,6 @@ Check [WILDCARD]/main.md$11-14.js Check [WILDCARD]/main.md$17-20.ts Check [WILDCARD]/main.md$29-32.ts error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. -const a: string = 42; - ^ - at [WILDCARD]/main.md$29-32.ts:1:7 + const a: string = 42; + ^ + at [WILDCARD]/main.md$29-32.ts:2:11 diff --git a/tests/specs/test/markdown_full_block_names/main.out b/tests/specs/test/markdown_full_block_names/main.out index 9e64522dd..d7e991ce1 100644 --- a/tests/specs/test/markdown_full_block_names/main.out +++ b/tests/specs/test/markdown_full_block_names/main.out @@ -1,6 +1,6 @@ Check [WILDCARD]/main.md$5-8.js Check [WILDCARD]/main.md$17-20.ts error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. -const a: string = 42; - ^ - at [WILDCARD]/main.md$17-20.ts:1:7 + const a: string = 42; + ^ + at [WILDCARD]/main.md$17-20.ts:2:11 diff --git a/tests/specs/test/markdown_ignore_html_comment/main.out b/tests/specs/test/markdown_ignore_html_comment/main.out index 4de738845..d30ba7822 100644 --- a/tests/specs/test/markdown_ignore_html_comment/main.out +++ b/tests/specs/test/markdown_ignore_html_comment/main.out @@ -1,5 +1,5 @@ Check [WILDCARD]/main.md$34-37.ts error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. -const a: string = 42; - ^ - at [WILDCARD]/main.md$34-37.ts:1:7 + const a: string = 42; + ^ + at [WILDCARD]/main.md$34-37.ts:2:11 diff --git a/tests/specs/test/markdown_windows/main.out b/tests/specs/test/markdown_windows/main.out index 30327c72f..bdbd4325f 100644 --- a/tests/specs/test/markdown_windows/main.out +++ b/tests/specs/test/markdown_windows/main.out @@ -2,6 +2,6 @@ Check [WILDCARD]/main.md$11-14.js Check [WILDCARD]/main.md$17-20.ts Check [WILDCARD]/main.md$29-32.ts error: TS2322 [ERROR]: Type 'number' is not assignable to type 'string'. -const a: string = 42; - ^ - at [WILDCARD]/main.md$29-32.ts:1:7 + const a: string = 42; + ^ + at [WILDCARD]/main.md$29-32.ts:2:11 diff --git a/tests/specs/test/type_check_with_doc/main.out b/tests/specs/test/type_check_with_doc/main.out index 931a6a5f3..56b7ba9e8 100644 --- a/tests/specs/test/type_check_with_doc/main.out +++ b/tests/specs/test/type_check_with_doc/main.out @@ -6,8 +6,8 @@ const a: string = 1; at file://[WILDCARD]/main.ts:8:7 TS2322 [ERROR]: Type 'string' is not assignable to type 'number'. -const b: number = "1"; - ^ - at file://[WILDCARD]/main.ts$2-5.ts:1:7 + const b: number = "1"; + ^ + at file://[WILDCARD]/main.ts$2-5.ts:2:11 Found 2 errors. |