1+ let { nollup, fs, expect, rollup } = require ( '../nollup' ) ;
2+
3+ describe ( 'Error Handling' , ( ) => {
4+ it ( 'should throw syntax error if there is a problem parsing the syntax' , async ( ) => {
5+ fs . stub ( './src/main.js' , ( ) => [
6+ 'console.log(123);' ,
7+ 'var abc def = 123;'
8+ ] . join ( '\n' ) ) ;
9+ let compiled = false , thrown = false ;
10+
11+ let bundle = await nollup ( {
12+ input : './src/main.js'
13+ } ) ;
14+
15+ try {
16+ await bundle . generate ( { format : 'esm' } ) ;
17+ compiled = true ;
18+ } catch ( e ) {
19+ thrown = true ;
20+ expect ( e . name ) . to . equal ( 'SyntaxError' ) ;
21+ expect ( e . message ) . to . equal ( [
22+ 'Unexpected token (2:8)' ,
23+ ' var abc def = 123;' ,
24+ ' ^'
25+ ] . join ( '\n' ) ) ;
26+ }
27+
28+ expect ( compiled ) . to . be . false ;
29+ expect ( thrown ) . to . be . true ;
30+ } ) ;
31+
32+ it ( 'should not fail if plugin returns no sourcesContent is not returned from plugin' , async ( ) => {
33+ fs . stub ( './src/main.js' , ( ) => `console.log(123)` ) ;
34+ let thrown = false ;
35+
36+ let bundle = await nollup ( {
37+ input : './src/main.js' ,
38+ plugins : [ {
39+ transform ( code , id ) {
40+ return {
41+ code,
42+ map : {
43+ version : 3 ,
44+ file : 'main.js' ,
45+ sources : [ 'main.js' ] ,
46+ mappings : 'A;'
47+ }
48+ }
49+ }
50+ } , {
51+ transform ( code , id ) {
52+ return {
53+ code,
54+ map : {
55+ version : 3 ,
56+ file : 'main.js' ,
57+ sources : [ 'main.js' ] ,
58+ mappings : 'A;'
59+ }
60+ }
61+ }
62+ } ]
63+ } ) ;
64+
65+ await bundle . generate ( { format : 'esm' } ) ;
66+ } ) ;
67+
68+ it ( 'should show frame error property if async error thrown in plugin hook not using error function' , async ( ) => {
69+ fs . stub ( './src/main.js' , ( ) => `console.log(123)` ) ;
70+ let compiled = false , thrown = false ;
71+
72+ let bundle = await nollup ( {
73+ input : './src/main.js' ,
74+ plugins : [ {
75+ transform ( code ) {
76+ return new Promise ( resolve => {
77+ let err = new Error ( 'mycustomerror' ) ;
78+ err . frame = ' My Code Frame' ;
79+ err . loc = {
80+ file : process . cwd ( ) + '/lol.js' ,
81+ line : 1 ,
82+ column : 3
83+ }
84+ throw err ;
85+ } ) ;
86+ }
87+ } ]
88+ } ) ;
89+
90+ try {
91+ await bundle . generate ( { format : 'esm' } ) ;
92+ compiled = true ;
93+ } catch ( e ) {
94+ thrown = true ;
95+ expect ( e . name ) . to . equal ( 'Error' ) ;
96+ expect ( e . message ) . to . equal ( [
97+ 'mycustomerror' ,
98+ '\x1b[1m\x1b[37m/lol.js (1:3)\x1b[39m\x1b[22m' ,
99+ '\x1b[1m\x1b[37m My Code Frame\x1b[39m\x1b[22m'
100+ ] . join ( '\n' ) ) ;
101+ }
102+
103+ expect ( compiled ) . to . be . false ;
104+ expect ( thrown ) . to . be . true ;
105+ } ) ;
106+
107+ it ( 'should support filename and start options for error object' , async ( ) => {
108+ fs . stub ( './src/main.js' , ( ) => `console.log(123)` ) ;
109+ let compiled = false , thrown = false ;
110+
111+ let bundle = await nollup ( {
112+ input : './src/main.js' ,
113+ plugins : [ {
114+ transform ( code ) {
115+ return new Promise ( resolve => {
116+ let err = new Error ( 'mycustomerror' ) ;
117+ err . frame = ' My Code Frame' ;
118+ err . filename = process . cwd ( ) + '/lol.js' ;
119+ err . start = { line : 1 , column : 3 } ;
120+ throw err ;
121+ } ) ;
122+ }
123+ } ]
124+ } ) ;
125+
126+ try {
127+ await bundle . generate ( { format : 'esm' } ) ;
128+ compiled = true ;
129+ } catch ( e ) {
130+ thrown = true ;
131+ expect ( e . name ) . to . equal ( 'Error' ) ;
132+ expect ( e . message ) . to . equal ( [
133+ 'mycustomerror' ,
134+ '\x1b[1m\x1b[37m/lol.js (1:3)\x1b[39m\x1b[22m' ,
135+ '\x1b[1m\x1b[37m My Code Frame\x1b[39m\x1b[22m'
136+ ] . join ( '\n' ) ) ;
137+ }
138+
139+ expect ( compiled ) . to . be . false ;
140+ expect ( thrown ) . to . be . true ;
141+ } ) ;
142+
143+ it ( 'should be able to rebuild after non error() method thrown' , async ( ) => {
144+ fs . stub ( './src/main.js' , ( ) => `console.log(123)` ) ;
145+ let phase = 1 , passed = false ;
146+
147+ let bundle = await nollup ( {
148+ input : './src/main.js' ,
149+ plugins : [ {
150+ transform ( code ) {
151+ if ( phase === 1 ) {
152+ return new Promise ( resolve => {
153+ let err = new Error ( 'mycustomerror' ) ;
154+ err . frame = ' My Code Frame' ;
155+ err . loc = {
156+ file : process . cwd ( ) + '/lol.js' ,
157+ line : 1 ,
158+ column : 3
159+ }
160+ throw err ;
161+ } ) ;
162+ } else {
163+ passed = true ;
164+ }
165+ }
166+ } ]
167+ } ) ;
168+
169+ try {
170+ await bundle . generate ( { format : 'esm' } ) ;
171+ compiled = true ;
172+ } catch ( e ) {
173+ thrown = true ;
174+ expect ( e . name ) . to . equal ( 'Error' ) ;
175+ expect ( e . message ) . to . equal ( [
176+ 'mycustomerror' ,
177+ '\x1b[1m\x1b[37m/lol.js (1:3)\x1b[39m\x1b[22m' ,
178+ '\x1b[1m\x1b[37m My Code Frame\x1b[39m\x1b[22m'
179+ ] . join ( '\n' ) ) ;
180+
181+ phase ++ ;
182+ await bundle . generate ( { format : 'esm' } ) ;
183+ }
184+
185+ expect ( phase ) . to . equal ( 2 ) ;
186+ expect ( passed ) . to . be . true ;
187+ } ) ;
188+ } ) ;
0 commit comments