UNPKG

16.2 kBJavaScriptView Raw
1var assert = require("assert");
2var transpile = require("../main");
3var convert = require("./convert");
4var assign = require("lodash/assign");
5var doTranspile = require("./do-transpile");
6
7// converters
8var es62cjs = require("../lib/es6_cjs");
9var cjs2steal = require("../lib/cjs_steal");
10var amd2cjs = require("../lib/amd_cjs");
11var steal2amd = require("../lib/steal_amd");
12var global2amd = require("../lib/global_amd");
13var amd2amd = require("../lib/amd_amd");
14
15describe("es6 - cjs", function() {
16 it("should work", function() {
17 return convert({
18 converter: es62cjs,
19 sourceFileName: "es6",
20 expectedFileName: "es6_cjs"
21 });
22 });
23
24 it("options argument is not required", function(){
25 var res = transpile.to({
26 name: "foo",
27 source: "var GoogleMapReact = require('google-map-react')",
28 metadata: { format: "es6" }
29 }, "cjs");
30
31 var e = "'use strict';\nvar GoogleMapReact = require('google-map-react');";
32 assert.equal(res.code, e);
33 })
34
35 it("works if global.System is something else (#14)", function() {
36 global.System = {};
37
38 return convert({
39 converter: es62cjs,
40 sourceFileName: "es6",
41 expectedFileName: "es6_cjs"
42 });
43 });
44
45 it("works with babel", function() {
46 return convert({
47 converter: es62cjs,
48 sourceFileName: "es6",
49 expectedFileName: "es6_cjs_babel",
50 options: {
51 transpiler: "babel",
52 babelOptions: {
53 optional: {},
54 blacklist: [],
55 whitelist: []
56 }
57 }
58 });
59 });
60});
61
62describe("cjs - steal", function() {
63 it("should work", function() {
64 return convert({
65 converter: cjs2steal,
66 sourceFileName: "cjs",
67 expectedFileName: "cjs_steal"
68 });
69 });
70
71 it("should work with objects", function() {
72 return convert({
73 converter: cjs2steal,
74 sourceFileName: "cjs2",
75 expectedFileName: "cjs2_steal"
76 });
77 });
78
79 it("should work with npm names", function() {
80 return convert({
81 converter: cjs2steal,
82 sourceFileName: "cjs_npm",
83 expectedFileName: "cjs_npm_steal"
84 });
85 });
86});
87
88describe("amd - cjs", function() {
89 it("should work", function() {
90 return convert({
91 converter: amd2cjs,
92 sourceFileName: "amd",
93 expectedFileName: "amd_cjs"
94 });
95 });
96});
97
98describe("steal - amd", function() {
99 it("should work", function() {
100 return convert({
101 converter: steal2amd,
102 sourceFileName: "steal",
103 expectedFileName: "steal_amd"
104 });
105 });
106
107 it("should work with namedDefines", function() {
108 return convert({
109 converter: steal2amd,
110 sourceFileName: "steal",
111 expectedFileName: "steal_amd_named_defines",
112 options: { namedDefines: true }
113 });
114 });
115
116 it("should leave nested steals alone", function() {
117 return convert({
118 converter: steal2amd,
119 sourceFileName: "nested_steal",
120 expectedFileName: "nested_steal_amd"
121 });
122 });
123});
124
125describe("global - amd", function() {
126 it("should work", function() {
127 var load = { metadata: { format: "global", exports: "GLOBAL" } };
128
129 return convert({
130 load: load,
131 converter: global2amd,
132 sourceFileName: "global",
133 expectedFileName: "global_amd"
134 });
135 });
136
137 it("should include the export name", function() {
138 var load = {
139 metadata: {
140 format: "global",
141 deps: ["foo"],
142 exports: "GLOBAL"
143 }
144 };
145
146 return convert({
147 load: load,
148 converter: global2amd,
149 sourceFileName: "global",
150 expectedFileName: "global_amd_export"
151 });
152 });
153
154 it("if no export is defined do not pass the exportname", function() {
155 var load = {
156 metadata: {
157 format: "global",
158 deps: []
159 }
160 };
161
162 return convert({
163 load: load,
164 converter: global2amd,
165 sourceFileName: "global",
166 expectedFileName: "global_amd_noexport"
167 });
168 });
169
170 it("exports: false passes the value false", function() {
171 var load = {
172 metadata: {
173 format: "global",
174 exports: false
175 }
176 };
177
178 return convert({
179 load: load,
180 converter: global2amd,
181 sourceFileName: "global",
182 expectedFileName: "global_amd_exportfalse"
183 });
184 });
185
186 it("works with an init function passed", function() {
187 var load = {
188 metadata: {
189 format: "global",
190 init: function(){
191 return window.FOO; // eslint-disable-line
192 }
193 }
194 };
195 return convert({
196 load: load,
197 converter: global2amd,
198 sourceFileName: "global",
199 expectedFileName: "global_amd_init"
200 });
201 });
202
203 it("works with 'eval': 'script'", function() {
204 var load = {
205 metadata: {
206 format: "global",
207 eval: "script"
208 }
209 };
210 return convert({
211 load: load,
212 converter: global2amd,
213 sourceFileName: "global",
214 expectedFileName: "global_amd_eval"
215 });
216 });
217});
218
219describe("transpile", function(){
220 it("able to steal to cjs", function() {
221 var res = transpile.able("steal", "cjs");
222 assert.deepEqual(res, ["steal", "amd"]);
223 });
224
225 it("able to steal to amd", function() {
226 var res = transpile.able("steal", "amd");
227 assert.deepEqual(res, ["steal"]);
228 });
229
230 it("able to es6 to amd", function() {
231 var res = transpile.able("es6", "amd");
232 assert.deepEqual(res, ["es6"]);
233 });
234
235 it("to steal to cjs", function() {
236 return doTranspile({
237 moduleFormat: "steal",
238 resultModuleFormat: "cjs",
239 sourceFileName: "steal",
240 expectedFileName: "steal_cjs"
241 });
242 });
243
244 it("able to global to amd", function() {
245 return doTranspile({
246 moduleFormat: "global",
247 resultModuleFormat: "amd",
248 sourceFileName: "global",
249 expectedFileName: "global_amd_with_format"
250 });
251 });
252
253 it("able to steal to cjs with missing args", function() {
254 return doTranspile({
255 moduleFormat: "steal",
256 resultModuleFormat: "cjs",
257 sourceFileName: "steal_no_value_arg",
258 expectedFileName: "steal_no_value_arg_cjs"
259 });
260 });
261});
262
263describe("amd - amd", function() {
264 it("should work", function() {
265 return convert({
266 converter: amd2amd,
267 sourceFileName: "amd",
268 expectedFileName: "amd_amd",
269 options: { namedDefines: true }
270 });
271 });
272
273 it("works with transpile", function() {
274 return doTranspile({
275 moduleFormat: "amd",
276 resultModuleFormat: "amd",
277 sourceFileName: "amd",
278 expectedFileName: "amd_amd",
279 options: { namedDefines: true }
280 });
281 });
282
283 it("should work with a normalizeMap", function() {
284 var options = {
285 normalizeMap: {
286 './baz': 'baz'
287 },
288
289 namedDefines: true
290 };
291
292 return convert({
293 options: options,
294 converter: amd2amd,
295 sourceFileName: "amd_deps",
296 expectedFileName: "amd_deps"
297 });
298 });
299
300 it("should rename the define name if able", function() {
301 return convert({
302 converter: amd2amd,
303 sourceFileName: "amd_named",
304 expectedFileName: "amd_named_amd",
305 options: { namedDefines: true },
306 load: { name: "redefined" }
307 });
308 });
309});
310
311describe("metadata.format", function(){
312 it("should be detected from amd source", function() {
313 return doTranspile({
314 moduleFormat: undefined,
315 resultModuleFormat: "amd",
316 sourceFileName: "amd",
317 expectedFileName: "amd_amd",
318 options: { namedDefines: true }
319 });
320 });
321
322 it("should be detected from steal source", function() {
323 return doTranspile({
324 moduleFormat: undefined,
325 resultModuleFormat: "cjs",
326 sourceFileName: "steal",
327 expectedFileName: "steal_cjs"
328 });
329 });
330
331 it("should be detected from es6 source", function() {
332 return doTranspile({
333 moduleFormat: undefined,
334 resultModuleFormat: "cjs",
335 sourceFileName: "es6",
336 expectedFileName: "es6_cjs"
337 });
338 });
339});
340
341describe("es6 - amd", function() {
342 it("should work with bangs", function() {
343 return doTranspile({
344 moduleFormat: "es6",
345 resultModuleFormat: "amd",
346 sourceFileName: "es_with_bang",
347 expectedFileName: "es_with_bang_amd",
348 options: { namedDefines: true }
349 });
350 });
351
352 it("should work with babel", function() {
353 return doTranspile({
354 moduleFormat: "es6",
355 resultModuleFormat: "amd",
356 sourceFileName: "es6",
357 expectedFileName: "es6_amd_babel",
358 options: { transpiler: "babel" }
359 });
360 });
361
362 it("should work with babel-standalone included plugins", function() {
363 return doTranspile({
364 moduleFormat: "es6",
365 resultModuleFormat: "amd",
366 sourceFileName: "es6_and_decorators",
367 expectedFileName: "es6_decorators_amd_babel",
368 options: {
369 transpiler: "babel",
370 babelOptions: {
371 plugins: ["transform-decorators-legacy"]
372 }
373 }
374 });
375 });
376
377 it("should work with babel plugins NOT included in babel-standalone", function() {
378 return doTranspile({
379 moduleFormat: "es6",
380 resultModuleFormat: "amd",
381 sourceFileName: "es6",
382 expectedFileName: "es6_amd_babel_and_plugin",
383 options: {
384 transpiler: "babel",
385 babelOptions: {
386 plugins: [ require("./babel-plugin") ]
387 }
388 }
389 });
390 });
391
392 it("should work with babel presets NOT built in babel-standalone", function() {
393 return doTranspile({
394 moduleFormat: "es6",
395 resultModuleFormat: "amd",
396 sourceFileName: "es6",
397 expectedFileName: "es6_amd_babel_and_plugin",
398 options: {
399 transpiler: "babel",
400 babelOptions: {
401 presets: [ require("babel-preset-steal-test") ]
402 }
403 }
404 });
405 });
406
407 it("presets override the default ones", function() {
408 return doTranspile({
409 sourceFileName: "es6",
410 moduleFormat: "es6",
411 expectedFileName: "es6_amd_babel_loose",
412 resultModuleFormat: "amd",
413 options: {
414 transpiler: "babel",
415 babelOptions: {
416 presets: [
417 ["es2015", { loose: true }]
418 ]
419 }
420 }
421 });
422 });
423
424 it("should work with traceurOptions", function() {
425 return doTranspile({
426 sourceFileName: "es6",
427 moduleFormat: "es6",
428 expectedFileName: "es_with_traceur_options",
429 resultModuleFormat: "amd",
430 options: {
431 traceurOptions: {
432 properTailCalls: true
433 }
434 }
435 });
436 });
437
438 it("works with babel and circular dependencies", function() {
439 return doTranspile({
440 moduleFormat: "es6",
441 sourceFileName: "es6",
442 load: { circular: true },
443 resultModuleFormat: "amd",
444 options: { transpiler: "babel" },
445 expectedFileName: "es6_amd_babel_circular"
446 });
447 });
448});
449
450describe("normalize options", function() {
451 it("steal - amd + normalizeMap", function() {
452 var options = {
453 normalizeMap: {
454 "./baz": "baz"
455 },
456 namedDefines: true
457 };
458
459 return convert({
460 options: options,
461 converter: steal2amd,
462 sourceFileName: "steal_deps",
463 expectedFileName: "steal_amd_dep"
464 });
465 });
466
467 it("steal - amd + normalize", function() {
468 return convert({
469 converter: steal2amd,
470 sourceFileName: "steal_deps",
471 expectedFileName: "steal_amd_normalize",
472 options: {
473 normalizeMap: {
474 "./baz": "baz"
475 },
476 normalize: function(name){
477 var parts = name.split("/");
478 var len = parts.length;
479
480 if (parts[len-1] === parts[len-2]) {
481 parts.pop();
482 }
483 return parts.join("/");
484 },
485 namedDefines: true
486 }
487 });
488 });
489
490 it("es6 - cjs + normalize", function() {
491 return convert({
492 sourceFileName: "es_needing_normalize",
493 converter: es62cjs,
494 expectedFileName: "es_needing_normalize_cjs",
495 options: {
496 normalize: function(name) {
497 if(name.lastIndexOf("/") === name.length - 1) {
498 var parts = name.split("/");
499 parts[parts.length - 1] = parts[parts.length - 2];
500 return parts.join("/");
501 } else if( name.indexOf("!") >= 0 ) {
502 return name.substr(0, name.indexOf("!") );
503 }
504 return name;
505 }
506 }
507 });
508 });
509
510 it("amd - cjs + normalize", function() {
511 return convert({
512 sourceFileName: "amd_needing_normalize",
513 converter: amd2cjs,
514 expectedFileName: "amd_needing_normalize_cjs",
515 options: {
516 normalize: function(name){
517 if(name.lastIndexOf("/") === name.length - 1) {
518 var parts = name.split("/");
519 parts[parts.length - 1] = parts[parts.length - 2];
520 return parts.join("/");
521 } else if( name.indexOf("!") >= 0 ) {
522 return name.substr(name.indexOf("!")+1);
523 }
524 return name;
525 }
526 }
527 });
528 });
529
530 it("steal - cjs + normalize",function() {
531 return doTranspile({
532 moduleFormat: "steal",
533 resultModuleFormat: "cjs",
534 sourceFileName: "steal_needing_normalize",
535 expectedFileName: "steal_needing_normalize_cjs",
536 options: {
537 normalize: function(name){
538 return name+"-normalized";
539 }
540 }
541 });
542 });
543
544 it("cjs - cjs + normalize", function() {
545 return doTranspile({
546 moduleFormat: "cjs",
547 resultModuleFormat: "cjs",
548 sourceFileName: "cjs_needing_normalize",
549 expectedFileName: "cjs_needing_normalize_cjs",
550 options: {
551 normalize: function(name) {
552 return name + "-normalized";
553 }
554 }
555 });
556 });
557});
558
559describe("transpile options", function() {
560 it("es6 - cjs + normalize",function() {
561 var options = {
562 transpile: function() {
563 return {
564 code: 'require("foo")'
565 };
566 }
567 };
568
569 return convert({
570 options: options,
571 converter: es62cjs,
572 sourceFileName: "es_needing_normalize",
573 expectedFileName: "es_self_transpile"
574 });
575 });
576});
577
578describe("Source Maps", function() {
579 var normal = { sourceMaps: true };
580 var content = { sourceMaps: true, sourceMapsContent: true };
581
582 [normal, content].forEach(function(opts) {
583 opts.baseURL = __dirname + "/tests";
584 opts.sourceRoot = "../";
585 });
586
587 describe("External file", function() {
588 it("steal - amd works", function() {
589 return doTranspile({
590 options: normal,
591 moduleFormat: "steal",
592 resultModuleFormat: "amd",
593 sourceFileName: "steal",
594 expectedFileName: "steal_amd_sm"
595 });
596 });
597
598 it("steal - cjs works", function() {
599 return doTranspile({
600 options: normal,
601 moduleFormat: "steal",
602 resultModuleFormat: "cjs",
603 sourceFileName: "steal",
604 expectedFileName: "steal_cjs_sm"
605 });
606 });
607
608 it("cjs - amd works", function() {
609 return doTranspile({
610 options: normal,
611 moduleFormat: "cjs",
612 resultModuleFormat: "amd",
613 sourceFileName: "cjs",
614 expectedFileName: "cjs_amd_sm"
615 });
616 });
617
618 it("amd - cjs works", function() {
619 return doTranspile({
620 options: normal,
621 moduleFormat: "amd",
622 resultModuleFormat: "cjs",
623 sourceFileName: "amd",
624 expectedFileName: "amd_cjs_sm"
625 });
626 });
627
628 it("amd - amd works", function() {
629 return doTranspile({
630 options: normal,
631 moduleFormat: "amd",
632 resultModuleFormat: "amd",
633 sourceFileName: "amd",
634 expectedFileName: "amd_amd_sm"
635 });
636 });
637
638 it("es6(traceur) - amd works", function() {
639 return doTranspile({
640 options: normal,
641 moduleFormat: "es6",
642 resultModuleFormat: "amd",
643 sourceFileName: "es6",
644 expectedFileName: "es6_amd_sm"
645 });
646 });
647
648 it("es6(babel) - amd works", function() {
649 var opts = assign({ transpiler: "babel" }, normal);
650
651 return doTranspile({
652 options: opts,
653 moduleFormat: "es6",
654 resultModuleFormat: "amd",
655 sourceFileName: "es6",
656 expectedFileName: "es62_amd_sm"
657 });
658 });
659
660 it("es6(traceur) - cjs works", function() {
661 return doTranspile({
662 options: normal,
663 moduleFormat: "es6",
664 resultModuleFormat: "cjs",
665 sourceFileName: "es6",
666 expectedFileName: "es6_cjs_sm"
667 });
668 });
669
670 it("es6(babel) - cjs works", function() {
671 var opts = assign({ transpiler: "babel" }, normal);
672
673 return doTranspile({
674 options: opts,
675 moduleFormat: "es6",
676 resultModuleFormat: "cjs",
677 sourceFileName: "es6",
678 expectedFileName: "es62_cjs_sm"
679 });
680 });
681 });
682
683 describe("Content included", function() {
684 it("steal - amd works", function() {
685 doTranspile({
686 options: content,
687 moduleFormat: "steal",
688 resultModuleFormat: "amd",
689 sourceFileName: "steal",
690 expectedFileName: "steal_amd_cont_sm"
691 });
692 });
693
694 it("steal - cjs works", function() {
695 return doTranspile({
696 options: content,
697 moduleFormat: "steal",
698 resultModuleFormat: "cjs",
699 sourceFileName: "steal",
700 expectedFileName: "steal_cjs_cont_sm"
701 });
702 });
703
704 it("amd - cjs works", function() {
705 return doTranspile({
706 options: content,
707 moduleFormat: "amd",
708 resultModuleFormat: "cjs",
709 sourceFileName: "amd",
710 expectedFileName: "amd_cjs_cont_sm"
711 });
712 });
713
714 it("es6(traceur) - amd works", function() {
715 return doTranspile({
716 options: content,
717 moduleFormat: "es6",
718 resultModuleFormat: "amd",
719 sourceFileName: "es6",
720 expectedFileName: "es6_amd_cont_sm"
721 });
722 });
723
724 it("es6(babel) - amd works", function() {
725 var opts = assign({ transpiler: "babel" }, content);
726
727 return doTranspile({
728 options: opts,
729 moduleFormat: "es6",
730 resultModuleFormat: "amd",
731 sourceFileName: "es6",
732 expectedFileName: "es62_amd_cont_sm"
733 });
734 });
735 });
736});