-
Notifications
You must be signed in to change notification settings - Fork 580
/
Copy pathnode-api-test.js
222 lines (182 loc) · 6.54 KB
/
node-api-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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var fs = require('fs');
var path = require('path');
var assert = global.assert = require('chai').assert;
suite('node public api', function() {
var traceurAPI = require('../src/node/api.js');
var sourceMapUtil = require('source-map/lib/source-map/util.js');
var relativeFilename = '/commonjs/BasicImport.js';
var nativeFilename = path.join(__dirname + relativeFilename);
var nativeDirname = __dirname;
assert(nativeDirname[nativeDirname.length - 1] !== path.sep,
'expect no trailing slash');
// Non-native path separators
function swapSlash(s) {
return s.split(path.sep).join(path.sep === '/' ? '\\' : '/');
}
var swapSlashFilename = swapSlash(nativeFilename);
var swapSlashDirname = swapSlash(nativeDirname);
// Traceur uses forward slashes since these work cross platform in APIs.
function forwardSlash(s) {
return s.replace(/\\/g, '/');
}
var traceurFilename = forwardSlash(nativeFilename);
var traceurDirname = forwardSlash(nativeDirname);
var contents = fs.readFileSync(nativeFilename, 'utf8');
test('moduleName from filename with backslashes', function() {
var compiler = new traceurAPI.NodeCompiler({
// build ES6 style modules rather then cjs
modules: 'bootstrap',
// single file compile defaults to moduleName false
moduleName: true,
// ensure the source map works
sourceMaps: 'file'
});
// windows-simulation, with .js
var compiled = compiler.compile(contents, swapSlashFilename);
assert.ok(compiled, 'can compile');
assert.include(
compiled,
// the module path is relative to the cwd setting when we compile it.
'commonjs/BasicImport',
'module name without backslashes'
);
assert.ok(compiler.getSourceMap(), 'has sourceMap');
});
test('sourceRoot with backslashes', function() {
var compiler = new traceurAPI.NodeCompiler({
// build ES6 style modules rather then cjs
modules: 'bootstrap',
// ensure the source map works
sourceMaps: true
});
// windows-simulation, with .js
var compiled = compiler.compile(contents, swapSlashFilename,
swapSlashFilename, swapSlashDirname);
assert.ok(compiled, 'can compile');
assert.ok(compiler.getSourceMap(), 'has sourceMap');
var sourceMap = JSON.parse(compiler.getSourceMap());
assert.equal(traceurDirname, sourceMap.sourceRoot,
'has correct sourceRoot');
// The sourceRoot given here, when combined with the 'sources'
// entry is not a filename.
var notAFileName = traceurDirname + '/BasicImport.js';
assert(sourceMap.sources.some(function(name) {
var fromSourceRoot = sourceMapUtil.join(sourceMap.sourceRoot, name);
return fromSourceRoot === notAFileName;
}), 'One of the sources is the source');
});
test('sourceRoot with full windows path and backslashes', function() {
var compiler = new traceurAPI.NodeCompiler({
// build ES6 style modules rather then cjs
modules: 'bootstrap',
// ensure the source map works
sourceMaps: true
});
// windows-simulation, with .js
var compiled = compiler.compile(contents, swapSlashFilename,
swapSlashFilename, swapSlashDirname);
assert.ok(compiled, 'can compile');
assert.ok(compiler.getSourceMap(), 'has sourceMap');
var sourceMap = JSON.parse(compiler.getSourceMap());
assert.equal(traceurDirname, sourceMap.sourceRoot,
'has correct sourceRoot');
// The sourceRoot given here, when combined with the 'sources'
// entry is not a filename.
var notAFileName = traceurDirname + '/BasicImport.js';
assert(sourceMap.sources.some(function(name) {
return (sourceMap.sourceRoot + '/' + name) === notAFileName;
}), 'One of the sources is the source');
});
test('modules: true', function() {
var compiler = new traceurAPI.NodeCompiler({
// build ES6 style modules rather then cjs
modules: 'bootstrap',
// single file compile defaults to moduleName false
moduleName: true,
// ensure the source map works
sourceMaps: true
});
var compiled = compiler.compile(contents, nativeFilename);
assert.ok(compiled, 'can compile');
assert.include(
compiled,
// the module path is relative to the cwd setting when we compile it.
'commonjs/BasicImport',
'module defines its path'
);
assert.ok(compiler.getSourceMap(), 'has sourceMap');
});
test('named amd', function() {
var compiled = traceurAPI.compile(contents, {
// build requirejs style modules rather then cjs
modules: 'amd',
// enforce a module name in the AMD define
moduleName: true
}, 'test-module');
assert.ok(compiled, 'can compile');
var gotName;
function define(name) {
gotName = name;
}
// eval locally to capture the define() mock
eval(compiled);
assert.equal(gotName, 'test-module');
});
test('default amd', function() {
var compiled = traceurAPI.compile(contents, {
// build requirejs style modules rather then cjs
modules: 'amd',
moduleName: true
}, 'test-module');
assert.ok(compiled, 'can compile');
var gotName;
function define(name) {
gotName = name;
}
// eval locally to capture the define() mock
eval(compiled);
assert.equal(gotName, 'test-module');
});
test('instantiate anonymous', function() {
var contents = "export var p = 5;";
var compiled = traceurAPI.compile(contents, {
module: 'instantiate',
moduleName: false
}, 'test-module');
assert.ok(compiled, 'can compile');
var gotName;
var System = {
register: function(name) {
gotName = typeof name === 'string' && name;
}
};
// eval locally to capture the define() mock
eval(compiled);
assert.equal(gotName, undefined);
});
test('instantiate named', function() {
var contents = "export var p = 5;";
var compiled = traceurAPI.compile(contents, {
modules: 'instantiate',
moduleName: true
}, 'test-module');
assert.ok(compiled, 'can compile');
var gotName;
var System = {
register: function(name) {
gotName = typeof name === 'string' && name;
}
};
// eval locally to capture the define() mock
eval(compiled);
assert.equal(gotName, 'test-module');
});
test('modules parse option', function() {
var contents = "export var p = 5;";
var compiled = traceurAPI.compile(contents, {
modules: 'parse'
}, 'test-module');
assert.ok(compiled, 'can compile');
assert(compiled.match(/^export var p = 5;/));
});
});