|
1 | 1 | import {buildFunctionExtension} from './extension.js' |
2 | 2 | import {testFunctionExtension} from '../../models/app/app.test-data.js' |
3 | | -import {buildJSFunction, runWasmOpt, runTrampoline} from '../function/build.js' |
| 3 | +import {buildGraphqlTypes, buildJSFunction, runWasmOpt, runTrampoline} from '../function/build.js' |
4 | 4 | import {ExtensionInstance} from '../../models/extensions/extension-instance.js' |
5 | 5 | import {FunctionConfigType} from '../../models/extensions/specifications/function.js' |
6 | 6 | import {beforeEach, describe, expect, test, vi} from 'vitest' |
@@ -254,6 +254,128 @@ describe('buildFunctionExtension', () => { |
254 | 254 | expect(runWasmOpt).not.toHaveBeenCalled() |
255 | 255 | }) |
256 | 256 |
|
| 257 | + test('runs typegen_command before build for non-JS function', async () => { |
| 258 | + // Given |
| 259 | + const configWithTypegen = { |
| 260 | + name: 'MyFunction', |
| 261 | + type: 'product_discounts', |
| 262 | + description: '', |
| 263 | + build: { |
| 264 | + command: 'make build', |
| 265 | + path: 'dist/index.wasm', |
| 266 | + wasm_opt: true, |
| 267 | + typegen_command: 'npx shopify-function-codegen --schema schema.graphql', |
| 268 | + }, |
| 269 | + configuration_ui: true, |
| 270 | + api_version: '2022-07', |
| 271 | + metafields: [], |
| 272 | + } |
| 273 | + extension = await testFunctionExtension({config: configWithTypegen}) |
| 274 | + |
| 275 | + // When |
| 276 | + await expect( |
| 277 | + buildFunctionExtension(extension, { |
| 278 | + stdout, |
| 279 | + stderr, |
| 280 | + signal, |
| 281 | + app, |
| 282 | + environment: 'production', |
| 283 | + }), |
| 284 | + ).resolves.toBeUndefined() |
| 285 | + |
| 286 | + // Then |
| 287 | + expect(buildGraphqlTypes).toHaveBeenCalledWith(extension, { |
| 288 | + stdout, |
| 289 | + stderr, |
| 290 | + signal, |
| 291 | + app, |
| 292 | + environment: 'production', |
| 293 | + }) |
| 294 | + expect(exec).toHaveBeenCalledWith('make', ['build'], { |
| 295 | + stdout, |
| 296 | + stderr, |
| 297 | + cwd: extension.directory, |
| 298 | + signal, |
| 299 | + }) |
| 300 | + }) |
| 301 | + |
| 302 | + test('runs typegen_command before build for JS function with custom build command', async () => { |
| 303 | + // Given |
| 304 | + const configWithTypegen = { |
| 305 | + name: 'MyFunction', |
| 306 | + type: 'product_discounts', |
| 307 | + description: '', |
| 308 | + build: { |
| 309 | + command: 'make build', |
| 310 | + path: 'dist/index.wasm', |
| 311 | + wasm_opt: true, |
| 312 | + typegen_command: 'custom-typegen --output types.ts', |
| 313 | + }, |
| 314 | + configuration_ui: true, |
| 315 | + api_version: '2022-07', |
| 316 | + metafields: [], |
| 317 | + } |
| 318 | + extension = await testFunctionExtension({config: configWithTypegen, entryPath: 'src/index.js'}) |
| 319 | + |
| 320 | + // When |
| 321 | + await expect( |
| 322 | + buildFunctionExtension(extension, { |
| 323 | + stdout, |
| 324 | + stderr, |
| 325 | + signal, |
| 326 | + app, |
| 327 | + environment: 'production', |
| 328 | + }), |
| 329 | + ).resolves.toBeUndefined() |
| 330 | + |
| 331 | + // Then |
| 332 | + expect(buildGraphqlTypes).toHaveBeenCalledWith(extension, { |
| 333 | + stdout, |
| 334 | + stderr, |
| 335 | + signal, |
| 336 | + app, |
| 337 | + environment: 'production', |
| 338 | + }) |
| 339 | + expect(exec).toHaveBeenCalledWith('make', ['build'], { |
| 340 | + stdout, |
| 341 | + stderr, |
| 342 | + cwd: extension.directory, |
| 343 | + signal, |
| 344 | + }) |
| 345 | + }) |
| 346 | + |
| 347 | + test('does not run typegen when typegen_command is not set', async () => { |
| 348 | + // Given |
| 349 | + const configWithoutTypegen = { |
| 350 | + name: 'MyFunction', |
| 351 | + type: 'product_discounts', |
| 352 | + description: '', |
| 353 | + build: { |
| 354 | + command: 'make build', |
| 355 | + path: 'dist/index.wasm', |
| 356 | + wasm_opt: true, |
| 357 | + }, |
| 358 | + configuration_ui: true, |
| 359 | + api_version: '2022-07', |
| 360 | + metafields: [], |
| 361 | + } |
| 362 | + extension = await testFunctionExtension({config: configWithoutTypegen}) |
| 363 | + |
| 364 | + // When |
| 365 | + await expect( |
| 366 | + buildFunctionExtension(extension, { |
| 367 | + stdout, |
| 368 | + stderr, |
| 369 | + signal, |
| 370 | + app, |
| 371 | + environment: 'production', |
| 372 | + }), |
| 373 | + ).resolves.toBeUndefined() |
| 374 | + |
| 375 | + // Then |
| 376 | + expect(buildGraphqlTypes).not.toHaveBeenCalled() |
| 377 | + }) |
| 378 | + |
257 | 379 | test('handles function with build config but undefined path', async () => { |
258 | 380 | // Given |
259 | 381 | const configWithoutPath = { |
|
0 commit comments