ast-utils.spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import ts from 'typescript';
  2. import { describe, expect, it } from 'vitest';
  3. import { findConfigExport, getPluginInfo } from './ast-utils.js';
  4. describe('getPluginInfo', () => {
  5. it('should return undefined when no plugin class is found', () => {
  6. const sourceText = `
  7. class NotAPlugin {
  8. constructor() {}
  9. }
  10. `;
  11. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  12. const result = getPluginInfo(sourceFile);
  13. expect(result).toBeUndefined();
  14. });
  15. it('should return plugin info when a valid plugin class is found', () => {
  16. const sourceText = `
  17. @VendurePlugin({
  18. imports: [],
  19. providers: []
  20. })
  21. class TestPlugin {
  22. constructor() {}
  23. }
  24. `;
  25. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  26. const result = getPluginInfo(sourceFile);
  27. expect(result).toEqual({
  28. name: 'TestPlugin',
  29. pluginPath: 'path/to',
  30. dashboardEntryPath: undefined,
  31. });
  32. });
  33. it('should handle multiple classes but only return the plugin one', () => {
  34. const sourceText = `
  35. class NotAPlugin {
  36. constructor() {}
  37. }
  38. @VendurePlugin({
  39. imports: [],
  40. providers: []
  41. })
  42. class TestPlugin {
  43. constructor() {}
  44. }
  45. class AnotherClass {
  46. constructor() {}
  47. }
  48. `;
  49. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  50. const result = getPluginInfo(sourceFile);
  51. expect(result).toEqual({
  52. name: 'TestPlugin',
  53. pluginPath: 'path/to',
  54. dashboardEntryPath: undefined,
  55. });
  56. });
  57. it('should determine the dashboard entry path when it is provided', () => {
  58. const sourceText = `
  59. @VendurePlugin({
  60. imports: [],
  61. providers: [],
  62. dashboard: './dashboard/index.tsx',
  63. })
  64. class TestPlugin {
  65. constructor() {}
  66. }
  67. `;
  68. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  69. const result = getPluginInfo(sourceFile);
  70. expect(result).toEqual({
  71. name: 'TestPlugin',
  72. pluginPath: 'path/to',
  73. dashboardEntryPath: './dashboard/index.tsx',
  74. });
  75. });
  76. });
  77. describe('findConfigExport', () => {
  78. it('should return undefined when no VendureConfig export is found', () => {
  79. const sourceText = `
  80. export const notConfig = {
  81. some: 'value'
  82. };
  83. `;
  84. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  85. const result = findConfigExport(sourceFile);
  86. expect(result).toBeUndefined();
  87. });
  88. it('should find exported variable with VendureConfig type', () => {
  89. const sourceText = `
  90. import { VendureConfig } from '@vendure/core';
  91. export const config: VendureConfig = {
  92. authOptions: {
  93. tokenMethod: 'bearer'
  94. }
  95. };
  96. `;
  97. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  98. const result = findConfigExport(sourceFile);
  99. expect(result).toBe('config');
  100. });
  101. it('should find exported variable with VendureConfig type among other exports', () => {
  102. const sourceText = `
  103. import { VendureConfig } from '@vendure/core';
  104. export const otherExport = 'value';
  105. export const config: VendureConfig = {
  106. authOptions: {
  107. tokenMethod: 'bearer'
  108. }
  109. };
  110. export const anotherExport = 123;
  111. `;
  112. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  113. const result = findConfigExport(sourceFile);
  114. expect(result).toBe('config');
  115. });
  116. });