|
| 1 | +import test from 'ava'; |
| 2 | +import install from './'; |
| 3 | +import System from 'fake-module-system'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +test('module is compiled with source returned from precompiler', t => { |
| 7 | + const system = new System({ |
| 8 | + '/foo.js': 'normal foo' |
| 9 | + }); |
| 10 | + |
| 11 | + install(filename => { |
| 12 | + t.is(filename, '/foo.js'); |
| 13 | + return 'precompiled foo'; |
| 14 | + }, '.js', system.extensions); |
| 15 | + |
| 16 | + const module = system.load('/foo.js'); |
| 17 | + |
| 18 | + t.is(module.code, 'precompiled foo'); |
| 19 | + t.is(module.file, '/foo.js'); |
| 20 | +}); |
| 21 | + |
| 22 | +test('passes through to underlying extension precompiler returns undefined', t => { |
| 23 | + const system = new System({ |
| 24 | + '/foo.js': 'normal foo' |
| 25 | + }); |
| 26 | + |
| 27 | + install(filename => { |
| 28 | + t.is(filename, '/foo.js'); |
| 29 | + return; |
| 30 | + }, '.js', system.extensions); |
| 31 | + |
| 32 | + const module = system.load('/foo.js'); |
| 33 | + |
| 34 | + t.is(module.code, 'normal foo'); |
| 35 | + t.is(module.file, '/foo.js'); |
| 36 | +}); |
| 37 | + |
| 38 | +test('allows extensions beyond ".js"', t => { |
| 39 | + const system = new System({ |
| 40 | + '/foo.coffee': 'coffee foo' |
| 41 | + }); |
| 42 | + |
| 43 | + install(filename => { |
| 44 | + t.is(filename, '/foo.coffee'); |
| 45 | + return 'precompiled coffee'; |
| 46 | + }, '.coffee', system.extensions); |
| 47 | + |
| 48 | + const module = system.load('/foo.coffee'); |
| 49 | + |
| 50 | + t.is(module.code, 'precompiled coffee'); |
| 51 | + t.is(module.file, '/foo.coffee'); |
| 52 | +}); |
| 53 | + |
| 54 | +test('test actual require', t => { |
| 55 | + var fixtureFile = path.join(__dirname, 'fixture.js'); |
| 56 | + |
| 57 | + install(filename => { |
| 58 | + if (filename === fixtureFile) { |
| 59 | + return 'module.exports = "foobar"'; |
| 60 | + } |
| 61 | + return null; |
| 62 | + }); |
| 63 | + |
| 64 | + t.is(require('./fixture'), 'foobar'); |
| 65 | +}); |
0 commit comments