This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest.js
140 lines (117 loc) · 4.11 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* eslint-env mocha */
'use strict';
const assert = require('assert');
const path = require('path');
const sourceMaps = require('gulp-sourcemaps');
const Vinyl = require('vinyl');
const traceur = require('.');
const fixtures = {
'fixture.js': 'import {Foo} from \'./foo\';',
'errored.js': 'cons x = 1;',
'calc.js': 'import {a, b} from "util/constants";\nimport {add} from "calc/add";\nconsole.log(add(a, b));',
'util/constants.js': 'export var a = 5;\nexport var b = 3;',
'calc/add.js': 'export function add (...args) {\n\treturn args.reduce((sum, val) => sum + val, 0);\n};'
};
function getFixtureFile(name) {
return new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture' + name.substring(0, name.lastIndexOf('/'))),
path: path.join(__dirname, 'fixture', name),
contents: Buffer.from(fixtures[name])
});
}
it('should transpile with Traceur', cb => {
const stream = traceur({blockBinding: true});
stream.on('data', file => {
assert(/Foo/.test(file.contents.toString()));
cb();
});
stream.write(getFixtureFile('fixture.js'));
});
it('should generate source maps', cb => {
const init = sourceMaps.init();
const write = sourceMaps.write();
init
.pipe(traceur())
.pipe(write);
write.on('data', file => {
assert.equal(file.sourceMap.sources[0], 'fixture.js');
const contents = file.contents.toString();
assert(/function/.test(contents));
assert(/sourceMappingURL=data:application\/json;charset=utf8;base64/.test(contents));
assert.strictEqual((contents.match(/sourceMappingURL/g) || []).length, 1, 'should be one sourceMappingURL in the content');
cb();
});
init.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture.js'),
contents: Buffer.from('[].map(v => v + 1)'),
sourceMap: ''
}));
init.end();
});
it('should pass syntax errors', cb => {
const stream = traceur();
stream.on('error', err => {
assert(/Semi-colon expected/.test(err.message));
cb();
});
stream.write(getFixtureFile('errored.js'));
});
it('should expose the Traceur runtime path', () => {
assert(typeof traceur.RUNTIME_PATH === 'string');
assert(traceur.RUNTIME_PATH.length > 0);
});
it('should keep folder in module names with cjs modules', cb => {
// Cjs is default module implementation
const stream = traceur();
stream.on('data', file => {
const content = file.contents.toString();
const name = path.relative(path.join(__dirname, 'fixture'), file.path);
switch (name) {
case 'calc.js':
assert(/require\("util\/constants"\)/.test(content), 'calc.js does not require constants');
assert(/require\("calc\/add"\)/.test(content), 'calc.js does not require add');
break;
case path.join('util', 'constants.js'):
case path.join('calc', 'add.js'):
// Just check that files have right names
break;
default:
cb(`unexpected compiled file: ${name}`);
}
});
stream.on('end', cb);
for (const name of ['calc.js', 'util/constants.js', 'calc/add.js']) {
stream.write(getFixtureFile(name));
}
stream.end();
});
// TODO: update this test for latest Traceur
it.skip('should keep folder in module names with register modules', cb => {
const stream = traceur({modules: 'register', moduleName: true});
stream.on('data', file => {
const content = file.contents.toString();
const name = path.relative(path.join(__dirname, 'fixture'), file.path);
let fileName;
switch (name) {
case 'calc.js':
assert(/System\.get\("util\/constants"\)/.test(content), 'calc.js does not require constants');
assert(/System\.get\("calc\/add"\)/.test(content), 'calc.js does not require add');
break;
case path.join('util', 'constants.js'):
case path.join('calc', 'add.js'):
fileName = file.relative.replace(new RegExp(`\\${path.sep}`, 'g'), '/').replace('.js', '');
assert(new RegExp(`System\\.registerModule\\("${fileName}.js"`).test(content), `${fileName} does not contains its filename`);
break;
default:
cb(`unexpected compiled file: ${file.relative}`);
}
});
stream.on('end', cb);
for (const name of ['calc.js', 'util/constants.js', 'calc/add.js']) {
stream.write(getFixtureFile(name));
}
stream.end();
});