|
| 1 | +import { mockBuildCtx, mockValidatedConfig } from '@stencil/core/testing'; |
| 2 | +import childProcess from 'child_process'; |
| 3 | + |
| 4 | +import * as d from '../../../declarations'; |
| 5 | +import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub'; |
| 6 | +import { writeExportMaps } from '../write-export-maps'; |
| 7 | + |
| 8 | +describe('writeExportMaps', () => { |
| 9 | + let config: d.ValidatedConfig; |
| 10 | + let buildCtx: d.BuildCtx; |
| 11 | + let execSyncSpy: jest.SpyInstance; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + config = mockValidatedConfig(); |
| 15 | + buildCtx = mockBuildCtx(config); |
| 16 | + |
| 17 | + execSyncSpy = jest.spyOn(childProcess, 'execSync').mockImplementation(() => ''); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should not generate any exports if there are no output targets', () => { |
| 25 | + writeExportMaps(config, buildCtx); |
| 26 | + |
| 27 | + expect(execSyncSpy).toHaveBeenCalledTimes(0); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should generate the default exports for the lazy build if present', () => { |
| 31 | + config.outputTargets = [ |
| 32 | + { |
| 33 | + type: 'dist', |
| 34 | + dir: '/dist', |
| 35 | + typesDir: '/dist/types', |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + writeExportMaps(config, buildCtx); |
| 40 | + |
| 41 | + expect(execSyncSpy).toHaveBeenCalledTimes(3); |
| 42 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[.][import]"="./dist/index.js"`); |
| 43 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[.][require]"="./dist/index.cjs.js"`); |
| 44 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[.][types]"="./dist/types/index.d.ts"`); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should generate the default exports for the custom elements build if present', () => { |
| 48 | + config.outputTargets = [ |
| 49 | + { |
| 50 | + type: 'dist-custom-elements', |
| 51 | + dir: '/dist/components', |
| 52 | + generateTypeDeclarations: true, |
| 53 | + }, |
| 54 | + ]; |
| 55 | + |
| 56 | + writeExportMaps(config, buildCtx); |
| 57 | + |
| 58 | + expect(execSyncSpy).toHaveBeenCalledTimes(2); |
| 59 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[.][import]"="./dist/components/index.js"`); |
| 60 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[.][types]"="./dist/components/index.d.ts"`); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should generate the lazy loader exports if the output target is present', () => { |
| 64 | + config.rootDir = '/'; |
| 65 | + config.outputTargets.push({ |
| 66 | + type: 'dist-lazy-loader', |
| 67 | + dir: '/dist/lazy-loader', |
| 68 | + empty: true, |
| 69 | + esmDir: '/dist/esm', |
| 70 | + cjsDir: '/dist/cjs', |
| 71 | + componentDts: '/dist/components.d.ts', |
| 72 | + }); |
| 73 | + |
| 74 | + writeExportMaps(config, buildCtx); |
| 75 | + |
| 76 | + expect(execSyncSpy).toHaveBeenCalledTimes(3); |
| 77 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[./loader][import]"="./dist/lazy-loader/index.js"`); |
| 78 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[./loader][require]"="./dist/lazy-loader/index.cjs"`); |
| 79 | + expect(execSyncSpy).toHaveBeenCalledWith(`npm pkg set "exports[./loader][types]"="./dist/lazy-loader/index.d.ts"`); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should generate the custom elements exports if the output target is present', () => { |
| 83 | + config.rootDir = '/'; |
| 84 | + config.outputTargets.push({ |
| 85 | + type: 'dist-custom-elements', |
| 86 | + dir: '/dist/components', |
| 87 | + generateTypeDeclarations: true, |
| 88 | + }); |
| 89 | + |
| 90 | + buildCtx.components = [ |
| 91 | + stubComponentCompilerMeta({ |
| 92 | + tagName: 'my-component', |
| 93 | + componentClassName: 'MyComponent', |
| 94 | + }), |
| 95 | + ]; |
| 96 | + |
| 97 | + writeExportMaps(config, buildCtx); |
| 98 | + |
| 99 | + expect(execSyncSpy).toHaveBeenCalledTimes(4); |
| 100 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 101 | + `npm pkg set "exports[./my-component][import]"="./dist/components/my-component.js"`, |
| 102 | + ); |
| 103 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 104 | + `npm pkg set "exports[./my-component][types]"="./dist/components/my-component.d.ts"`, |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should generate the custom elements exports for multiple components', () => { |
| 109 | + config.rootDir = '/'; |
| 110 | + config.outputTargets.push({ |
| 111 | + type: 'dist-custom-elements', |
| 112 | + dir: '/dist/components', |
| 113 | + generateTypeDeclarations: true, |
| 114 | + }); |
| 115 | + |
| 116 | + buildCtx.components = [ |
| 117 | + stubComponentCompilerMeta({ |
| 118 | + tagName: 'my-component', |
| 119 | + componentClassName: 'MyComponent', |
| 120 | + }), |
| 121 | + stubComponentCompilerMeta({ |
| 122 | + tagName: 'my-other-component', |
| 123 | + componentClassName: 'MyOtherComponent', |
| 124 | + }), |
| 125 | + ]; |
| 126 | + |
| 127 | + writeExportMaps(config, buildCtx); |
| 128 | + |
| 129 | + expect(execSyncSpy).toHaveBeenCalledTimes(6); |
| 130 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 131 | + `npm pkg set "exports[./my-component][import]"="./dist/components/my-component.js"`, |
| 132 | + ); |
| 133 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 134 | + `npm pkg set "exports[./my-component][types]"="./dist/components/my-component.d.ts"`, |
| 135 | + ); |
| 136 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 137 | + `npm pkg set "exports[./my-other-component][import]"="./dist/components/my-other-component.js"`, |
| 138 | + ); |
| 139 | + expect(execSyncSpy).toHaveBeenCalledWith( |
| 140 | + `npm pkg set "exports[./my-other-component][types]"="./dist/components/my-other-component.d.ts"`, |
| 141 | + ); |
| 142 | + }); |
| 143 | +}); |
0 commit comments