|
| 1 | +jest.mock('live-plugin-manager'); |
1 | 2 | jest.mock('jscodeshift/src/Runner', () => ({
|
2 | 3 | run: jest.fn().mockImplementation(() => Promise.resolve()),
|
3 | 4 | }));
|
4 | 5 |
|
5 |
| -jest.mock('live-plugin-manager', () => ({ |
6 |
| - PluginManager: () => ({ |
7 |
| - install: () => Promise.resolve(undefined), |
8 |
| - require: (codemodName: string) => ({ |
9 |
| - default: { |
10 |
| - transforms: { |
11 |
| - '18.0.0': `${codemodName}/path/to/18.js`, |
12 |
| - '19.0.0': `${codemodName}/path/to/19.js`, |
13 |
| - '20.0.0': `${codemodName}/path/to/20.js`, |
14 |
| - }, |
15 |
| - presets: { |
16 |
| - 'update-formatting': `${codemodName}/path/to/update-formatting.js`, |
17 |
| - 'update-imports': `${codemodName}/path/to/update-imports.js`, |
18 |
| - }, |
19 |
| - }, |
20 |
| - }), |
21 |
| - uninstallAll: () => Promise.resolve(), |
22 |
| - }), |
23 |
| -})); |
24 |
| - |
25 | 6 | // @ts-ignore
|
26 | 7 | import * as jscodeshift from 'jscodeshift/src/Runner';
|
27 |
| - |
| 8 | +import { PluginManager } from 'live-plugin-manager'; |
28 | 9 | import main from './main';
|
29 | 10 |
|
30 | 11 | const mockPath = 'src/pages/home-page/';
|
31 | 12 |
|
32 | 13 | describe('main', () => {
|
33 | 14 | beforeEach(() => {
|
| 15 | + (PluginManager as jest.Mock).mockReturnValue({ |
| 16 | + install: () => Promise.resolve(undefined), |
| 17 | + require: (codemodName: string) => ({ |
| 18 | + default: { |
| 19 | + transforms: { |
| 20 | + '18.0.0': `${codemodName}/path/to/18.js`, |
| 21 | + '19.0.0': `${codemodName}/path/to/19.js`, |
| 22 | + '20.0.0': `${codemodName}/path/to/20.js`, |
| 23 | + }, |
| 24 | + presets: { |
| 25 | + 'update-formatting': `${codemodName}/path/to/update-formatting.js`, |
| 26 | + 'update-imports': `${codemodName}/path/to/update-imports.js`, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }), |
| 30 | + uninstallAll: () => Promise.resolve(), |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(() => { |
34 | 35 | jest.resetAllMocks();
|
35 | 36 | });
|
36 | 37 |
|
@@ -327,6 +328,55 @@ describe('main', () => {
|
327 | 328 | expect.any(Object),
|
328 | 329 | );
|
329 | 330 | });
|
| 331 | + |
| 332 | + it('should not throw when attempting to run transform that is not present in config', async () => { |
| 333 | + (PluginManager as jest.Mock).mockReturnValue({ |
| 334 | + install: () => Promise.resolve(undefined), |
| 335 | + require: (codemodName: string) => ({ |
| 336 | + default: { |
| 337 | + presets: { |
| 338 | + 'update-formatting': `${codemodName}/path/to/update-formatting.js`, |
| 339 | + }, |
| 340 | + }, |
| 341 | + }), |
| 342 | + uninstallAll: () => Promise.resolve(), |
| 343 | + }); |
| 344 | + |
| 345 | + await expect( |
| 346 | + main([mockPath], { |
| 347 | + packages: 'mylib@20.0.0', |
| 348 | + parser: 'babel', |
| 349 | + extensions: 'js', |
| 350 | + }), |
| 351 | + ).rejects.toEqual( |
| 352 | + Error( |
| 353 | + 'Invalid version provided to the --packages flag. Unable to resolve version "20.0.0" for package "mylib"', |
| 354 | + ), |
| 355 | + ); |
| 356 | + }); |
| 357 | + |
| 358 | + it('should not throw when transform are not present in the config', async () => { |
| 359 | + (PluginManager as jest.Mock).mockReturnValue({ |
| 360 | + install: () => Promise.resolve(undefined), |
| 361 | + // @ts-ignore |
| 362 | + require: (codemodName: string) => ({ |
| 363 | + default: { |
| 364 | + presets: { |
| 365 | + 'update-formatting': `${codemodName}/path/to/update-formatting.js`, |
| 366 | + }, |
| 367 | + }, |
| 368 | + }), |
| 369 | + uninstallAll: () => Promise.resolve(), |
| 370 | + }); |
| 371 | + |
| 372 | + await expect( |
| 373 | + main([mockPath], { |
| 374 | + packages: 'mylib#update-formatting', |
| 375 | + parser: 'babel', |
| 376 | + extensions: 'js', |
| 377 | + }), |
| 378 | + ).resolves.not.toThrow(); |
| 379 | + }); |
330 | 380 | });
|
331 | 381 |
|
332 | 382 | describe('when running presets with the -p flag', () => {
|
@@ -416,5 +466,55 @@ describe('main', () => {
|
416 | 466 | );
|
417 | 467 | }
|
418 | 468 | });
|
| 469 | + |
| 470 | + it('should not throw when attempting to run preset that is not present in config', async () => { |
| 471 | + (PluginManager as jest.Mock).mockReturnValue({ |
| 472 | + install: () => Promise.resolve(undefined), |
| 473 | + // @ts-ignore |
| 474 | + require: (codemodName: string) => ({ |
| 475 | + default: { |
| 476 | + transforms: { |
| 477 | + '20.0.0': `${codemodName}/path/to/20.js`, |
| 478 | + }, |
| 479 | + }, |
| 480 | + }), |
| 481 | + uninstallAll: () => Promise.resolve(), |
| 482 | + }); |
| 483 | + |
| 484 | + await expect( |
| 485 | + main([mockPath], { |
| 486 | + packages: 'mylib#foo-bar', |
| 487 | + parser: 'babel', |
| 488 | + extensions: 'js', |
| 489 | + }), |
| 490 | + ).rejects.toEqual( |
| 491 | + Error( |
| 492 | + 'Invalid preset provided to the --packages flag. Unable to resolve preset "foo-bar" for package "mylib"', |
| 493 | + ), |
| 494 | + ); |
| 495 | + }); |
| 496 | + |
| 497 | + it('should not throw when presets are not present in the config', async () => { |
| 498 | + (PluginManager as jest.Mock).mockReturnValue({ |
| 499 | + install: () => Promise.resolve(undefined), |
| 500 | + // @ts-ignore |
| 501 | + require: (codemodName: string) => ({ |
| 502 | + default: { |
| 503 | + transforms: { |
| 504 | + '20.0.0': `${codemodName}/path/to/20.js`, |
| 505 | + }, |
| 506 | + }, |
| 507 | + }), |
| 508 | + uninstallAll: () => Promise.resolve(), |
| 509 | + }); |
| 510 | + |
| 511 | + await expect( |
| 512 | + main([mockPath], { |
| 513 | + packages: 'mylib@20.0.0', |
| 514 | + parser: 'babel', |
| 515 | + extensions: 'js', |
| 516 | + }), |
| 517 | + ).resolves.not.toThrow(); |
| 518 | + }); |
419 | 519 | });
|
420 | 520 | });
|
0 commit comments