|
1 | | -import path from 'node:path' |
| 1 | +import { join } from 'node:path' |
| 2 | +import { run } from 'jscodeshift/src/Runner' |
2 | 3 | import prompts from 'prompts' |
3 | 4 | import { transform } from '../transform' |
4 | 5 |
|
5 | | -describe('interative mode', () => { |
6 | | - it('Without user input', async () => { |
| 6 | +jest.mock('jscodeshift/src/Runner', () => ({ |
| 7 | + run: jest.fn(), |
| 8 | +})) |
| 9 | + |
| 10 | +describe('interactive mode', () => { |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks() |
| 13 | + }) |
| 14 | + |
| 15 | + it('runs without codemodName and source params provided', async () => { |
| 16 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 17 | + |
7 | 18 | prompts.inject(['magic-redirect']) |
8 | | - prompts.inject([path.resolve(process.cwd(), './transforms/__testfixtures__')]) |
9 | | - const res = await transform(undefined, undefined, { dry: true, silent: true }) |
| 19 | + prompts.inject(['./transforms/__testfixtures__']) |
| 20 | + |
| 21 | + await transform(undefined, undefined, { dry: true, silent: true }) |
10 | 22 |
|
11 | | - expect(res.ok).toBe(2) |
12 | | - expect(res.error).toBe(0) |
13 | | - }, 10000) |
| 23 | + expect(spyOnConsole).not.toHaveBeenCalled() |
| 24 | + expect(run).toHaveBeenCalledTimes(1) |
| 25 | + expect(run).toHaveBeenCalledWith( |
| 26 | + join(__dirname, '../../', 'transforms/magic-redirect.js'), |
| 27 | + ['./transforms/__testfixtures__'], |
| 28 | + { |
| 29 | + babel: false, |
| 30 | + dry: true, |
| 31 | + extensions: 'cts,mts,ts,js,mjs,cjs', |
| 32 | + ignorePattern: '**/node_modules/**', |
| 33 | + silent: true, |
| 34 | + verbose: 0, |
| 35 | + }, |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('runs properly on incorrect user input', async () => { |
| 40 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
14 | 41 |
|
15 | | - it('Bad codemod user input', async () => { |
16 | 42 | prompts.inject(['magic-redirect']) |
17 | | - const res = await transform('bad-codemod', path.resolve(process.cwd(), './transforms/__testfixtures__'), { |
| 43 | + |
| 44 | + await transform('bad-codemod', './transforms/__testfixtures__', { |
18 | 45 | dry: true, |
19 | 46 | silent: true, |
20 | 47 | }) |
21 | 48 |
|
22 | | - expect(res.ok).toBe(2) |
23 | | - expect(res.error).toBe(0) |
24 | | - }, 10000) |
| 49 | + expect(spyOnConsole).not.toHaveBeenCalled() |
| 50 | + expect(run).toHaveBeenCalledTimes(1) |
| 51 | + expect(run).toHaveBeenCalledWith( |
| 52 | + join(__dirname, '../../', 'transforms/magic-redirect.js'), |
| 53 | + ['./transforms/__testfixtures__'], |
| 54 | + { |
| 55 | + babel: false, |
| 56 | + dry: true, |
| 57 | + extensions: 'cts,mts,ts,js,mjs,cjs', |
| 58 | + ignorePattern: '**/node_modules/**', |
| 59 | + silent: true, |
| 60 | + verbose: 0, |
| 61 | + }, |
| 62 | + ) |
| 63 | + }) |
| 64 | + |
| 65 | + it('runs with codemodName and without source param provided', async () => { |
| 66 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 67 | + |
| 68 | + prompts.inject(['__testfixtures__']) |
25 | 69 |
|
26 | | - it("Don't source user input", async () => { |
27 | | - prompts.inject([path.resolve(process.cwd(), './transforms/__testfixtures__')]) |
28 | | - const res = await transform('magic-redirect', undefined, { |
| 70 | + await transform('magic-redirect', undefined, { |
29 | 71 | dry: true, |
30 | 72 | silent: true, |
31 | 73 | }) |
32 | 74 |
|
33 | | - expect(res.ok).toBe(2) |
34 | | - expect(res.error).toBe(0) |
35 | | - }, 10000) |
| 75 | + expect(spyOnConsole).not.toHaveBeenCalled() |
| 76 | + expect(run).toHaveBeenCalledTimes(1) |
| 77 | + expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], { |
| 78 | + babel: false, |
| 79 | + dry: true, |
| 80 | + extensions: 'cts,mts,ts,js,mjs,cjs', |
| 81 | + ignorePattern: '**/node_modules/**', |
| 82 | + silent: true, |
| 83 | + verbose: 0, |
| 84 | + }) |
| 85 | + }) |
36 | 86 | }) |
37 | 87 |
|
38 | 88 | describe('Non-Interactive Mode', () => { |
39 | | - it('Transform code with codemod and user input source', async () => { |
40 | | - const res = await transform('magic-redirect', path.resolve(process.cwd(), './transforms/__testfixtures__'), { |
| 89 | + beforeEach(() => { |
| 90 | + jest.clearAllMocks() |
| 91 | + }) |
| 92 | + |
| 93 | + it('Transforms code with codemodName and source params provided', async () => { |
| 94 | + const spyOnConsole = jest.spyOn(console, 'log').mockImplementation() |
| 95 | + |
| 96 | + await transform('magic-redirect', '__testfixtures__', { |
41 | 97 | dry: true, |
42 | 98 | silent: true, |
43 | 99 | }) |
44 | 100 |
|
45 | | - expect(res.ok).toBe(2) |
46 | | - expect(res.error).toBe(0) |
47 | | - }, 10000) |
| 101 | + expect(spyOnConsole).not.toHaveBeenCalled() |
| 102 | + expect(run).toHaveBeenCalledTimes(1) |
| 103 | + expect(run).toHaveBeenCalledWith(join(__dirname, '../../', 'transforms/magic-redirect.js'), ['__testfixtures__'], { |
| 104 | + babel: false, |
| 105 | + dry: true, |
| 106 | + extensions: 'cts,mts,ts,js,mjs,cjs', |
| 107 | + ignorePattern: '**/node_modules/**', |
| 108 | + silent: true, |
| 109 | + verbose: 0, |
| 110 | + }) |
| 111 | + }) |
48 | 112 | }) |
0 commit comments