|
| 1 | +import { createGetModuleFromFilename } from '../../src'; |
| 2 | + |
| 3 | +describe('createGetModuleFromFilename', () => { |
| 4 | + it.each([ |
| 5 | + ['/path/to/file.js', 'file'], |
| 6 | + ['/path/to/file.mjs', 'file'], |
| 7 | + ['/path/to/file.cjs', 'file'], |
| 8 | + ['file.js', 'file'], |
| 9 | + ])('returns the module name from a filename %s', (filename, expected) => { |
| 10 | + const getModule = createGetModuleFromFilename(); |
| 11 | + expect(getModule(filename)).toBe(expected); |
| 12 | + }); |
| 13 | + |
| 14 | + it('applies the given base path', () => { |
| 15 | + const getModule = createGetModuleFromFilename('/path/to/base'); |
| 16 | + expect(getModule('/path/to/base/file.js')).toBe('file'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('decodes URI-encoded file names', () => { |
| 20 | + const getModule = createGetModuleFromFilename(); |
| 21 | + expect(getModule('/path%20with%space/file%20with%20spaces(1).js')).toBe('file with spaces(1)'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('returns undefined if no filename is provided', () => { |
| 25 | + const getModule = createGetModuleFromFilename(); |
| 26 | + expect(getModule(undefined)).toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it.each([ |
| 30 | + ['/path/to/base/node_modules/@sentry/test/file.js', '@sentry.test:file'], |
| 31 | + ['/path/to/base/node_modules/somePkg/file.js', 'somePkg:file'], |
| 32 | + ])('handles node_modules file paths %s', (filename, expected) => { |
| 33 | + const getModule = createGetModuleFromFilename(); |
| 34 | + expect(getModule(filename)).toBe(expected); |
| 35 | + }); |
| 36 | + |
| 37 | + it('handles windows paths with passed basePath and node_modules', () => { |
| 38 | + const getModule = createGetModuleFromFilename('C:\\path\\to\\base', true); |
| 39 | + expect(getModule('C:\\path\\to\\base\\node_modules\\somePkg\\file.js')).toBe('somePkg:file'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('handles windows paths with default basePath', () => { |
| 43 | + const getModule = createGetModuleFromFilename(undefined, true); |
| 44 | + expect(getModule('C:\\path\\to\\base\\somePkg\\file.js')).toBe('file'); |
| 45 | + }); |
| 46 | +}); |
0 commit comments