ast-utils.spec.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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', { timeout: 30_000 }, () => {
  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', { timeout: 30_000 }, () => {
  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(
  29. 'should find exported variable with VendureConfig type among other exports',
  30. { timeout: 30_000 },
  31. () => {
  32. const sourceText = `
  33. import { VendureConfig } from '@vendure/core';
  34. export const otherExport = 'value';
  35. export const config: VendureConfig = {
  36. authOptions: {
  37. tokenMethod: 'bearer'
  38. }
  39. };
  40. export const anotherExport = 123;
  41. `;
  42. const sourceFile = ts.createSourceFile(
  43. 'path/to/test.ts',
  44. sourceText,
  45. ts.ScriptTarget.Latest,
  46. true,
  47. );
  48. const result = findConfigExport(sourceFile);
  49. expect(result).toBe('config');
  50. },
  51. );
  52. });