|
| 1 | +import type * as d from '@stencil/core/declarations'; |
| 2 | +import { mockBuildCtx, mockCompilerCtx, mockCompilerSystem, mockValidatedConfig } from '@stencil/core/testing'; |
| 3 | +import { DIST, resolve } from '@utils'; |
| 4 | + |
| 5 | +import { validateDist } from '../../config/outputs/validate-dist'; |
| 6 | +import { outputLazyLoader } from '../output-lazy-loader'; |
| 7 | + |
| 8 | +function setup(configOverrides: Partial<d.ValidatedConfig> = {}) { |
| 9 | + const sys = mockCompilerSystem(); |
| 10 | + const config: d.ValidatedConfig = mockValidatedConfig({ |
| 11 | + ...configOverrides, |
| 12 | + configPath: '/testing-path', |
| 13 | + buildAppCore: true, |
| 14 | + namespace: 'TestApp', |
| 15 | + outputTargets: [ |
| 16 | + { |
| 17 | + type: DIST, |
| 18 | + dir: 'my-test-dir', |
| 19 | + }, |
| 20 | + ], |
| 21 | + srcDir: '/src', |
| 22 | + sys, |
| 23 | + }); |
| 24 | + |
| 25 | + config.outputTargets = validateDist(config, config.outputTargets); |
| 26 | + |
| 27 | + const compilerCtx = mockCompilerCtx(config); |
| 28 | + const writeFileSpy = jest.spyOn(compilerCtx.fs, 'writeFile'); |
| 29 | + const buildCtx = mockBuildCtx(config, compilerCtx); |
| 30 | + |
| 31 | + return { config, compilerCtx, buildCtx, writeFileSpy }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('Lazy Loader Output Target', () => { |
| 35 | + let config: d.ValidatedConfig; |
| 36 | + let compilerCtx: d.CompilerCtx; |
| 37 | + let writeFileSpy: jest.SpyInstance; |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + writeFileSpy.mockRestore(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should write code for initializing polyfills when buildEs5=true', async () => { |
| 44 | + ({ config, compilerCtx, writeFileSpy } = setup({ buildEs5: true })); |
| 45 | + await outputLazyLoader(config, compilerCtx); |
| 46 | + |
| 47 | + const expectedIndexOutput = `export * from '../esm/polyfills/index.js'; |
| 48 | +export * from '../esm-es5/loader.js';`; |
| 49 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.js'), expectedIndexOutput); |
| 50 | + |
| 51 | + const expectedCjsIndexOutput = `module.exports = require('../cjs/loader.cjs.js'); |
| 52 | +module.exports.applyPolyfills = function() { return Promise.resolve() };`; |
| 53 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.cjs.js'), expectedCjsIndexOutput); |
| 54 | + |
| 55 | + const expectedES2017Output = `export * from '../esm/polyfills/index.js'; |
| 56 | +export * from '../esm/loader.js';`; |
| 57 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.es2017.js'), expectedES2017Output); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should exclude polyfill code when buildEs5=false', async () => { |
| 61 | + ({ config, compilerCtx, writeFileSpy } = setup({ buildEs5: false })); |
| 62 | + await outputLazyLoader(config, compilerCtx); |
| 63 | + |
| 64 | + const expectedIndexOutput = `export * from '../esm/loader.js';`; |
| 65 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.js'), expectedIndexOutput); |
| 66 | + |
| 67 | + const expectedCjsIndexOutput = `module.exports = require('../cjs/loader.cjs.js');`; |
| 68 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.cjs.js'), expectedCjsIndexOutput); |
| 69 | + |
| 70 | + const expectedES2017Output = `export * from '../esm/loader.js';`; |
| 71 | + expect(writeFileSpy).toHaveBeenCalledWith(resolve('/my-test-dir/loader/index.es2017.js'), expectedES2017Output); |
| 72 | + }); |
| 73 | +}); |
0 commit comments