|
| 1 | +const { build } = require('./lib/cli'); |
| 2 | +const { subject } = require('./lib/output'); |
| 3 | + |
| 4 | +const formats = ['cjs', 'esm']; |
| 5 | + |
| 6 | +const prerenderUrlFiles = [ |
| 7 | + 'array.js', |
| 8 | + 'stringified-array.js', |
| 9 | + 'function-returning-array.js', |
| 10 | + 'function-returning-stringified-array.js', |
| 11 | +]; |
| 12 | + |
| 13 | +const preactConfigFiles = ['function.js', 'object.js']; |
| 14 | + |
| 15 | +describe('config files', () => { |
| 16 | + describe('prerender-urls', () => { |
| 17 | + it(`should load the 'prerender-urls.json' file`, async () => { |
| 18 | + let dir = await subject('multiple-config-files'); |
| 19 | + |
| 20 | + const logSpy = jest.spyOn(process.stdout, 'write'); |
| 21 | + |
| 22 | + await build(dir); |
| 23 | + |
| 24 | + expect(logSpy).not.toHaveBeenCalledWith( |
| 25 | + expect.stringContaining( |
| 26 | + 'Failed to load prerenderUrls file, using default!' |
| 27 | + ) |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + formats.forEach(moduleFormat => { |
| 32 | + prerenderUrlFiles.forEach(dataFormat => { |
| 33 | + it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => { |
| 34 | + let dir = await subject('multiple-config-files'); |
| 35 | + |
| 36 | + const logSpy = jest.spyOn(process.stdout, 'write'); |
| 37 | + |
| 38 | + await build(dir, { |
| 39 | + prerenderUrls: `prerender/${moduleFormat}/${dataFormat}`, |
| 40 | + }); |
| 41 | + |
| 42 | + expect(logSpy).not.toHaveBeenCalledWith( |
| 43 | + expect.stringContaining( |
| 44 | + 'Failed to load prerenderUrls file, using default!' |
| 45 | + ) |
| 46 | + ); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it(`should fail to load malformed prerender-urls data in ${moduleFormat}`, async () => { |
| 51 | + let dir = await subject('multiple-config-files'); |
| 52 | + |
| 53 | + const logSpy = jest.spyOn(process.stdout, 'write'); |
| 54 | + |
| 55 | + await build(dir, { |
| 56 | + prerenderUrls: `prerender/${moduleFormat}/returns-bad-json.js`, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(logSpy).toHaveBeenCalledWith( |
| 60 | + expect.stringContaining( |
| 61 | + 'Failed to load prerenderUrls file, using default!' |
| 62 | + ) |
| 63 | + ); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('preact.config', () => { |
| 69 | + formats.forEach(moduleFormat => { |
| 70 | + preactConfigFiles.forEach(dataFormat => { |
| 71 | + it(`should load the '${dataFormat}' file in ${moduleFormat}`, async () => { |
| 72 | + let dir = await subject('multiple-config-files'); |
| 73 | + |
| 74 | + await expect( |
| 75 | + build(dir, { config: `preactConfig/${moduleFormat}/${dataFormat}` }) |
| 76 | + ).resolves.not.toThrow(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it(`should fail to load malformed config data in ${moduleFormat}`, async () => { |
| 81 | + let dir = await subject('multiple-config-files'); |
| 82 | + |
| 83 | + await expect( |
| 84 | + build(dir, { |
| 85 | + config: `preactConfig/${moduleFormat}/returns-bad-config.js`, |
| 86 | + }) |
| 87 | + ).rejects.toThrow('Failed to load preact-cli config!'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments