1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-18 03:44:05 -05:00
denoland-deno/tests/specs
Yusuke Tanaka d5c00ef50e
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
2024-09-17 21:35:48 -07:00
..
add feat(add/install): Flag to add dev dependency to package.json (#25495) 2024-09-07 02:22:27 -07:00
bench feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
bundle/removed BREAKING: remove deno bundle (#25339) 2024-09-02 17:27:37 +02:00
cache fix: update hint for deno add <package> (#25455) 2024-09-05 11:08:22 +02:00
cert BREAKING: remove deno bundle (#25339) 2024-09-02 17:27:37 +02:00
check feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
clean/general feat(clean): add progress bar (#25026) 2024-08-14 13:04:07 +02:00
cli/help_and_version_broken_pipe fix: panic when piping "deno help" or "deno --version" (#22917) 2024-07-09 22:57:00 +00:00
compile feat(cli): use NotCapable error for permission errors (#25431) 2024-09-10 11:12:24 -07:00
coverage fix(coverage): Error if no files are included in the report (#22952) 2024-03-15 20:58:57 -07:00
doc feat: include version number in all --json based outputs (#25335) 2024-09-05 08:51:40 +00:00
eval chore: deprecate eval itests (#25382) 2024-09-03 15:14:19 +00:00
flags feat: add lowercase -v version flag (#23750) 2024-05-23 00:20:20 +00:00
fmt fix: update malva in deno to support astro css comments (#25553) 2024-09-10 12:54:43 +02:00
future chore: cleanup remaining internals.future code (#25624) 2024-09-16 09:28:35 +10:00
import_map/import_map_config refactor: move importMap with imports/scopes diagnostic to deno_config (#24553) 2024-07-12 12:48:53 -04:00
info test: rewrite some ignored tests to spec tests (#25652) 2024-09-16 15:39:58 +02:00
info_tests chore: move info itests (#25659) 2024-09-16 17:08:00 +00:00
init/lib fix(init): use bare specifier for jsr:@std/assert (#24581) 2024-07-14 13:22:43 -04:00
install fix: remove recently added deno.json node_modules aliasing (#25542) 2024-09-09 20:19:29 +00:00
jsr fix: update dry run success message (#24885) 2024-08-06 08:14:06 +00:00
jupyter/install_command refactor(jupyter): use runtimelib for Jupyter structures and directory paths (#23826) 2024-05-21 22:35:21 +02:00
lint chore: deprecate lint itests (#25655) 2024-09-16 17:45:25 +00:00
lockfile test: reenable frozen_lockfile tests (#25381) 2024-09-03 06:43:10 -04:00
node chore: remove DENO_FUTURE=1 from spec tests (#25329) 2024-08-31 21:25:32 +00:00
node_compat_tests chore: deprecate node compat itests (#25573) 2024-09-16 09:53:20 +01:00
npm fix(npm): better error handling for remote npm deps (#25670) 2024-09-16 21:23:40 +00:00
permission feat: update warning message for --allow-run with no list (#25693) 2024-09-18 00:32:52 +02:00
publish feat: TypeScript 5.6 and npm:@types/node@22 (#25614) 2024-09-14 11:58:47 +01:00
remove feat(add): Add npm packages to package.json if present (#25477) 2024-09-06 17:18:13 +00:00
run feat: update warning message for --allow-run with no list (#25693) 2024-09-18 00:32:52 +02:00
serve feat(serve): Support second parameter in deno serve (#25606) 2024-09-12 23:32:28 +00:00
task fix(cli): deno task exit with status 0 (#25637) 2024-09-15 11:29:24 +05:30
test feat(cli): evaluate code snippets in JSDoc and markdown (#25220) 2024-09-17 21:35:48 -07:00
upgrade/invalid_version feat(upgrade): better error message on failure (#25503) 2024-09-11 17:19:45 +02:00
vendor/removed BREAKING: remove deno vendor (#25343) 2024-09-03 17:00:57 +10:00
worker chore: Deprecate worker itests (#25514) 2024-09-09 11:07:41 -04:00
workspaces fix: remove recently added deno.json node_modules aliasing (#25542) 2024-09-09 20:19:29 +00:00
mod.rs test: add supporting for ignoring spec tests (#25242) 2024-08-28 00:19:29 +00:00
README.md chore: support -- --nocapture in the spec tests (#24113) 2024-06-06 08:37:13 -04:00
schema.json test: add supporting for ignoring spec tests (#25242) 2024-08-28 00:19:29 +00:00

specs

These are integration tests that execute the deno binary. They supersede the itest macro found in the tests/integration folder and are the preferred way of writing tests that use the deno binary.

Structure

Tests must have the following directory structure:

tests/specs/<category_name>/<test_name>/__test__.json

Test filtering

To run a specific test, run:

cargo test specs::category_name::test_name

Or just the following, though it might run other tests:

cargo test test_name

To run showing the output of every test use -- --nocapture (note: this will cause tests to run sequentially instead of in parallel):

cargo test test_name -- --nocapture

__test__.json file

This file describes the test(s) to execute and the steps to execute. A basic example looks like:

{
  "args": "run main.js",
  "output": "main.out"
}

This will run deno run main.js then assert that the output matches the text in main.out.

Or another example that runs multiple steps:

{
  "tempDir": true,
  "steps": [{
    "args": "cache main.ts",
    "output": "cache.out"
  }, {
    "args": "run main.ts",
    "output": "error.out",
    "exitCode": 1
  }]
}

Or if you want to run several tests at the same time:

{
  "tests": {
    "ignore_dir": {
      "args": "run script.ts",
      "output": "script.out"
    },
    "some_other_test": {
      "args": "run other.ts",
      "output": "other.out"
    }
  }
}

Top level properties

  • repeat (number) - Number of times to repeat a test.
  • tempDir (boolean) - Copy all the non-test files to a temporary directory and execute the command in that temporary directory.
    • By default, tests are executed with a current working directory of the test, but this may not be desirable for tests such as ones that create a node_modules directory.

Step properties

When writing a single step, these may be at the top level rather than nested in a "steps" array or "tests" object.

  • args - A string (that will be spilt on whitespace into an args array) or an array of arguments.
  • output - Path to use to assert the output or text (must end with an .out extension) or text to pattern match against the output.
  • flaky - Step should be repeated until success a maximum of 3 times.
  • if ("windows", "linux", "mac", "unix") - Whether to run this step.
  • exitCode (number) - Expected exit code.

Auto-complete

To get auto-complete for these files, add the following to a local .vscode/settings.json file:

{
  "json.schemas": [{
    "fileMatch": [
      "__test__.jsonc"
    ],
    "url": "./tests/specs/schema.json"
  }]
}

.out files

.out files are used to assert the output when running a test or test step.

Within the file, you can use the following for matching:

  • [WILDCARD] - match any text at the wildcard
  • [WILDLINE] - match any text on the current line
  • [WILDCHAR] - match the next character
  • [WILDCHARS(5)] - match any of the next 5 characters
  • [UNORDERED_START] followed by many lines then [UNORDERED_END] will match the lines in any order (useful for non-deterministic output)
  • [# example] - line comments start with [# and end with ]