Skip to content

Commit 64fd08a

Browse files
committed
update transform unit test with mocks
1 parent 7347e37 commit 64fd08a

File tree

1 file changed

+88
-24
lines changed

1 file changed

+88
-24
lines changed
Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,112 @@
1-
import path from 'node:path'
1+
import { join } from 'node:path'
2+
import { run } from 'jscodeshift/src/Runner'
23
import prompts from 'prompts'
34
import { transform } from '../transform'
45

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+
718
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 })
1022

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()
1441

15-
it('Bad codemod user input', async () => {
1642
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__', {
1845
dry: true,
1946
silent: true,
2047
})
2148

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__'])
2569

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, {
2971
dry: true,
3072
silent: true,
3173
})
3274

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+
})
3686
})
3787

3888
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__', {
4197
dry: true,
4298
silent: true,
4399
})
44100

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+
})
48112
})

0 commit comments

Comments
 (0)