@@ -24,6 +24,21 @@ test('tv4 validates against a schema', function () {
2424});
2525` ;
2626
27+ const REQUIRE_FS_TESTS_SCRIPT = `
28+ test('ran before the throw', function () { expect(1).to.equal(1); });
29+ require('fs');
30+ test('never reached', function () { expect(1).to.equal(1); });
31+ ` ;
32+
33+ const UNREACHABLE_HOST_POST_RESPONSE_SCRIPT = `
34+ const axios = require('axios');
35+ await axios.get('https://unreachable.invalid/get');
36+ ` ;
37+
38+ const REQUIRE_LODASH_PRE_REQUEST_SCRIPT = `
39+ const _ = require('lodash');
40+ ` ;
41+
2742const setEditorScript = async ( page : Page , editor : CodeEditorComponent , script : string ) : Promise < void > => {
2843 await editor . focus ( ) ;
2944 await page . keyboard . press ( 'ControlOrMeta+a' ) ;
@@ -54,4 +69,66 @@ test.describe('playground script execution', () => {
5469 await expect ( page . getByText ( / P a s s e d : [ 1 - 9 ] \d * , F a i l e d : 0 / ) . first ( ) ) . toBeVisible ( ) ;
5570 await expect ( page . getByText ( / F a i l e d : [ 1 - 9 ] / ) ) . toHaveCount ( 0 ) ;
5671 } ) ;
72+
73+ test ( 'a tests script that throws shows a dismissable Test Script Error card and keeps the tests that ran' , async ( { page, playground, responsePane } ) => {
74+ await responsePane . mockUsersResponse ( JSON . stringify ( { users : [ ] } ) ) ;
75+
76+ await page . goto ( '/#/?pg=1&dock=bottom' ) ;
77+ await playground . openSidebarItem ( 'get users' ) ;
78+ await playground . selectTab ( 'tests' ) ;
79+ await setEditorScript ( page , playground . testsEditor , REQUIRE_FS_TESTS_SCRIPT ) ;
80+
81+ await responsePane . send ( ) ;
82+
83+ await expect ( responsePane . status ) . toContainText ( '200' ) ;
84+ await expect ( responsePane . scriptErrors . getByTestId ( 'error-title' ) ) . toHaveText ( 'Test Script Error' ) ;
85+ await expect ( responsePane . scriptErrors . getByTestId ( 'error-message' ) ) . toContainText ( '\'fs\' is a Node.js builtin' ) ;
86+
87+ await responsePane . switchToTab ( 'tests' ) ;
88+ await expect ( responsePane . testsPanel . getByText ( 'Tests (2), Passed: 1, Failed: 1' ) ) . toBeVisible ( ) ;
89+ await expect ( responsePane . testsPanel . getByText ( 'ran before the throw' ) ) . toBeVisible ( ) ;
90+ await expect ( responsePane . testsPanel . getByText ( 'never reached' ) ) . toHaveCount ( 0 ) ;
91+ await expect ( responsePane . testsScriptErrors . getByTestId ( 'error-title' ) ) . toHaveText ( 'Test Script Error' ) ;
92+
93+ await responsePane . testsScriptErrorsDismiss . click ( ) ;
94+ await expect ( responsePane . testsScriptErrors ) . toHaveCount ( 0 ) ;
95+ await responsePane . switchToTab ( 'response' ) ;
96+ await expect ( responsePane . scriptErrors ) . toHaveCount ( 0 ) ;
97+ await expect ( responsePane . bodyEditor . surface ) . toBeVisible ( ) ;
98+ } ) ;
99+
100+ test ( 'a pre-request script that throws shows a Pre-Request Script Error card instead of a response' , async ( { page, playground, responsePane } ) => {
101+ await page . goto ( '/#/?pg=1&dock=bottom' ) ;
102+ await playground . openSidebarItem ( 'get users' ) ;
103+ await playground . selectTab ( 'scripts' ) ;
104+ await setEditorScript ( page , playground . preRequestScriptEditor , REQUIRE_LODASH_PRE_REQUEST_SCRIPT ) ;
105+
106+ await responsePane . send ( ) ;
107+
108+ await expect ( responsePane . errorTitle ) . toHaveText ( 'Pre-Request Script Error' ) ;
109+ await expect ( responsePane . errorMessage ) . toContainText ( '\'lodash\' is only available in the Bruno desktop app\'s developer mode' ) ;
110+ await expect ( responsePane . status ) . toHaveCount ( 0 ) ;
111+ } ) ;
112+
113+ test ( 'a post-response script that throws shows a Post-Response Script Error card while the body still renders' , async ( { page, playground, responsePane } ) => {
114+ await responsePane . mockUsersResponse ( JSON . stringify ( { users : [ ] } ) ) ;
115+ await page . route ( 'https://unreachable.invalid/**' , ( route ) => route . abort ( 'namenotresolved' ) ) ;
116+
117+ await page . goto ( '/#/?pg=1&dock=bottom' ) ;
118+ await playground . openSidebarItem ( 'get users' ) ;
119+ await playground . selectTab ( 'scripts' ) ;
120+ await page . getByTestId ( 'scripts-tabs-tab-post-response' ) . click ( ) ;
121+ await setEditorScript ( page , playground . postResponseScriptEditor , UNREACHABLE_HOST_POST_RESPONSE_SCRIPT ) ;
122+
123+ await responsePane . send ( ) ;
124+
125+ await expect ( responsePane . status ) . toContainText ( '200' ) ;
126+ await expect ( responsePane . scriptErrors . getByTestId ( 'error-title' ) ) . toHaveText ( 'Post-Response Script Error' ) ;
127+ await expect ( responsePane . scriptErrors . getByTestId ( 'error-message' ) ) . toHaveText ( 'Network Error' ) ;
128+ await expect ( responsePane . bodyEditor . surface ) . toBeVisible ( ) ;
129+
130+ await responsePane . switchToTab ( 'tests' ) ;
131+ await expect ( responsePane . testsPanel . getByText ( 'Tests (1), Passed: 0, Failed: 1' ) ) . toBeVisible ( ) ;
132+ await expect ( responsePane . testsPanel . getByText ( 'Post-Response Script Error' ) . first ( ) ) . toBeVisible ( ) ;
133+ } ) ;
57134} ) ;
0 commit comments