From 15fae197482a9ac84e0227200ef8f4d0d4e4bd33 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Thu, 14 Nov 2024 14:04:32 +0900 Subject: fix(cli): preserve comments in doc tests (#26828) This commit makes comments in code snippets in JSDoc or markdown preserved when they are executed as tests. In particular, this is needed to get TypeScript special comments such as `@ts-ignore` or `@ts-expect-error` to work correctly. Fixes #26728 --- tests/specs/test/doc_ts_expect_error/__test__.jsonc | 5 +++++ tests/specs/test/doc_ts_expect_error/mod.out | 8 ++++++++ tests/specs/test/doc_ts_expect_error/mod.ts | 13 +++++++++++++ tests/specs/test/markdown_ts_expect_error/__test__.jsonc | 5 +++++ tests/specs/test/markdown_ts_expect_error/main.md | 8 ++++++++ tests/specs/test/markdown_ts_expect_error/main.out | 6 ++++++ 6 files changed, 45 insertions(+) create mode 100644 tests/specs/test/doc_ts_expect_error/__test__.jsonc create mode 100644 tests/specs/test/doc_ts_expect_error/mod.out create mode 100644 tests/specs/test/doc_ts_expect_error/mod.ts create mode 100644 tests/specs/test/markdown_ts_expect_error/__test__.jsonc create mode 100644 tests/specs/test/markdown_ts_expect_error/main.md create mode 100644 tests/specs/test/markdown_ts_expect_error/main.out (limited to 'tests') diff --git a/tests/specs/test/doc_ts_expect_error/__test__.jsonc b/tests/specs/test/doc_ts_expect_error/__test__.jsonc new file mode 100644 index 000000000..ba64887a3 --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc mod.ts", + "exitCode": 0, + "output": "mod.out" +} diff --git a/tests/specs/test/doc_ts_expect_error/mod.out b/tests/specs/test/doc_ts_expect_error/mod.out new file mode 100644 index 000000000..d464d13d7 --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/mod.out @@ -0,0 +1,8 @@ +Check [WILDCARD]/mod.ts +Check [WILDCARD]/mod.ts$2-10.ts +running 0 tests from ./mod.ts +running 1 test from ./mod.ts$2-10.ts +[WILDCARD]/mod.ts$2-10.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) + diff --git a/tests/specs/test/doc_ts_expect_error/mod.ts b/tests/specs/test/doc_ts_expect_error/mod.ts new file mode 100644 index 000000000..eeace602a --- /dev/null +++ b/tests/specs/test/doc_ts_expect_error/mod.ts @@ -0,0 +1,13 @@ +/** + * ```ts + * import { add } from "./mod.ts"; + * + * add(1, 2); + * + * // @ts-expect-error: can only add numbers + * add('1', '2'); + * ``` + */ +export function add(first: number, second: number) { + return first + second; +} diff --git a/tests/specs/test/markdown_ts_expect_error/__test__.jsonc b/tests/specs/test/markdown_ts_expect_error/__test__.jsonc new file mode 100644 index 000000000..ad73f6df6 --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "test --doc main.md", + "exitCode": 0, + "output": "main.out" +} diff --git a/tests/specs/test/markdown_ts_expect_error/main.md b/tests/specs/test/markdown_ts_expect_error/main.md new file mode 100644 index 000000000..9be70e919 --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/main.md @@ -0,0 +1,8 @@ +# Documentation + +This test case checks if `@ts-expect-error` comment works as expected. + +```ts +// @ts-expect-error +const a: string = 42; +``` diff --git a/tests/specs/test/markdown_ts_expect_error/main.out b/tests/specs/test/markdown_ts_expect_error/main.out new file mode 100644 index 000000000..65990cd3f --- /dev/null +++ b/tests/specs/test/markdown_ts_expect_error/main.out @@ -0,0 +1,6 @@ +Check [WILDCARD]/main.md$5-9.ts +running 1 test from ./main.md$5-9.ts +[WILDCARD]/main.md$5-9.ts ... ok ([WILDCARD]ms) + +ok | 1 passed | 0 failed ([WILDCARD]ms) + -- cgit v1.2.3