|
1 | 1 | const path = require('path');
|
| 2 | +const url = require('url'); |
2 | 3 | const Loader = require('../lib/loader');
|
3 | 4 |
|
4 | 5 | describe('loader', function() {
|
@@ -55,6 +56,21 @@ describe('loader', function() {
|
55 | 56 | expect(importShim).toHaveBeenCalledWith('some-module');
|
56 | 57 | });
|
57 | 58 |
|
| 59 | + it('imports namespaced modules', async function() { |
| 60 | + const payload = {default: {}}; |
| 61 | + const requireShim = jasmine.createSpy('requireShim'); |
| 62 | + const importShim = jasmine.createSpy('importShim') |
| 63 | + .and.returnValue(Promise.resolve(payload)); |
| 64 | + const loader = new Loader({requireShim, importShim}); |
| 65 | + loader.alwaysImport = true; |
| 66 | + |
| 67 | + const result = await loader.load('@namespace/some-module'); |
| 68 | + |
| 69 | + expect(result).toBe(payload.default); |
| 70 | + expect(requireShim).not.toHaveBeenCalled(); |
| 71 | + expect(importShim).toHaveBeenCalledWith('@namespace/some-module'); |
| 72 | + }); |
| 73 | + |
58 | 74 | it('uses require to load JSON files', async function() {
|
59 | 75 | const requireShim = jasmine.createSpy('requireShim')
|
60 | 76 | .and.returnValue(Promise.resolve());
|
@@ -101,6 +117,19 @@ describe('loader', function() {
|
101 | 117 | expect(importShim).not.toHaveBeenCalled();
|
102 | 118 | });
|
103 | 119 |
|
| 120 | + it('loads namespaced commonjs module', async function () { |
| 121 | + const requireShim = jasmine.createSpy('requireShim') |
| 122 | + .and.returnValue(Promise.resolve()); |
| 123 | + const importShim = jasmine.createSpy('importShim'); |
| 124 | + const loader = new Loader({requireShim, importShim}); |
| 125 | + loader.alwaysImport = false; |
| 126 | + |
| 127 | + await expectAsync(loader.load('@namespace/some-module')).toBeResolved(); |
| 128 | + |
| 129 | + expect(requireShim).toHaveBeenCalledWith('@namespace/some-module'); |
| 130 | + expect(importShim).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
104 | 133 | it('propagates the error when import fails', async function () {
|
105 | 134 | const underlyingError = new Error('nope');
|
106 | 135 | const requireShim = jasmine.createSpy('requireShim')
|
@@ -135,18 +164,18 @@ function esModuleSharedExamples(extension, alwaysImport) {
|
135 | 164 |
|
136 | 165 | expect(requireShim).not.toHaveBeenCalled();
|
137 | 166 | expect(resolvePath).toHaveBeenCalledWith(requestedPath);
|
138 |
| - expect(importShim).toHaveBeenCalledWith('file:///the/path/to/the/module'); |
| 167 | + expect(importShim).toHaveBeenCalledWith(url.pathToFileURL('/the/path/to/the/module').toString()); |
139 | 168 | await expectAsync(loaderPromise).toBePending();
|
140 | 169 |
|
141 | 170 | resolve({});
|
142 | 171 |
|
143 | 172 | await expectAsync(loaderPromise).toBeResolved();
|
144 | 173 | }
|
145 |
| - |
| 174 | + |
146 | 175 | it('loads the file as an es module', async function () {
|
147 | 176 | await testBasicEsModuleLoading(path.sep);
|
148 | 177 | });
|
149 |
| - |
| 178 | + |
150 | 179 | it('supports /-separated paths', async function() {
|
151 | 180 | await testBasicEsModuleLoading('/');
|
152 | 181 | });
|
|
0 commit comments