|
| 1 | +import { |
| 2 | + _interopDefault, |
| 3 | + _interopNamespace, |
| 4 | + _interopNamespaceDefaultOnly, |
| 5 | + _interopRequireDefault, |
| 6 | + _interopRequireWildcard, |
| 7 | +} from '../../src/buildPolyfills'; |
| 8 | +import { RequireResult } from '../../src/buildPolyfills/types'; |
| 9 | +import { |
| 10 | + _interopDefault as _interopDefaultOrig, |
| 11 | + _interopNamespace as _interopNamespaceOrig, |
| 12 | + _interopNamespaceDefaultOnly as _interopNamespaceDefaultOnlyOrig, |
| 13 | + _interopRequireDefault as _interopRequireDefaultOrig, |
| 14 | + _interopRequireWildcard as _interopRequireWildcardOrig, |
| 15 | +} from './originals'; |
| 16 | + |
| 17 | +// This file tests five different functions against a range of test cases. Though the inputs are the same for each |
| 18 | +// function's test cases, the expected output differs. The testcases for each function are therefore built from separate |
| 19 | +// collections of expected inputs and expected outputs. Further, for readability purposes, the tests labels have also |
| 20 | +// been split into their own object. It's also worth noting that in real life, there are some test-case/function |
| 21 | +// pairings which would never happen, but by testing all combinations, we're guaranteed to have tested the ones which |
| 22 | +// show up in the wild. |
| 23 | + |
| 24 | +const dogStr = 'dogs are great!'; |
| 25 | +const dogFunc = () => dogStr; |
| 26 | +const dogAdjectives = { maisey: 'silly', charlie: 'goofy' }; |
| 27 | + |
| 28 | +const withESModuleFlag = { __esModule: true, ...dogAdjectives }; |
| 29 | +const withESModuleFlagAndDefault = { __esModule: true, default: dogFunc, ...dogAdjectives }; |
| 30 | +const namedExports = { ...dogAdjectives }; |
| 31 | +const withNonEnumerableProp = { ...dogAdjectives }; |
| 32 | +// Properties added using `Object.defineProperty` are non-enumerable by default |
| 33 | +Object.defineProperty(withNonEnumerableProp, 'hiddenProp', { value: 'shhhhhhhh' }); |
| 34 | +const withDefaultExport = { default: dogFunc, ...dogAdjectives }; |
| 35 | +const withOnlyDefaultExport = { default: dogFunc }; |
| 36 | +const exportsEquals = dogFunc as RequireResult; |
| 37 | +const exportsEqualsWithDefault = dogFunc as RequireResult; |
| 38 | +exportsEqualsWithDefault.default = exportsEqualsWithDefault; |
| 39 | + |
| 40 | +const mockRequireResults: Record<string, RequireResult> = { |
| 41 | + withESModuleFlag, |
| 42 | + withESModuleFlagAndDefault, |
| 43 | + namedExports, |
| 44 | + withNonEnumerableProp, |
| 45 | + withDefaultExport, |
| 46 | + withOnlyDefaultExport, |
| 47 | + exportsEquals: exportsEquals, |
| 48 | + exportsEqualsWithDefault: exportsEqualsWithDefault as unknown as RequireResult, |
| 49 | +}; |
| 50 | + |
| 51 | +const testLabels: Record<string, string> = { |
| 52 | + withESModuleFlag: 'module with `__esModule` flag', |
| 53 | + withESModuleFlagAndDefault: 'module with `__esModule` flag and default export', |
| 54 | + namedExports: 'module with named exports', |
| 55 | + withNonEnumerableProp: 'module with named exports and non-enumerable prop', |
| 56 | + withDefaultExport: 'module with default export', |
| 57 | + withOnlyDefaultExport: 'module with only default export', |
| 58 | + exportsEquals: 'module using `exports =`', |
| 59 | + exportsEqualsWithDefault: 'module using `exports =` with default export', |
| 60 | +}; |
| 61 | + |
| 62 | +function makeTestCases(expectedOutputs: Record<string, RequireResult>): Array<[string, RequireResult, RequireResult]> { |
| 63 | + return Object.keys(mockRequireResults).map(key => [testLabels[key], mockRequireResults[key], expectedOutputs[key]]); |
| 64 | +} |
| 65 | + |
| 66 | +describe('_interopNamespace', () => { |
| 67 | + describe('returns the same result as the original', () => { |
| 68 | + const expectedOutputs: Record<string, RequireResult> = { |
| 69 | + withESModuleFlag: withESModuleFlag, |
| 70 | + withESModuleFlagAndDefault: withESModuleFlagAndDefault, |
| 71 | + namedExports: { ...namedExports, default: namedExports }, |
| 72 | + withNonEnumerableProp: { |
| 73 | + ...withNonEnumerableProp, |
| 74 | + default: withNonEnumerableProp, |
| 75 | + }, |
| 76 | + withDefaultExport: { ...withDefaultExport, default: withDefaultExport }, |
| 77 | + withOnlyDefaultExport: { default: withOnlyDefaultExport }, |
| 78 | + exportsEquals: { default: exportsEquals }, |
| 79 | + exportsEqualsWithDefault: { default: exportsEqualsWithDefault }, |
| 80 | + }; |
| 81 | + |
| 82 | + const testCases = makeTestCases(expectedOutputs); |
| 83 | + |
| 84 | + it.each(testCases)('%s', (_, requireResult, expectedOutput) => { |
| 85 | + expect(_interopNamespace(requireResult)).toEqual(_interopNamespaceOrig(requireResult)); |
| 86 | + expect(_interopNamespace(requireResult)).toEqual(expectedOutput); |
| 87 | + }); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe('_interopNamespaceDefaultOnly', () => { |
| 92 | + describe('returns the same result as the original', () => { |
| 93 | + const expectedOutputs: Record<string, RequireResult> = { |
| 94 | + withESModuleFlag: { default: withESModuleFlag }, |
| 95 | + withESModuleFlagAndDefault: { default: withESModuleFlagAndDefault }, |
| 96 | + namedExports: { default: namedExports }, |
| 97 | + withNonEnumerableProp: { default: withNonEnumerableProp }, |
| 98 | + withDefaultExport: { default: withDefaultExport }, |
| 99 | + withOnlyDefaultExport: { default: withOnlyDefaultExport }, |
| 100 | + exportsEquals: { default: exportsEquals }, |
| 101 | + exportsEqualsWithDefault: { default: exportsEqualsWithDefault }, |
| 102 | + }; |
| 103 | + |
| 104 | + const testCases = makeTestCases(expectedOutputs); |
| 105 | + |
| 106 | + it.each(testCases)('%s', (_, requireResult, expectedOutput) => { |
| 107 | + expect(_interopNamespaceDefaultOnly(requireResult)).toEqual(_interopNamespaceDefaultOnlyOrig(requireResult)); |
| 108 | + expect(_interopNamespaceDefaultOnly(requireResult)).toEqual(expectedOutput); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('_interopRequireWildcard', () => { |
| 114 | + describe('returns the same result as the original', () => { |
| 115 | + const expectedOutputs: Record<string, RequireResult> = { |
| 116 | + withESModuleFlag: withESModuleFlag, |
| 117 | + withESModuleFlagAndDefault: withESModuleFlagAndDefault, |
| 118 | + namedExports: { ...namedExports, default: namedExports }, |
| 119 | + withNonEnumerableProp: { |
| 120 | + ...withNonEnumerableProp, |
| 121 | + default: withNonEnumerableProp, |
| 122 | + }, |
| 123 | + withDefaultExport: { ...withDefaultExport, default: withDefaultExport }, |
| 124 | + withOnlyDefaultExport: { default: withOnlyDefaultExport }, |
| 125 | + exportsEquals: { default: exportsEquals }, |
| 126 | + exportsEqualsWithDefault: { default: exportsEqualsWithDefault }, |
| 127 | + }; |
| 128 | + |
| 129 | + const testCases = makeTestCases(expectedOutputs); |
| 130 | + |
| 131 | + it.each(testCases)('%s', (_, requireResult, expectedOutput) => { |
| 132 | + expect(_interopRequireWildcard(requireResult)).toEqual(_interopRequireWildcardOrig(requireResult)); |
| 133 | + expect(_interopRequireWildcard(requireResult)).toEqual(expectedOutput); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe('_interopDefault', () => { |
| 139 | + describe('returns the same result as the original', () => { |
| 140 | + const expectedOutputs: Record<string, RequireResult> = { |
| 141 | + withESModuleFlag: undefined as unknown as RequireResult, |
| 142 | + withESModuleFlagAndDefault: withESModuleFlagAndDefault.default as RequireResult, |
| 143 | + namedExports: namedExports, |
| 144 | + withNonEnumerableProp: withNonEnumerableProp, |
| 145 | + withDefaultExport: withDefaultExport, |
| 146 | + withOnlyDefaultExport: withOnlyDefaultExport, |
| 147 | + exportsEquals: exportsEquals, |
| 148 | + exportsEqualsWithDefault: exportsEqualsWithDefault, |
| 149 | + }; |
| 150 | + |
| 151 | + const testCases = makeTestCases(expectedOutputs); |
| 152 | + |
| 153 | + it.each(testCases)('%s', (_, requireResult, expectedOutput) => { |
| 154 | + expect(_interopDefault(requireResult)).toEqual(_interopDefaultOrig(requireResult)); |
| 155 | + expect(_interopDefault(requireResult)).toEqual(expectedOutput); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('_interopRequireDefault', () => { |
| 161 | + describe('returns the same result as the original', () => { |
| 162 | + const expectedOutputs: Record<string, RequireResult> = { |
| 163 | + withESModuleFlag: withESModuleFlag, |
| 164 | + withESModuleFlagAndDefault: withESModuleFlagAndDefault, |
| 165 | + namedExports: { default: namedExports }, |
| 166 | + withNonEnumerableProp: { default: withNonEnumerableProp }, |
| 167 | + withDefaultExport: { default: withDefaultExport }, |
| 168 | + withOnlyDefaultExport: { default: withOnlyDefaultExport }, |
| 169 | + exportsEquals: { default: exportsEquals }, |
| 170 | + exportsEqualsWithDefault: { default: exportsEqualsWithDefault }, |
| 171 | + }; |
| 172 | + |
| 173 | + const testCases = makeTestCases(expectedOutputs); |
| 174 | + |
| 175 | + it.each(testCases)('%s', (_, requireResult, expectedOutput) => { |
| 176 | + expect(_interopRequireDefault(requireResult)).toEqual(_interopRequireDefaultOrig(requireResult)); |
| 177 | + expect(_interopRequireDefault(requireResult)).toEqual(expectedOutput); |
| 178 | + }); |
| 179 | + }); |
| 180 | +}); |
0 commit comments