|
1 |
| -const {System, applyImportMap} = require('systemjs'); |
2 |
| -require('systemjs/dist/extras/amd'); // eslint-disable-line import/no-unassigned-import |
3 |
| -require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
4 |
| - |
5 |
| -const nonExistentPkg = 'pkg-not-on-npm-' + Math.random(); |
6 |
| -applyImportMap(System, { |
7 |
| - imports: { |
8 |
| - [nonExistentPkg]: 'https://unpkg.com/lodash', |
9 |
| - underscore: 'https://unpkg.com/lodash', |
10 |
| - }, |
| 1 | +beforeEach(() => { |
| 2 | + jest.resetModules(); |
11 | 3 | });
|
12 | 4 |
|
13 | 5 | test('should resolve module on UNPKG', async () => {
|
14 |
| - const _ = await System.import('lodash'); |
15 |
| - expect(_).toBeTruthy(); |
16 |
| -}); |
| 6 | + const {System} = require('systemjs'); |
| 7 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 8 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 9 | + |
| 10 | + await System.import('lodash'); |
| 11 | + expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/lodash', undefined); |
| 12 | + |
| 13 | + spy.mockRestore(); |
| 14 | +}, 10000); |
| 15 | + |
| 16 | +test('should resolve versioned module on UNPKG', async () => { |
| 17 | + const {System} = require('systemjs'); |
| 18 | + require('systemjs/dist/extras/amd'); // eslint-disable-line import/no-unassigned-import |
| 19 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 20 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 21 | + |
| 22 | + const _ = await System.import('lodash@4.17.0'); |
| 23 | + expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/lodash@4.17.0', undefined); |
| 24 | + expect(_.VERSION).toBe('4.17.0'); |
| 25 | + spy.mockRestore(); |
| 26 | +}, 10000); |
17 | 27 |
|
18 | 28 | test('should fail when resolving non-existing module', async () => {
|
| 29 | + const {System} = require('systemjs'); |
| 30 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 31 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 32 | + |
| 33 | + const nonExistentPkg = 'non-existent-package-for-testing-purposes-' + Math.random(); |
19 | 34 | await expect(() =>
|
20 |
| - System.import('non-existent-package-for-testing-purposes-' + Math.random()), |
| 35 | + System.import(nonExistentPkg), |
21 | 36 | ).rejects.toThrow('404 Not Found');
|
22 |
| -}); |
| 37 | + |
| 38 | + expect(spy).toHaveBeenNthCalledWith(2, 'https://unpkg.com/' + nonExistentPkg, undefined); |
| 39 | + |
| 40 | + spy.mockRestore(); |
| 41 | +}, 10000); |
| 42 | + |
| 43 | +test('should not try to resolve relative paths', async () => { |
| 44 | + const {System} = require('systemjs'); |
| 45 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 46 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 47 | + |
| 48 | + await expect(() => |
| 49 | + System.import('./index.js'), |
| 50 | + ).rejects.toThrow('ECONNREFUSED'); |
| 51 | + |
| 52 | + expect(spy).toHaveBeenCalledTimes(1); |
| 53 | + |
| 54 | + spy.mockRestore(); |
| 55 | +}, 10000); |
| 56 | + |
| 57 | +test('should not try to resolve absolute paths', async () => { |
| 58 | + const {System} = require('systemjs'); |
| 59 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 60 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 61 | + |
| 62 | + await expect(() => |
| 63 | + System.import('/index.js'), |
| 64 | + ).rejects.toThrow('ECONNREFUSED'); |
| 65 | + |
| 66 | + expect(spy).toHaveBeenCalledTimes(1); |
| 67 | + |
| 68 | + spy.mockRestore(); |
| 69 | +}, 10000); |
23 | 70 |
|
24 | 71 | test('should use importmap to resolve non-existing package', async () => {
|
25 |
| - const _ = await System.import(nonExistentPkg); |
26 |
| - expect(_).toBeTruthy(); |
27 |
| -}); |
| 72 | + const {System, applyImportMap} = require('systemjs'); |
| 73 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 74 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 75 | + |
| 76 | + const nonExistentPkg = 'pkg-not-on-npm-' + Math.random(); |
| 77 | + |
| 78 | + applyImportMap(System, { |
| 79 | + imports: { |
| 80 | + [nonExistentPkg]: 'https://unpkg.com/lodash', |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + await System.import(nonExistentPkg); |
| 85 | + expect(spy).toHaveBeenCalledTimes(1); |
| 86 | + |
| 87 | + spy.mockRestore(); |
| 88 | +}, 10000); |
28 | 89 |
|
29 | 90 | test('should use importmap to resolve underscore to lodash', async () => {
|
30 |
| - const _ = await System.import('underscore'); |
31 |
| - expect(_.default.name).toBe('lodash'); |
32 |
| -}); |
| 91 | + const {System, applyImportMap} = require('systemjs'); |
| 92 | + const spy = jest.spyOn(System.constructor.prototype, 'resolve'); |
| 93 | + require('../src/systemjs-unpkg'); // eslint-disable-line import/no-unassigned-import |
| 94 | + |
| 95 | + applyImportMap(System, { |
| 96 | + imports: { |
| 97 | + underscore: 'https://unpkg.com/lodash', |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + await System.import('underscore'); |
| 102 | + expect(spy).toHaveBeenCalledTimes(1); |
| 103 | + |
| 104 | + spy.mockRestore(); |
| 105 | +}, 10000); |
0 commit comments