Skip to content

Commit da43fc6

Browse files
committed
mock partially
1 parent 5954bd1 commit da43fc6

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

test/commands/console/publickey/upload.test.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,27 @@ jest.mock('@adobe/aio-cli-lib-console', () => ({
5656
init: jest.fn().mockResolvedValue(mockConsoleCLIInstance),
5757
cleanStdOut: jest.fn()
5858
}))
59-
jest.spyOn(fs, 'statSync').mockImplementation(file => {
60-
if (file === 'certificate_pub.crt') {
61-
return { isFile: () => { return true } }
62-
} else if (file === 'a_directory') {
63-
return { isFile: () => { return false } }
64-
} else {
65-
throw new Error('file does not exist')
66-
}
67-
})
68-
jest.spyOn(fs, 'readFileSync').mockImplementation(file => {
69-
if (file === 'certificate_pub.crt') {
70-
return Buffer.from('a PEM encoded cert')
71-
} else {
72-
throw new Error('dOEs NoT c()MUPt3')
73-
}
74-
})
59+
60+
const realStatSync = fs.statSync
61+
const realReadFileSync = fs.readFileSync
62+
const mockStatSync = jest.spyOn(fs, 'statSync')
63+
const mockReadFileSync = jest.spyOn(fs, 'readFileSync')
64+
65+
/** @private */
66+
function setDefaultMockFs () {
67+
mockStatSync.mockReset().mockImplementation(file => {
68+
const fileStr = String(file)
69+
if (fileStr === 'certificate_pub.crt') return { isFile: () => true }
70+
if (fileStr === 'a_directory') return { isFile: () => false }
71+
return realStatSync(file)
72+
})
73+
74+
mockReadFileSync.mockReset().mockImplementation(file => {
75+
const fileStr = String(file)
76+
if (fileStr === 'certificate_pub.crt') return Buffer.from('a PEM encoded cert')
77+
return realReadFileSync(file)
78+
})
79+
}
7580

7681
const UploadAndBindCommand = require('../../../../src/commands/console/publickey/upload')
7782

@@ -120,6 +125,7 @@ describe('console:publickey:upload', () => {
120125
beforeEach(() => {
121126
setDefaultMockConsoleCLI()
122127
setDefaultMockConfigGet()
128+
setDefaultMockFs()
123129
config.set.mockReset()
124130
command = new UploadAndBindCommand([])
125131
})

0 commit comments

Comments
 (0)