|
1 |
| -import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 | import * as util from '../../src/config/util';
|
3 | 3 | import { DEFAULT_SERVER_EXTERNAL_PACKAGES } from '../../src/config/withSentryConfig';
|
4 | 4 | import { defaultRuntimePhase, defaultsObject, exportedNextConfig, userNextConfig } from './fixtures';
|
@@ -269,6 +269,280 @@ describe('withSentryConfig', () => {
|
269 | 269 | });
|
270 | 270 | });
|
271 | 271 |
|
| 272 | + describe('bundler detection with version-based defaults', () => { |
| 273 | + const originalTurbopack = process.env.TURBOPACK; |
| 274 | + const originalArgv = process.argv; |
| 275 | + |
| 276 | + beforeEach(() => { |
| 277 | + process.argv = [...originalArgv]; |
| 278 | + delete process.env.TURBOPACK; |
| 279 | + }); |
| 280 | + |
| 281 | + afterEach(() => { |
| 282 | + vi.restoreAllMocks(); |
| 283 | + process.env.TURBOPACK = originalTurbopack; |
| 284 | + process.argv = originalArgv; |
| 285 | + }); |
| 286 | + |
| 287 | + describe('Next.js 16+ defaults to turbopack', () => { |
| 288 | + it('uses turbopack config by default for Next.js 16.0.0', () => { |
| 289 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0'); |
| 290 | + |
| 291 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 292 | + |
| 293 | + expect(finalConfig.turbopack).toBeDefined(); |
| 294 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 295 | + }); |
| 296 | + |
| 297 | + it('uses turbopack config by default for Next.js 17.0.0', () => { |
| 298 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('17.0.0'); |
| 299 | + |
| 300 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 301 | + |
| 302 | + expect(finalConfig.turbopack).toBeDefined(); |
| 303 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 304 | + }); |
| 305 | + |
| 306 | + it('uses webpack when --webpack flag is present on Next.js 16.0.0', () => { |
| 307 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0'); |
| 308 | + process.argv.push('--webpack'); |
| 309 | + |
| 310 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 311 | + |
| 312 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 313 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 314 | + }); |
| 315 | + |
| 316 | + it('prioritizes TURBOPACK env var over --webpack flag', () => { |
| 317 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0'); |
| 318 | + process.env.TURBOPACK = '1'; |
| 319 | + process.argv.push('--webpack'); |
| 320 | + |
| 321 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 322 | + |
| 323 | + expect(finalConfig.turbopack).toBeDefined(); |
| 324 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 325 | + }); |
| 326 | + }); |
| 327 | + |
| 328 | + describe('Next.js 15.6.0-canary.38+ defaults to turbopack', () => { |
| 329 | + it('uses turbopack config by default for 15.6.0-canary.38', () => { |
| 330 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.38'); |
| 331 | + |
| 332 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 333 | + |
| 334 | + expect(finalConfig.turbopack).toBeDefined(); |
| 335 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 336 | + }); |
| 337 | + |
| 338 | + it('uses turbopack config by default for 15.6.0-canary.50', () => { |
| 339 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.50'); |
| 340 | + |
| 341 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 342 | + |
| 343 | + expect(finalConfig.turbopack).toBeDefined(); |
| 344 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 345 | + }); |
| 346 | + |
| 347 | + it('uses turbopack config by default for 15.7.0-canary.1', () => { |
| 348 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.7.0-canary.1'); |
| 349 | + |
| 350 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 351 | + |
| 352 | + expect(finalConfig.turbopack).toBeDefined(); |
| 353 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 354 | + }); |
| 355 | + |
| 356 | + it('uses webpack when --webpack flag is present on 15.6.0-canary.38', () => { |
| 357 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.38'); |
| 358 | + process.argv.push('--webpack'); |
| 359 | + |
| 360 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 361 | + |
| 362 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 363 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 364 | + }); |
| 365 | + |
| 366 | + it('uses webpack when --webpack flag is present on 15.7.0-canary.1', () => { |
| 367 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.7.0-canary.1'); |
| 368 | + process.argv.push('--webpack'); |
| 369 | + |
| 370 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 371 | + |
| 372 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 373 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 374 | + }); |
| 375 | + }); |
| 376 | + |
| 377 | + describe('Next.js 15.6.0-canary.37 and below defaults to webpack', () => { |
| 378 | + it('uses webpack config by default for 15.6.0-canary.37', () => { |
| 379 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.37'); |
| 380 | + |
| 381 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 382 | + |
| 383 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 384 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 385 | + }); |
| 386 | + |
| 387 | + it('uses webpack config by default for 15.6.0-canary.1', () => { |
| 388 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.1'); |
| 389 | + |
| 390 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 391 | + |
| 392 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 393 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 394 | + }); |
| 395 | + |
| 396 | + it('uses turbopack when TURBOPACK env var is set on 15.6.0-canary.37', () => { |
| 397 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0-canary.37'); |
| 398 | + process.env.TURBOPACK = '1'; |
| 399 | + |
| 400 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 401 | + |
| 402 | + expect(finalConfig.turbopack).toBeDefined(); |
| 403 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 404 | + }); |
| 405 | + }); |
| 406 | + |
| 407 | + describe('Next.js 15.6.x stable releases default to webpack', () => { |
| 408 | + it('uses webpack config by default for 15.6.0 stable', () => { |
| 409 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0'); |
| 410 | + |
| 411 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 412 | + |
| 413 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 414 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 415 | + }); |
| 416 | + |
| 417 | + it('uses webpack config by default for 15.6.1 stable', () => { |
| 418 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.1'); |
| 419 | + |
| 420 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 421 | + |
| 422 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 423 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 424 | + }); |
| 425 | + |
| 426 | + it('uses webpack config by default for 15.7.0 stable', () => { |
| 427 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.7.0'); |
| 428 | + |
| 429 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 430 | + |
| 431 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 432 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 433 | + }); |
| 434 | + |
| 435 | + it('uses turbopack when explicitly requested via env var on 15.6.0 stable', () => { |
| 436 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.6.0'); |
| 437 | + process.env.TURBOPACK = '1'; |
| 438 | + |
| 439 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 440 | + |
| 441 | + expect(finalConfig.turbopack).toBeDefined(); |
| 442 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 443 | + }); |
| 444 | + }); |
| 445 | + |
| 446 | + describe('older Next.js versions default to webpack', () => { |
| 447 | + it.each([['15.5.0'], ['15.0.0'], ['14.2.0'], ['13.5.0']])( |
| 448 | + 'uses webpack config by default for Next.js %s', |
| 449 | + version => { |
| 450 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue(version); |
| 451 | + |
| 452 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 453 | + |
| 454 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 455 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 456 | + }, |
| 457 | + ); |
| 458 | + |
| 459 | + it.each([['15.5.0-canary.100'], ['15.0.0-canary.1'], ['14.2.0-canary.50']])( |
| 460 | + 'uses webpack config by default for Next.js %s canary', |
| 461 | + version => { |
| 462 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue(version); |
| 463 | + |
| 464 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 465 | + |
| 466 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 467 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 468 | + }, |
| 469 | + ); |
| 470 | + }); |
| 471 | + |
| 472 | + describe('warnings are shown for unsupported turbopack usage', () => { |
| 473 | + let consoleWarnSpy: ReturnType<typeof vi.spyOn>; |
| 474 | + |
| 475 | + beforeEach(() => { |
| 476 | + consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 477 | + }); |
| 478 | + |
| 479 | + it('warns when using turbopack on unsupported version', () => { |
| 480 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.0.0'); |
| 481 | + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false); |
| 482 | + process.env.TURBOPACK = '1'; |
| 483 | + |
| 484 | + materializeFinalNextConfig(exportedNextConfig); |
| 485 | + |
| 486 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 487 | + expect.stringContaining('WARNING: You are using the Sentry SDK with Turbopack'), |
| 488 | + ); |
| 489 | + expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('15.0.0')); |
| 490 | + }); |
| 491 | + |
| 492 | + it('does not warn when using turbopack on supported version', () => { |
| 493 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('16.0.0'); |
| 494 | + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(true); |
| 495 | + process.env.TURBOPACK = '1'; |
| 496 | + |
| 497 | + materializeFinalNextConfig(exportedNextConfig); |
| 498 | + |
| 499 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 500 | + }); |
| 501 | + |
| 502 | + it('does not warn when using webpack', () => { |
| 503 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('15.0.0'); |
| 504 | + vi.spyOn(util, 'supportsProductionCompileHook').mockReturnValue(false); |
| 505 | + |
| 506 | + materializeFinalNextConfig(exportedNextConfig); |
| 507 | + |
| 508 | + expect(consoleWarnSpy).not.toHaveBeenCalled(); |
| 509 | + }); |
| 510 | + }); |
| 511 | + |
| 512 | + describe('edge cases', () => { |
| 513 | + it('defaults to webpack when Next.js version cannot be determined', () => { |
| 514 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue(undefined); |
| 515 | + |
| 516 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 517 | + |
| 518 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 519 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 520 | + }); |
| 521 | + |
| 522 | + it('uses turbopack when TURBOPACK env var is set even when version is undefined', () => { |
| 523 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue(undefined); |
| 524 | + process.env.TURBOPACK = '1'; |
| 525 | + |
| 526 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 527 | + |
| 528 | + // Note: turbopack config won't be added when version is undefined because |
| 529 | + // isTurbopackSupported will be false, but webpack config should still be skipped |
| 530 | + expect(finalConfig.webpack).toBe(exportedNextConfig.webpack); |
| 531 | + // Turbopack config is only added when both isTurbopack AND isTurbopackSupported are true |
| 532 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 533 | + }); |
| 534 | + |
| 535 | + it('handles malformed version strings gracefully', () => { |
| 536 | + vi.spyOn(util, 'getNextjsVersion').mockReturnValue('not.a.version'); |
| 537 | + |
| 538 | + const finalConfig = materializeFinalNextConfig(exportedNextConfig); |
| 539 | + |
| 540 | + expect(finalConfig.turbopack).toBeUndefined(); |
| 541 | + expect(finalConfig.webpack).toBeInstanceOf(Function); |
| 542 | + }); |
| 543 | + }); |
| 544 | + }); |
| 545 | + |
272 | 546 | describe('turbopack sourcemap configuration', () => {
|
273 | 547 | const originalTurbopack = process.env.TURBOPACK;
|
274 | 548 |
|
|
0 commit comments