@@ -9,6 +9,14 @@ import fs from 'fs';
99// template.get の上書き
1010template . get = function ( id ) { return fs . readFileSync ( 'test/data-' + id + '.tmpl' , 'utf-8' ) } ;
1111
12+ function templateWithCompiledFunction ( stringTemplate , data ) {
13+ data = Object . assign ( { } , data ) ;
14+ data . __context = { } ;
15+ stringTemplate += `\n<% __context.compiled = arguments.callee; %>` ;
16+ const result = template ( stringTemplate , data ) ;
17+ return [ result , data . __context . compiled ] ;
18+ }
19+
1220// --- template 基本テスト ---
1321test ( 'template renders with data' , ( t ) => {
1422 const result = template ( '<b><%= foo %></b><i><%= bar %></i>' , { foo : 'foo' , bar : 'bar' } ) ;
@@ -252,6 +260,18 @@ test('reference not found throws', (t) => {
252260 ) ;
253261} ) ;
254262
263+ test ( 'TemplateError has correct cause' , ( t ) => {
264+ let thrownError ;
265+ assert . throws ( ( ) => {
266+ template ( '<%= notfound %>' , { } ) ;
267+ } , e => {
268+ thrownError = e ;
269+ return e instanceof Error && / T e m p l a t e E r r o r : R e f e r e n c e E r r o r : n o t f o u n d i s n o t d e f i n e d / . test ( e . message ) ;
270+ } ) ;
271+ assert ( thrownError . cause instanceof ReferenceError ) ;
272+ assert . strictEqual ( thrownError . cause . message , 'notfound is not defined' ) ;
273+ } ) ;
274+
255275test ( 'template with explicit throw' , ( t ) => {
256276 assert . throws ( ( ) => template ( '<% throw new Error("fail") %>' , { } ) , e => e instanceof Error && / T e m p l a t e E r r o r : E r r o r : f a i l / . test ( e . message ) ) ;
257277} ) ;
@@ -304,3 +324,18 @@ test('template with properties script tags', (t) => {
304324 } ) ;
305325 assert . strictEqual ( result , 'foobar' ) ;
306326} ) ;
327+
328+ test ( 'template output includes sourceMappingURL comment' , ( t ) => {
329+ const [ _ , compiledFunction ] = templateWithCompiledFunction ( 'foo bar' , { } ) ;
330+ const compiledFunctionString = compiledFunction . toString ( ) ;
331+ console . log ( compiledFunctionString ) ;
332+ const match = compiledFunctionString . match ( / \n \/ \/ \# s o u r c e M a p p i n g U R L = d a t a : a p p l i c a t i o n \/ j s o n , ( .+ ) \n / ) ;
333+ assert ( match , 'sourceMappingURL comment is present and correctly formatted' ) ;
334+ const json = decodeURIComponent ( match [ 1 ] ) ;
335+ let sourceMap ;
336+ assert . doesNotThrow ( ( ) => sourceMap = JSON . parse ( json ) , 'sourceMappingURL JSON is valid' ) ;
337+ console . log ( sourceMap ) ;
338+ assert . strictEqual ( sourceMap . version , 3 , 'source map version is 3' ) ;
339+ assert . ok ( sourceMap . sourcesContent , 'source map has sourcesContent' ) ;
340+ assert . ok ( sourceMap . mappings , 'source map has mappings' ) ;
341+ } ) ;
0 commit comments