|
| 1 | +import { LiteralSource } from '@app-config/core'; |
| 2 | +import { FileSource } from '@app-config/node'; |
| 3 | +import { withTempFiles } from '@app-config/test-utils'; |
| 4 | +import jsModuleDirective from './index'; |
| 5 | + |
| 6 | +describe('$jsModule directive', () => { |
| 7 | + it('loads function node export', () => |
| 8 | + withTempFiles( |
| 9 | + { |
| 10 | + 'foo.js': ` |
| 11 | + module.exports = () => 'bar'; |
| 12 | + `, |
| 13 | + }, |
| 14 | + async (inDir) => { |
| 15 | + const source = new LiteralSource({ |
| 16 | + $jsModule: inDir('foo.js'), |
| 17 | + }); |
| 18 | + |
| 19 | + expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar'); |
| 20 | + }, |
| 21 | + )); |
| 22 | + |
| 23 | + it('loads default function node export', () => |
| 24 | + withTempFiles( |
| 25 | + { |
| 26 | + 'foo.js': ` |
| 27 | + module.exports.__esModule = true; |
| 28 | + module.exports.default = () => 'bar'; |
| 29 | + `, |
| 30 | + }, |
| 31 | + async (inDir) => { |
| 32 | + const source = new LiteralSource({ |
| 33 | + $jsModule: inDir('foo.js'), |
| 34 | + }); |
| 35 | + |
| 36 | + expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar'); |
| 37 | + }, |
| 38 | + )); |
| 39 | + |
| 40 | + it('loads object node export', () => |
| 41 | + withTempFiles( |
| 42 | + { |
| 43 | + 'foo.js': ` |
| 44 | + module.exports = 'bar'; |
| 45 | + `, |
| 46 | + }, |
| 47 | + async (inDir) => { |
| 48 | + const source = new LiteralSource({ |
| 49 | + $jsModule: inDir('foo.js'), |
| 50 | + }); |
| 51 | + |
| 52 | + expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar'); |
| 53 | + }, |
| 54 | + )); |
| 55 | + |
| 56 | + it('loads default object node export', () => |
| 57 | + withTempFiles( |
| 58 | + { |
| 59 | + 'foo.js': ` |
| 60 | + module.exports.__esModule = true; |
| 61 | + module.exports.default = 'bar'; |
| 62 | + `, |
| 63 | + }, |
| 64 | + async (inDir) => { |
| 65 | + const source = new LiteralSource({ |
| 66 | + $jsModule: inDir('foo.js'), |
| 67 | + }); |
| 68 | + |
| 69 | + expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar'); |
| 70 | + }, |
| 71 | + )); |
| 72 | + |
| 73 | + it('loads file relative to app-config', () => |
| 74 | + withTempFiles( |
| 75 | + { |
| 76 | + 'config.yml': ` |
| 77 | + $jsModule: ./foo.js |
| 78 | + `, |
| 79 | + 'foo.js': ` |
| 80 | + module.exports.__esModule = true; |
| 81 | + module.exports.default = 'bar'; |
| 82 | + `, |
| 83 | + }, |
| 84 | + async (inDir) => { |
| 85 | + const source = new FileSource(inDir('config.yml')); |
| 86 | + |
| 87 | + expect(await source.readToJSON([jsModuleDirective()])).toEqual('bar'); |
| 88 | + }, |
| 89 | + )); |
| 90 | +}); |
0 commit comments