@@ -4,7 +4,7 @@ import { describe, expect, it } from "vitest";
44
55import { name , rule } from "#/rules/no-throw-statements" ;
66
7- import { esLatestConfig } from "../utils/configs" ;
7+ import { esLatestConfig , typescriptConfig } from "../utils/configs" ;
88
99describe ( name , ( ) => {
1010 describe ( "javascript - es latest" , ( ) => {
@@ -57,5 +57,120 @@ describe(name, () => {
5757 } ) ;
5858 expect ( invalidResult . messages ) . toMatchSnapshot ( ) ;
5959 } ) ;
60+
61+ describe ( "options" , ( ) => {
62+ describe ( "allowToRejectPromises" , ( ) => {
63+ it ( "doesn't report throw statements in async functions" , ( ) => {
64+ valid ( {
65+ code : dedent `
66+ async function foo() {
67+ throw new Error();
68+ }
69+ ` ,
70+ options : [ { allowToRejectPromises : true } ] ,
71+ } ) ;
72+ } ) ;
73+
74+ it ( "doesn't report throw statements in try without catch in async functions" , ( ) => {
75+ valid ( {
76+ code : dedent `
77+ async function foo() {
78+ try {
79+ throw new Error("hello");
80+ } finally {
81+ console.log("world");
82+ }
83+ }
84+ ` ,
85+ options : [ { allowToRejectPromises : true } ] ,
86+ } ) ;
87+ } ) ;
88+
89+ it ( "reports throw statements in try with catch in async functions" , ( ) => {
90+ const invalidResult = invalid ( {
91+ code : dedent `
92+ async function foo() {
93+ try {
94+ throw new Error("hello world");
95+ } catch (e) {
96+ console.log(e);
97+ }
98+ }
99+ ` ,
100+ errors : [ "generic" ] ,
101+ options : [ { allowToRejectPromises : true } ] ,
102+ } ) ;
103+ expect ( invalidResult . messages ) . toMatchSnapshot ( ) ;
104+ } ) ;
105+
106+ it ( "reports throw statements in functions nested in async functions" , ( ) => {
107+ const invalidResult = invalid ( {
108+ code : dedent `
109+ async function foo() {
110+ function bar() {
111+ throw new Error();
112+ }
113+ }
114+ ` ,
115+ errors : [ "generic" ] ,
116+ options : [ { allowToRejectPromises : true } ] ,
117+ } ) ;
118+ expect ( invalidResult . messages ) . toMatchSnapshot ( ) ;
119+ } ) ;
120+ } ) ;
121+ } ) ;
122+ } ) ;
123+
124+ describe ( "typescript" , ( ) => {
125+ const { valid, invalid } = createRuleTester ( {
126+ name,
127+ rule,
128+ configs : typescriptConfig ,
129+ } ) ;
130+
131+ describe ( "options" , ( ) => {
132+ describe ( "allowToRejectPromises" , ( ) => {
133+ it ( "doesn't report throw statements in promise then handlers" , ( ) => {
134+ valid ( {
135+ code : dedent `
136+ function foo() {
137+ Promise.resolve().then(() => {
138+ throw new Error();
139+ });
140+ }
141+ ` ,
142+ options : [ { allowToRejectPromises : true } ] ,
143+ } ) ;
144+ } ) ;
145+
146+ it ( "doesn't report throw statements in promise catch handlers" , ( ) => {
147+ valid ( {
148+ code : dedent `
149+ function foo() {
150+ Promise.resolve().catch(() => {
151+ throw new Error();
152+ });
153+ }
154+ ` ,
155+ options : [ { allowToRejectPromises : true } ] ,
156+ } ) ;
157+ } ) ;
158+
159+ it ( "doesn't report throw statements in promise handlers" , ( ) => {
160+ valid ( {
161+ code : dedent `
162+ function foo() {
163+ Promise.resolve().then(() => {
164+ throw new Error();
165+ }, () => {
166+ throw new Error();
167+ });
168+ }
169+ ` ,
170+ options : [ { allowToRejectPromises : true } ] ,
171+ } ) ;
172+ } ) ;
173+ } ) ;
174+ } ) ;
60175 } ) ;
61176} ) ;
0 commit comments