-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
75 lines (57 loc) · 1.59 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
const path = require('path');
const test = require('ava');
const System = require('fake-module-system');
const install = require('.');
test('module is compiled with source returned from precompiler', t => {
const system = new System({
'/foo.js': 'normal foo'
});
install(filename => {
t.is(filename, '/foo.js');
return 'precompiled foo';
}, '.js', system.extensions);
const module = system.load('/foo.js');
t.is(module.code, 'precompiled foo');
t.is(module.file, '/foo.js');
});
test('passes through to underlying extension precompiler returns null', t => {
const system = new System({
'/foo.js': 'normal foo'
});
install(filename => {
t.is(filename, '/foo.js');
return null;
}, '.js', system.extensions);
const module = system.load('/foo.js');
t.is(module.code, 'normal foo');
t.is(module.file, '/foo.js');
});
test('allows extensions beyond `.js`', t => {
const system = new System({
'/foo.coffee': 'coffee foo'
});
install(filename => {
t.is(filename, '/foo.coffee');
return 'precompiled coffee';
}, '.coffee', system.extensions);
const module = system.load('/foo.coffee');
t.is(module.code, 'precompiled coffee');
t.is(module.file, '/foo.coffee');
});
test('test actual require', t => {
const fixtureFile = path.join(__dirname, 'fixture.js');
install(filename => {
if (filename === fixtureFile) {
return 'module.exports = "foobar"';
}
return null;
});
t.is(require('./fixture'), 'foobar');
});
test('test require extension without previous handler extension', t => {
install(
() => null,
'.cjs'
);
t.is(require('./fixture.cjs'), 'foobar-cjs');
});