UNPKG

17.5 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 it("should not loop until the stack is out of space", function() {
311 return convert({
312 converter: amd2amd,
313 sourceFileName: "amd_umd_loop",
314 expectedFileName: "amd_umd_loop",
315 options: { namedDefines: true },
316 load: { name: "redefined" }
317 });
318 });
319});
320
321describe("metadata.format", function(){
322 it("should be detected from amd source", function() {
323 return doTranspile({
324 moduleFormat: undefined,
325 resultModuleFormat: "amd",
326 sourceFileName: "amd",
327 expectedFileName: "amd_amd",
328 options: { namedDefines: true }
329 });
330 });
331
332 it("should be detected from steal source", function() {
333 return doTranspile({
334 moduleFormat: undefined,
335 resultModuleFormat: "cjs",
336 sourceFileName: "steal",
337 expectedFileName: "steal_cjs"
338 });
339 });
340
341 it("should be detected from es6 source", function() {
342 return doTranspile({
343 moduleFormat: undefined,
344 resultModuleFormat: "cjs",
345 sourceFileName: "es6",
346 expectedFileName: "es6_cjs"
347 });
348 });
349});
350
351describe("es6 - amd", function() {
352 it("should work with bangs", function() {
353 return doTranspile({
354 moduleFormat: "es6",
355 resultModuleFormat: "amd",
356 sourceFileName: "es_with_bang",
357 expectedFileName: "es_with_bang_amd",
358 options: { namedDefines: true }
359 });
360 });
361
362 it("should work with babel", function() {
363 return doTranspile({
364 moduleFormat: "es6",
365 resultModuleFormat: "amd",
366 sourceFileName: "es6",
367 expectedFileName: "es6_amd_babel",
368 options: { transpiler: "babel" }
369 });
370 });
371
372 it("should work with babel-standalone included plugins", function() {
373 return doTranspile({
374 moduleFormat: "es6",
375 resultModuleFormat: "amd",
376 sourceFileName: "es6_and_decorators",
377 expectedFileName: "es6_decorators_amd_babel",
378 options: {
379 transpiler: "babel",
380 babelOptions: {
381 plugins: ["transform-decorators-legacy"]
382 }
383 }
384 });
385 });
386
387 it("stage0 is not a required preset", function() {
388 return doTranspile({
389 moduleFormat: "es6",
390 resultModuleFormat: "amd",
391 sourceFileName: "es6_and_async",
392 expectedFileName: "es6_and_async",
393 options: {
394 transpiler: "babel",
395 babelOptions: {
396 presets: ["react"]
397 }
398 }
399 });
400 });
401
402 it("should work with babel plugins NOT included in babel-standalone", function() {
403 return doTranspile({
404 moduleFormat: "es6",
405 resultModuleFormat: "amd",
406 sourceFileName: "es6",
407 expectedFileName: "es6_amd_babel_and_plugin",
408 options: {
409 transpiler: "babel",
410 babelOptions: {
411 plugins: [ require("./babel-plugin") ]
412 }
413 }
414 });
415 });
416
417 it("should work with babel presets NOT built in babel-standalone", function() {
418 return doTranspile({
419 moduleFormat: "es6",
420 resultModuleFormat: "amd",
421 sourceFileName: "es6",
422 expectedFileName: "es6_amd_babel_and_plugin",
423 options: {
424 transpiler: "babel",
425 babelOptions: {
426 presets: [ require("babel-preset-steal-test") ]
427 }
428 }
429 });
430 });
431
432 it("presets override the default ones", function() {
433 return doTranspile({
434 sourceFileName: "es6",
435 moduleFormat: "es6",
436 expectedFileName: "es6_amd_babel_loose",
437 resultModuleFormat: "amd",
438 options: {
439 transpiler: "babel",
440 babelOptions: {
441 presets: [
442 ["es2015", { loose: true }]
443 ]
444 }
445 }
446 });
447 });
448
449 it("should work with traceurOptions", function() {
450 return doTranspile({
451 sourceFileName: "es6",
452 moduleFormat: "es6",
453 expectedFileName: "es_with_traceur_options",
454 resultModuleFormat: "amd",
455 options: {
456 traceurOptions: {
457 properTailCalls: true
458 }
459 }
460 });
461 });
462
463 it("can skip es2015 transforms with options.forceES5", function() {
464 return doTranspile({
465 moduleFormat: "es6",
466 resultModuleFormat: "amd",
467 sourceFileName: "es6_and_async",
468 expectedFileName: "es6_and_async_not_es5",
469 options: {
470 transpiler: "babel",
471 forceES5: false
472 }
473 });
474 });
475
476 it("does not error with ES2015 features when not transforming to ES5", function() {
477 return doTranspile({
478 moduleFormat: "es6",
479 resultModuleFormat: "amd",
480 sourceFileName: "es6_string_literals",
481 expectedFileName: "es6_string_literals_not_es5",
482 options: {
483 transpiler: "babel",
484 forceES5: false
485 }
486 });
487 });
488
489 it("allows sourcemaps generation to be skipped", function() {
490 var es6ToAmd = require("../lib/es6_amd");
491
492 var load = {
493 source: "import foo from 'bar';",
494 name: "foo"
495 };
496
497 es6ToAmd(load, {
498 transpiler: "babel",
499 sourceMaps: false
500 });
501
502 assert.equal(load.map, null, "should not generate sourcemaps");
503 });
504});
505
506describe("normalize options", function() {
507 it("steal - amd + normalizeMap", function() {
508 var options = {
509 normalizeMap: {
510 "./baz": "baz"
511 },
512 namedDefines: true
513 };
514
515 return convert({
516 options: options,
517 converter: steal2amd,
518 sourceFileName: "steal_deps",
519 expectedFileName: "steal_amd_dep"
520 });
521 });
522
523 it("steal - amd + normalize", function() {
524 return convert({
525 converter: steal2amd,
526 sourceFileName: "steal_deps",
527 expectedFileName: "steal_amd_normalize",
528 options: {
529 normalizeMap: {
530 "./baz": "baz"
531 },
532 normalize: function(name){
533 var parts = name.split("/");
534 var len = parts.length;
535
536 if (parts[len-1] === parts[len-2]) {
537 parts.pop();
538 }
539 return parts.join("/");
540 },
541 namedDefines: true
542 }
543 });
544 });
545
546 it("es6 - cjs + normalize", function() {
547 return convert({
548 sourceFileName: "es_needing_normalize",
549 converter: es62cjs,
550 expectedFileName: "es_needing_normalize_cjs",
551 options: {
552 normalize: function(name) {
553 if(name.lastIndexOf("/") === name.length - 1) {
554 var parts = name.split("/");
555 parts[parts.length - 1] = parts[parts.length - 2];
556 return parts.join("/");
557 } else if( name.indexOf("!") >= 0 ) {
558 return name.substr(0, name.indexOf("!") );
559 }
560 return name;
561 }
562 }
563 });
564 });
565
566 it("amd - cjs + normalize", function() {
567 return convert({
568 sourceFileName: "amd_needing_normalize",
569 converter: amd2cjs,
570 expectedFileName: "amd_needing_normalize_cjs",
571 options: {
572 normalize: function(name){
573 if(name.lastIndexOf("/") === name.length - 1) {
574 var parts = name.split("/");
575 parts[parts.length - 1] = parts[parts.length - 2];
576 return parts.join("/");
577 } else if( name.indexOf("!") >= 0 ) {
578 return name.substr(name.indexOf("!")+1);
579 }
580 return name;
581 }
582 }
583 });
584 });
585
586 it("steal - cjs + normalize",function() {
587 return doTranspile({
588 moduleFormat: "steal",
589 resultModuleFormat: "cjs",
590 sourceFileName: "steal_needing_normalize",
591 expectedFileName: "steal_needing_normalize_cjs",
592 options: {
593 normalize: function(name){
594 return name+"-normalized";
595 }
596 }
597 });
598 });
599
600 it("cjs - cjs + normalize", function() {
601 return doTranspile({
602 moduleFormat: "cjs",
603 resultModuleFormat: "cjs",
604 sourceFileName: "cjs_needing_normalize",
605 expectedFileName: "cjs_needing_normalize_cjs",
606 options: {
607 normalize: function(name) {
608 return name + "-normalized";
609 }
610 }
611 });
612 });
613});
614
615describe("transpile options", function() {
616 it("es6 - cjs + normalize",function() {
617 var options = {
618 transpile: function() {
619 return {
620 code: 'require("foo")'
621 };
622 }
623 };
624
625 return convert({
626 options: options,
627 converter: es62cjs,
628 sourceFileName: "es_needing_normalize",
629 expectedFileName: "es_self_transpile"
630 });
631 });
632});
633
634describe("Source Maps", function() {
635 var normal = { sourceMaps: true };
636 var content = { sourceMaps: true, sourceMapsContent: true };
637
638 [normal, content].forEach(function(opts) {
639 opts.baseURL = __dirname + "/tests";
640 opts.sourceRoot = "../";
641 });
642
643 describe("External file", function() {
644 it("steal - amd works", function() {
645 return doTranspile({
646 options: normal,
647 moduleFormat: "steal",
648 resultModuleFormat: "amd",
649 sourceFileName: "steal",
650 expectedFileName: "steal_amd_sm"
651 });
652 });
653
654 it("steal - cjs works", function() {
655 return doTranspile({
656 options: normal,
657 moduleFormat: "steal",
658 resultModuleFormat: "cjs",
659 sourceFileName: "steal",
660 expectedFileName: "steal_cjs_sm"
661 });
662 });
663
664 it("cjs - amd works", function() {
665 return doTranspile({
666 options: normal,
667 moduleFormat: "cjs",
668 resultModuleFormat: "amd",
669 sourceFileName: "cjs",
670 expectedFileName: "cjs_amd_sm"
671 });
672 });
673
674 it("amd - cjs works", function() {
675 return doTranspile({
676 options: normal,
677 moduleFormat: "amd",
678 resultModuleFormat: "cjs",
679 sourceFileName: "amd",
680 expectedFileName: "amd_cjs_sm"
681 });
682 });
683
684 it("amd - amd works", function() {
685 return doTranspile({
686 options: normal,
687 moduleFormat: "amd",
688 resultModuleFormat: "amd",
689 sourceFileName: "amd",
690 expectedFileName: "amd_amd_sm"
691 });
692 });
693
694 it("es6(traceur) - amd works", function() {
695 return doTranspile({
696 options: normal,
697 moduleFormat: "es6",
698 resultModuleFormat: "amd",
699 sourceFileName: "es6",
700 expectedFileName: "es6_amd_sm"
701 });
702 });
703
704 it("es6(babel) - amd works", function() {
705 var opts = assign({ transpiler: "babel" }, normal);
706
707 return doTranspile({
708 options: opts,
709 moduleFormat: "es6",
710 resultModuleFormat: "amd",
711 sourceFileName: "es6",
712 expectedFileName: "es62_amd_sm"
713 });
714 });
715
716 it("es6(traceur) - cjs works", function() {
717 return doTranspile({
718 options: normal,
719 moduleFormat: "es6",
720 resultModuleFormat: "cjs",
721 sourceFileName: "es6",
722 expectedFileName: "es6_cjs_sm"
723 });
724 });
725
726 it("es6(babel) - cjs works", function() {
727 var opts = assign({ transpiler: "babel" }, normal);
728
729 return doTranspile({
730 options: opts,
731 moduleFormat: "es6",
732 resultModuleFormat: "cjs",
733 sourceFileName: "es6",
734 expectedFileName: "es62_cjs_sm"
735 });
736 });
737 });
738
739 describe("Content included", function() {
740 it("steal - amd works", function() {
741 doTranspile({
742 options: content,
743 moduleFormat: "steal",
744 resultModuleFormat: "amd",
745 sourceFileName: "steal",
746 expectedFileName: "steal_amd_cont_sm"
747 });
748 });
749
750 it("steal - cjs works", function() {
751 return doTranspile({
752 options: content,
753 moduleFormat: "steal",
754 resultModuleFormat: "cjs",
755 sourceFileName: "steal",
756 expectedFileName: "steal_cjs_cont_sm"
757 });
758 });
759
760 it("amd - cjs works", function() {
761 return doTranspile({
762 options: content,
763 moduleFormat: "amd",
764 resultModuleFormat: "cjs",
765 sourceFileName: "amd",
766 expectedFileName: "amd_cjs_cont_sm"
767 });
768 });
769
770 it("es6(traceur) - amd works", function() {
771 return doTranspile({
772 options: content,
773 moduleFormat: "es6",
774 resultModuleFormat: "amd",
775 sourceFileName: "es6",
776 expectedFileName: "es6_amd_cont_sm"
777 });
778 });
779
780 it("es6(babel) - amd works", function() {
781 var opts = assign({ transpiler: "babel" }, content);
782
783 return doTranspile({
784 options: opts,
785 moduleFormat: "es6",
786 resultModuleFormat: "amd",
787 sourceFileName: "es6",
788 expectedFileName: "es62_amd_cont_sm"
789 });
790 });
791 });
792});