cli-migrate.e2e-spec.ts 6.1 KB

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