|  | 
| 1 | 1 | import { describe, test, vi, expect } from 'vitest'; | 
| 2 |  | -import { is_building_for_cloudflare_pages } from './utils.js'; | 
|  | 2 | +import { is_building_for_cloudflare_pages, validate_worker_settings } from './utils.js'; | 
| 3 | 3 | 
 | 
| 4 | 4 | describe('detects Cloudflare Pages project', () => { | 
| 5 | 5 | 	test('by default', () => { | 
| @@ -56,12 +56,89 @@ describe('detects Cloudflare Workers project', () => { | 
| 56 | 56 | 			is_building_for_cloudflare_pages( | 
| 57 | 57 | 				/** @type {import('wrangler').Unstable_Config} */ ({ | 
| 58 | 58 | 					configPath: 'wrangler.jsonc', | 
|  | 59 | +					assets: { | 
|  | 60 | +						directory: 'dist/assets' | 
|  | 61 | +					} | 
|  | 62 | +				}) | 
|  | 63 | +			) | 
|  | 64 | +		).toBe(false); | 
|  | 65 | +	}); | 
|  | 66 | +}); | 
|  | 67 | + | 
|  | 68 | +describe('validates Wrangler config', () => { | 
|  | 69 | +	test('Worker and static assets', () => { | 
|  | 70 | +		expect(() => | 
|  | 71 | +			validate_worker_settings( | 
|  | 72 | +				/** @type {import('wrangler').Unstable_Config} */ ({ | 
|  | 73 | +					configPath: 'wrangler.jsonc', | 
|  | 74 | +					main: 'dist/index.js', | 
| 59 | 75 | 					assets: { | 
| 60 | 76 | 						directory: 'dist/assets', | 
| 61 | 77 | 						binding: 'ASSETS' | 
| 62 | 78 | 					} | 
| 63 | 79 | 				}) | 
| 64 | 80 | 			) | 
| 65 |  | -		).toBe(false); | 
|  | 81 | +		).not.toThrow(); | 
|  | 82 | +	}); | 
|  | 83 | + | 
|  | 84 | +	test('static assets only', () => { | 
|  | 85 | +		expect(() => | 
|  | 86 | +			validate_worker_settings( | 
|  | 87 | +				/** @type {import('wrangler').Unstable_Config} */ ({ | 
|  | 88 | +					configPath: 'wrangler.jsonc', | 
|  | 89 | +					assets: { | 
|  | 90 | +						directory: 'dist/assets' | 
|  | 91 | +					} | 
|  | 92 | +				}) | 
|  | 93 | +			) | 
|  | 94 | +		).not.toThrow(); | 
|  | 95 | +	}); | 
|  | 96 | + | 
|  | 97 | +	test('missing `assets.directory` key', () => { | 
|  | 98 | +		expect(() => | 
|  | 99 | +			validate_worker_settings( | 
|  | 100 | +				/** @type {import('wrangler').Unstable_Config} */ ({ | 
|  | 101 | +					configPath: 'wrangler.jsonc', | 
|  | 102 | +					main: 'dist/index.js', | 
|  | 103 | +					assets: { | 
|  | 104 | +						binding: 'ASSETS' | 
|  | 105 | +					} | 
|  | 106 | +				}) | 
|  | 107 | +			) | 
|  | 108 | +		).toThrow( | 
|  | 109 | +			`You must specify the \`assets.directory\` key in wrangler.jsonc. Consult https://developers.cloudflare.com/workers/static-assets/binding/#directory` | 
|  | 110 | +		); | 
|  | 111 | +	}); | 
|  | 112 | + | 
|  | 113 | +	test('missing `assets.binding` key', () => { | 
|  | 114 | +		expect(() => | 
|  | 115 | +			validate_worker_settings( | 
|  | 116 | +				/** @type {import('wrangler').Unstable_Config} */ ({ | 
|  | 117 | +					configPath: 'wrangler.jsonc', | 
|  | 118 | +					main: 'dist/index.js', | 
|  | 119 | +					assets: { | 
|  | 120 | +						directory: 'dist/assets' | 
|  | 121 | +					} | 
|  | 122 | +				}) | 
|  | 123 | +			) | 
|  | 124 | +		).toThrow( | 
|  | 125 | +			`You must specify the \`assets.binding\` key in wrangler.jsonc before deploying your Worker. Consult https://developers.cloudflare.com/workers/static-assets/binding/#binding` | 
|  | 126 | +		); | 
|  | 127 | +	}); | 
|  | 128 | + | 
|  | 129 | +	test('missing `main` key or should remove `assets.binding` key', () => { | 
|  | 130 | +		expect(() => | 
|  | 131 | +			validate_worker_settings( | 
|  | 132 | +				/** @type {import('wrangler').Unstable_Config} */ ({ | 
|  | 133 | +					configPath: 'wrangler.jsonc', | 
|  | 134 | +					assets: { | 
|  | 135 | +						directory: 'dist/assets', | 
|  | 136 | +						binding: 'ASSETS' | 
|  | 137 | +					} | 
|  | 138 | +				}) | 
|  | 139 | +			) | 
|  | 140 | +		).toThrow( | 
|  | 141 | +			`You must set the \`main\` key in wrangler.jsonc if you want to deploy a Worker alongside your static assets or remove the \`assets.binding\` key if you only want to deploy static assets.` | 
|  | 142 | +		); | 
| 66 | 143 | 	}); | 
| 67 | 144 | }); | 
0 commit comments