ast-utils.spec.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import ts from 'typescript';
  2. import { describe, expect, it } from 'vitest';
  3. import { findConfigExport } from './ast-utils.js';
  4. describe('findConfigExport', () => {
  5. it('should return undefined when no VendureConfig export is found', () => {
  6. const sourceText = `
  7. export const notConfig = {
  8. some: 'value'
  9. };
  10. `;
  11. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  12. const result = findConfigExport(sourceFile);
  13. expect(result).toBeUndefined();
  14. });
  15. it('should find exported variable with VendureConfig type', () => {
  16. const sourceText = `
  17. import { VendureConfig } from '@vendure/core';
  18. export const config: VendureConfig = {
  19. authOptions: {
  20. tokenMethod: 'bearer'
  21. }
  22. };
  23. `;
  24. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  25. const result = findConfigExport(sourceFile);
  26. expect(result).toBe('config');
  27. });
  28. it('should find exported variable with VendureConfig type among other exports', () => {
  29. const sourceText = `
  30. import { VendureConfig } from '@vendure/core';
  31. export const otherExport = 'value';
  32. export const config: VendureConfig = {
  33. authOptions: {
  34. tokenMethod: 'bearer'
  35. }
  36. };
  37. export const anotherExport = 123;
  38. `;
  39. const sourceFile = ts.createSourceFile('path/to/test.ts', sourceText, ts.ScriptTarget.Latest, true);
  40. const result = findConfigExport(sourceFile);
  41. expect(result).toBe('config');
  42. });
  43. });