Skip to content

Commit f353e4b

Browse files
committed
fs.spec.js
1 parent b635524 commit f353e4b

1 file changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
4+
jest.mock('fs')
5+
jest.mock('util', () => ({
6+
promisify: jest.fn(fn => fn),
7+
}))
8+
9+
describe('access()', () => {
10+
it('Calls fs.access', async () => {
11+
const { access } = jest.requireActual('./fs')
12+
await access()
13+
14+
expect(fs.access).toHaveBeenCalledTimes(1)
15+
})
16+
})
17+
18+
describe('lstat()', () => {
19+
it('Calls fs.lstat', async () => {
20+
const { lstat } = jest.requireActual('./fs')
21+
22+
await lstat()
23+
24+
expect(fs.lstat).toHaveBeenCalledTimes(1)
25+
})
26+
})
27+
28+
describe('mkdir()', () => {
29+
it('Calls fs.mkdir', async () => {
30+
const { mkdir } = jest.requireActual('./fs')
31+
32+
await mkdir()
33+
34+
expect(fs.mkdir).toHaveBeenCalledTimes(1)
35+
})
36+
})
37+
38+
describe('readDir()', () => {
39+
it('Calls fs.readdir', async () => {
40+
const { readDir } = jest.requireActual('./fs')
41+
42+
await readDir()
43+
44+
expect(fs.readdir).toHaveBeenCalledTimes(1)
45+
})
46+
})
47+
48+
describe('readFile()', () => {
49+
it('Calls fs.readFile', async () => {
50+
const { readFile } = jest.requireActual('./fs')
51+
52+
await readFile()
53+
54+
expect(fs.readFile).toHaveBeenCalledTimes(1)
55+
})
56+
})
57+
58+
describe('stat()', () => {
59+
it('Calls fs.stat', async () => {
60+
const { stat } = jest.requireActual('./fs')
61+
62+
await stat()
63+
64+
expect(fs.stat).toHaveBeenCalledTimes(1)
65+
})
66+
})
67+
68+
describe('writeFile()', () => {
69+
it('Calls fs.writeFile', async () => {
70+
const { writeFile } = jest.requireActual('./fs')
71+
72+
await writeFile()
73+
74+
expect(fs.writeFile).toHaveBeenCalledTimes(1)
75+
})
76+
})
77+
78+
describe('findUpwardsFile()', () => {
79+
const directory = '/home/test'
80+
const filename = 'test.file'
81+
82+
beforeEach(() => {
83+
jest.clearAllMocks()
84+
})
85+
86+
it('looks for file in the supplied directory', async () => {
87+
const { findUpwardsFile } = jest.requireActual('./fs')
88+
89+
fs.access.mockReturnValue()
90+
91+
const targetFile = await findUpwardsFile(filename, directory)
92+
93+
expect(fs.access).toHaveBeenCalledTimes(1)
94+
expect(targetFile).toBe(path.normalize('/home/test/test.file'))
95+
})
96+
it('defaults to process.cwd()', async () => {
97+
const { findUpwardsFile } = jest.requireActual('./fs')
98+
99+
const cwdSpy = jest.spyOn(process, 'cwd')
100+
cwdSpy.mockReturnValue(directory)
101+
102+
fs.access.mockReturnValue()
103+
104+
await findUpwardsFile(filename)
105+
106+
expect(cwdSpy).toHaveBeenCalledTimes(1)
107+
108+
cwdSpy.mockRestore()
109+
})
110+
it('returns an absolute path when matching file is found', async () => {
111+
const { findUpwardsFile } = jest.requireActual('./fs')
112+
113+
fs.access.mockResolvedValue()
114+
115+
const targetFile = await findUpwardsFile(filename, directory)
116+
117+
expect(targetFile).toBe(path.normalize('/home/test/test.file'))
118+
})
119+
it('returns false when file cannot be found', async () => {
120+
const { findUpwardsFile } = jest.requireActual('./fs')
121+
122+
const enoentError = Error()
123+
enoentError.code = 'ENOENT'
124+
fs.access.mockRejectedValue(enoentError)
125+
126+
const targetFile = await findUpwardsFile(filename, directory)
127+
128+
expect(targetFile).toBeFalse()
129+
})
130+
it('traverses the directory tree', async () => {
131+
const { findUpwardsFile } = jest.requireActual('./fs')
132+
133+
const enoentError = Error()
134+
enoentError.code = 'ENOENT'
135+
136+
fs.access.mockRejectedValueOnce(enoentError)
137+
fs.access.mockResolvedValueOnce()
138+
139+
await findUpwardsFile(filename, directory)
140+
141+
expect(fs.access).toHaveBeenCalledTimes(2)
142+
})
143+
it('propagates error when fs access throws an error different than ENOENT', () => {
144+
const { findUpwardsFile } = jest.requireActual('./fs')
145+
146+
const error = Error()
147+
fs.access.mockRejectedValue(error)
148+
149+
expect(findUpwardsFile(filename, directory))
150+
.rejects
151+
.toMatchObject(error)
152+
})
153+
})
154+
describe('listDirectoryFiles()', () => {
155+
it('lists files in the supplied directory', async () => {
156+
const { listDirectoryFiles } = jest.requireActual('./fs')
157+
const { Dirent, constants } = jest.requireActual('fs')
158+
const { UV_DIRENT_FILE } = constants
159+
160+
const file = new Dirent('file', UV_DIRENT_FILE)
161+
162+
fs.readdir.mockResolvedValue([ file ])
163+
164+
const files = await listDirectoryFiles()
165+
166+
expect(files).toMatchObject([ file.name ])
167+
})
168+
it('ignores subdirectories in the supplied directory', async () => {
169+
const { listDirectoryFiles } = jest.requireActual('./fs')
170+
const { Dirent, constants } = jest.requireActual('fs')
171+
const { UV_DIRENT_DIR } = constants
172+
173+
const directory = new Dirent('directory', UV_DIRENT_DIR)
174+
fs.readdir.mockResolvedValue([ directory ])
175+
176+
const files = await listDirectoryFiles()
177+
178+
expect(files).toMatchObject([])
179+
})
180+
})

0 commit comments

Comments
 (0)