Skip to content

Commit 8cffd26

Browse files
Reza Karim ZadehReza Karim Zadeh
authored andcommitted
fix: resolve TypeScript errors in test mocks
1 parent 5c64173 commit 8cffd26

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import path from 'path'
2+
3+
describe('getPageExtensions()', () => {
4+
const originalCwd = process.cwd
5+
6+
afterEach(() => {
7+
jest.resetModules()
8+
jest.restoreAllMocks()
9+
})
10+
11+
afterAll(() => {
12+
process.cwd = originalCwd
13+
})
14+
15+
it('returns default extensions when next.config.js is not found', () => {
16+
jest.isolateModules(() => {
17+
const getPageExtensions =
18+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
19+
20+
expect(getPageExtensions()).toEqual(['tsx', 'ts', 'jsx', 'js'])
21+
})
22+
})
23+
24+
it('returns custom extensions from next.config.js', () => {
25+
jest.isolateModules(() => {
26+
const fs = require('fs')
27+
jest.spyOn(fs, 'existsSync').mockReturnValue(true as any)
28+
29+
jest.doMock(
30+
path.resolve(process.cwd(), 'next.config.js'),
31+
() => ({ pageExtensions: ['md', 'mdx'] }),
32+
{ virtual: true }
33+
)
34+
35+
const getPageExtensions =
36+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
37+
38+
expect(getPageExtensions()).toEqual(['md', 'mdx'])
39+
})
40+
})
41+
42+
it('falls back to default when pageExtensions is not an array', () => {
43+
jest.doMock(
44+
path.resolve(process.cwd(), 'next.config.js'),
45+
() => ({ pageExtensions: 'invalid' }),
46+
{ virtual: true }
47+
)
48+
49+
jest.isolateModules(() => {
50+
const getPageExtensions =
51+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
52+
53+
expect(getPageExtensions()).toEqual(['tsx', 'ts', 'jsx', 'js'])
54+
})
55+
})
56+
57+
it('removes leading dots from extensions', () => {
58+
jest.isolateModules(() => {
59+
const fs = require('fs')
60+
jest.spyOn(fs, 'existsSync').mockReturnValue(true as any)
61+
62+
jest.doMock(
63+
path.resolve(process.cwd(), 'next.config.js'),
64+
() => ({ pageExtensions: ['.tsx', '.ts'] }),
65+
{ virtual: true }
66+
)
67+
68+
const getPageExtensions =
69+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
70+
71+
expect(getPageExtensions()).toEqual(['tsx', 'ts'])
72+
})
73+
})
74+
75+
it('returns default when pageExtensions is empty array', () => {
76+
jest.doMock(
77+
path.resolve(process.cwd(), 'next.config.js'),
78+
() => ({ pageExtensions: [] }),
79+
{ virtual: true }
80+
)
81+
82+
jest.isolateModules(() => {
83+
const getPageExtensions =
84+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
85+
86+
expect(getPageExtensions()).toEqual(['tsx', 'ts', 'jsx', 'js'])
87+
})
88+
})
89+
90+
it('supports next.config.mjs with ES module export', () => {
91+
jest.isolateModules(() => {
92+
const fs = require('fs')
93+
jest.spyOn(fs, 'existsSync').mockImplementation(((filepath: unknown) => {
94+
if (String(filepath).endsWith('next.config.mjs')) return true
95+
return false
96+
}) as any)
97+
98+
jest.doMock(
99+
path.resolve(process.cwd(), 'next.config.mjs'),
100+
() => ({ default: { pageExtensions: ['page.tsx', 'page.ts'] } }),
101+
{ virtual: true }
102+
)
103+
104+
const getPageExtensions =
105+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
106+
107+
expect(getPageExtensions()).toEqual(['page.tsx', 'page.ts'])
108+
})
109+
})
110+
111+
it('handles custom extensions like .page.tsx and .mdx', () => {
112+
jest.isolateModules(() => {
113+
const fs = require('fs')
114+
jest.spyOn(fs, 'existsSync').mockReturnValue(true as any)
115+
116+
jest.doMock(
117+
path.resolve(process.cwd(), 'next.config.js'),
118+
() => ({ pageExtensions: ['page.tsx', 'page.ts', 'mdx'] }),
119+
{ virtual: true }
120+
)
121+
122+
const getPageExtensions =
123+
require('../../../../packages/eslint-plugin-next/src/utils/url').getPageExtensions
124+
125+
expect(getPageExtensions()).toEqual(['page.tsx', 'page.ts', 'mdx'])
126+
})
127+
})
128+
129+
it('memoizes the result on subsequent calls', () => {
130+
jest.isolateModules(() => {
131+
const {
132+
getPageExtensions,
133+
} = require('../../../../packages/eslint-plugin-next/src/utils/url')
134+
135+
const firstCall = getPageExtensions()
136+
const secondCall = getPageExtensions()
137+
138+
expect(firstCall).toBe(secondCall) // Same reference
139+
expect(firstCall).toEqual(['tsx', 'ts', 'jsx', 'js'])
140+
})
141+
})
142+
})

0 commit comments

Comments
 (0)