Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 061068c

Browse filesBrowse files
feat: add node-test-floating-promises example
1 parent 0366be0 commit 061068c
Copy full SHA for 061068c

File tree

5 files changed

+89
-0
lines changed
Filter options

5 files changed

+89
-0
lines changed
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Example: `node:test` and Floating Promise Detection
2+
3+
An example of using [`node:test`](https://nodejs.org/api/test.html) along with [`@typescript-eslint/no-floating-promises` rule](https://typescript-eslint.io/rules/no-floating-promises) enabled.
4+
It uses the [`allowForKnownSafeCalls` rule option](https://typescript-eslint.io/rules/no-floating-promises/#allowforknownsafecalls) to not report on calls to `test()`.
5+
6+
## Setup
7+
8+
```shell
9+
npm i
10+
```
11+
12+
## Usage
13+
14+
```shell
15+
npm run lint
16+
```
17+
18+
There should be no lint reports.
19+
20+
If you remove the `allowForKnownSafeCalls` option from `eslint.config.js`, there will be:
21+
22+
```plaintext
23+
.../index.test.ts
24+
4:1 error Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator @typescript-eslint/no-floating-promises
25+
```
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @ts-check
2+
3+
import eslint from "@eslint/js";
4+
import tseslint from "typescript-eslint";
5+
6+
export default tseslint.config(
7+
eslint.configs.recommended,
8+
tseslint.configs.recommendedTypeChecked,
9+
{
10+
languageOptions: {
11+
parserOptions: {
12+
projectService: {
13+
allowDefaultProject: ['*.config.*']
14+
},
15+
},
16+
},
17+
rules: {
18+
"@typescript-eslint/no-floating-promises": [
19+
"error", {
20+
"allowForKnownSafeCalls": [
21+
{ "from": "package", "name": "test", "package": "node:test" }
22+
]
23+
}
24+
]
25+
}
26+
}
27+
);
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { strict as assert } from 'node:assert';
2+
import { test } from 'node:test';
3+
4+
test('synchronous passing test', () => {
5+
assert.strictEqual(1, 1);
6+
});
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "node-test-floating-promises",
3+
"version": "0.0.0",
4+
"description": "Example of using `node:test` with `@typescript-eslint/no-floating-promises`'s `allowForKnownSafeCalls` option.",
5+
"main": "index.ts",
6+
"repository": {
7+
"directory": "packages/node-test-floating-promises",
8+
"type": "git",
9+
"url": "https://github.com/typescript-eslint/typescript-eslint-examples"
10+
},
11+
"license": "MIT",
12+
"devDependencies": {
13+
"@types/node": "^22.10.7",
14+
"eslint": "^9.16.0",
15+
"typescript": "^5.7.2",
16+
"typescript-eslint": "^8.18.0"
17+
},
18+
"scripts": {
19+
"lint": "eslint .",
20+
"tsc": "tsc"
21+
},
22+
"type": "module"
23+
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"module": "NodeNext",
4+
"moduleResolution": "NodeNext",
5+
"noEmit": true,
6+
"strict": true
7+
},
8+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.