npm-plugin.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { rm } from 'node:fs/promises';
  2. import { join } from 'node:path';
  3. import tsconfigPaths from 'tsconfig-paths';
  4. import { describe, expect, it } from 'vitest';
  5. import { compile } from '../utils/compiler.js';
  6. import { debugLogger, noopLogger } from '../utils/logger.js';
  7. describe('detecting plugins in npm packages', () => {
  8. it(
  9. 'should detect plugins in npm packages',
  10. async () => {
  11. const tempDir = join(__dirname, './__temp/npm-plugin');
  12. await rm(tempDir, { recursive: true, force: true });
  13. const fakeNodeModules = join(__dirname, 'fixtures-npm-plugin', 'fake_node_modules');
  14. // For this test to work, we need to use tsconfigPaths to point
  15. // the `test-plugin` package to the `fake_node_modules` directory.
  16. // This is because the `test-plugin` package is not installed in
  17. // the `node_modules` directory, but in the `fake_node_modules`
  18. // directory.
  19. tsconfigPaths.register({
  20. baseUrl: fakeNodeModules,
  21. paths: {
  22. 'test-plugin': [join(fakeNodeModules, 'test-plugin')],
  23. },
  24. });
  25. const result = await compile({
  26. outputPath: tempDir,
  27. vendureConfigPath: join(__dirname, 'fixtures-npm-plugin', 'vendure-config.ts'),
  28. logger: process.env.LOG ? debugLogger : noopLogger,
  29. pluginPackageScanner: {
  30. nodeModulesRoot: fakeNodeModules,
  31. },
  32. });
  33. expect(result.pluginInfo).toHaveLength(1);
  34. expect(result.pluginInfo[0].name).toBe('TestPlugin');
  35. expect(result.pluginInfo[0].dashboardEntryPath).toBe('./dashboard/index.tsx');
  36. expect(result.pluginInfo[0].sourcePluginPath).toBeUndefined();
  37. expect(result.pluginInfo[0].pluginPath).toBe(join(fakeNodeModules, 'test-plugin', 'index.js'));
  38. },
  39. { timeout: 10_000 },
  40. );
  41. });