cli-migrate.e2e-spec.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { afterEach, describe, expect, it } from 'vitest';
  2. import { CliTestProject, createProblematicTsConfig, createTestProject } from './cli-test-utils';
  3. describe('CLI Migrate Command E2E', () => {
  4. let testProject: CliTestProject;
  5. afterEach(() => {
  6. if (testProject) {
  7. testProject.cleanup();
  8. }
  9. });
  10. describe('tsconfig.json comment stripping', () => {
  11. it('should successfully parse tsconfig.json with /* patterns in path values', async () => {
  12. testProject = createTestProject('tsconfig-comment-test');
  13. // Create a tsconfig.json with the problematic patterns that broke the old implementation
  14. testProject.writeFile('tsconfig.json', createProblematicTsConfig());
  15. // Try to run a CLI command that loads the tsconfig.json
  16. // Since the migrate command is currently not exposed, we'll test the underlying functionality
  17. // by using the --help command which should still load the config for validation
  18. const result = await testProject.runCliCommand(['--help']);
  19. // The command should complete successfully without crashing
  20. expect(result.exitCode).toBe(0);
  21. expect(result.stdout).toContain('Usage:');
  22. });
  23. it('should handle tsconfig.json with various comment patterns', async () => {
  24. testProject = createTestProject('complex-tsconfig-test');
  25. // Create a more complex tsconfig.json with various comment patterns
  26. const complexTsConfig = `{
  27. // Single line comment
  28. "compilerOptions": {
  29. /* Multi-line comment at the beginning */
  30. "target": "ES2021",
  31. "module": "commonjs", // Inline comment
  32. "baseUrl": "./",
  33. /*
  34. * Multi-line comment block
  35. * with multiple lines
  36. */
  37. "paths": {
  38. // This path contains /* which was problematic
  39. "@/vdb/*": ["./node_modules/@vendure/dashboard/src/lib/*"],
  40. "comment": "This // looks like a comment but isn't",
  41. "multiComment": "This /* looks */ like /* multiple */ comments",
  42. "protocol": "file://some-protocol-url/*",
  43. "regex": "some/*/pattern/*/here"
  44. }
  45. /* Final comment */
  46. }
  47. // End comment
  48. }`;
  49. testProject.writeFile('tsconfig.json', complexTsConfig);
  50. // The CLI should handle this complex config without issues
  51. const result = await testProject.runCliCommand(['--help']);
  52. expect(result.exitCode).toBe(0);
  53. });
  54. it('should preserve path mappings with wildcards correctly', async () => {
  55. testProject = createTestProject('wildcard-paths-test');
  56. // Create a tsconfig with various wildcard patterns
  57. const wildcardTsConfig = `{
  58. "compilerOptions": {
  59. "baseUrl": "./",
  60. "paths": {
  61. "@/dashboard/*": ["./node_modules/@vendure/dashboard/src/lib/*"],
  62. "@/plugins/*/src": ["./src/plugins/*/src/index.ts"],
  63. "@/api/*/types": ["./src/api/*/types.ts"],
  64. "*/utils": ["./src/utils/*"],
  65. "glob/**": ["./src/glob/**/*"]
  66. }
  67. }
  68. }`;
  69. testProject.writeFile('tsconfig.json', wildcardTsConfig);
  70. // Test that the CLI doesn't crash with these patterns
  71. const result = await testProject.runCliCommand(['--help']);
  72. expect(result.exitCode).toBe(0);
  73. });
  74. });
  75. describe('CLI command execution', () => {
  76. it('should show help when no arguments provided', async () => {
  77. testProject = createTestProject('help-test');
  78. // CLI shows help but exits with code 1 when no arguments provided
  79. const result = await testProject.runCliCommand([], { expectError: true });
  80. expect(result.exitCode).toBe(1);
  81. expect(result.stderr).toContain('Usage:');
  82. });
  83. it('should show version when --version flag is used', async () => {
  84. testProject = createTestProject('version-test');
  85. const result = await testProject.runCliCommand(['--version']);
  86. expect(result.exitCode).toBe(0);
  87. expect(result.stdout).toMatch(/\d+\.\d+\.\d+/); // Should contain version number
  88. });
  89. it('should handle invalid commands gracefully', async () => {
  90. testProject = createTestProject('invalid-command-test');
  91. const result = await testProject.runCliCommand(['invalid-command'], { expectError: true });
  92. expect(result.exitCode).not.toBe(0);
  93. expect(result.stderr).toContain('unknown command');
  94. });
  95. });
  96. describe('project structure validation', () => {
  97. it('should work with minimal project structure', async () => {
  98. testProject = createTestProject('minimal-project');
  99. // Create minimal files
  100. testProject.writeFile(
  101. 'tsconfig.json',
  102. JSON.stringify(
  103. {
  104. compilerOptions: {
  105. target: 'ES2021',
  106. module: 'commonjs',
  107. },
  108. },
  109. null,
  110. 2,
  111. ),
  112. );
  113. const result = await testProject.runCliCommand(['--help']);
  114. expect(result.exitCode).toBe(0);
  115. });
  116. it('should handle missing tsconfig.json gracefully', async () => {
  117. testProject = createTestProject('no-tsconfig');
  118. // Don't create a tsconfig.json file
  119. const result = await testProject.runCliCommand(['--help']);
  120. // Should still work, as tsconfig.json is not always required for help command
  121. expect(result.exitCode).toBe(0);
  122. });
  123. });
  124. });