1

i have a fs.readFileSync function that i need to mock using jest. i have tried the below code.

my original file which i want to test.

 const fs = require('fs'); const read_file = (path) => { try { const data = fs.readFileSync(path, 'utf8'); return data; } catch (err) { console.error('Error in read_file', err); throw err; } }; const getSecret = secretName => { try { return read_file(`/etc/secrets/${secretName}.txt`); } catch (err){ throw err; } }; const secretConfig = { kafka_keystore_password: getSecret('kafka_keystore_password') }; module.exports = secretConfig; 

here is my test case

const secret = require('./secret'); let fs = require('fs') jest.mock('fs'); describe('secret read files', () => { afterEach(jest.restoreAllMocks); it('should read secret from file', () => { //fs.readFileSync.mockReturnValue("randomPrivateKey"); fs.readFileSync.mockResolvedValue("randomPrivateKey") const secretMessage = secret.kafka_keystore_password; //expect(fs.readFileSync).toHaveBeenCalled(); expect(secretMessage).toEqual('randomPrivateKey'); }) }) describe('getSecretsFromFile', () => { const secret = 'secret'; const secrets = { mySecret: secret }; afterEach(jest.restoreAllMocks); it('returns secrets if file is present', () => { fs.existsSync.mockReturnValue(true); fs.readFileSync.mockReturnValue('randomPrivateKey'); const secretMessage = secret.kafka_keystore_password; expect(secretMessage).toEqual('randomPrivateKey'); }); }); 

and i get the following error.

FAIL src/config/secret.test.js ● secret read files › should read secret from file

expect(received).toEqual(expected) // deep equality Expected: "randomPrivateKey" Received: undefined 15 | const secretMessage = secret.kafka_keystore_password; 16 | //expect(fs.readFileSync).toHaveBeenCalled(); > 17 | expect(secretMessage).toEqual('randomPrivateKey'); | ^ 18 | 19 | }) 20 | at Object.<anonymous> (src/config/secret.test.js:17:27) 

● getSecretsFromFile › returns secrets if file is present

expect(received).toEqual(expected) // deep equality Expected: "randomPrivateKey" Received: undefined 32 | 33 | const secretMessage = secret.kafka_keystore_password; > 34 | expect(secretMessage).toEqual('randomPrivateKey'); | ^ 35 | }); 36 | }); 37 | at Object.<anonymous> (src/config/secret.test.js:34:29) 

help me fix this .

1
  • If my answer doesn't solve your problem please add a comment below it. If it does then please accept my answer. Happy to help but I might require further information. Commented Sep 16, 2021 at 7:44

1 Answer 1

1

try:

const fs = { readFileSync: jest.fn(() => ({ message: 'Test'})) } 

to mock the fs readFileSync message.

But I don't see how this test is ever going to pass because you're returning value "Test"

but checking for randomPrivateKey

expect(secretMessage).toEqual('randomPrivateKey'); 

Without seeing what's going on inside the code it's difficult to say but I am assuming you might want to change that line to:

expect(secretMessage).toEqual('test'); 

As test is the value your mocking alternatively return randomPrivateKey from the mock.

I'm having to make a load of assumptions here because I don't know-how secret.kafka_keystore_password is calculated. I'm assuming it just returns the whatever the fs.readFileSync returns.7

Sign up to request clarification or add additional context in comments.

5 Comments

added more details about my original file, also i have fixed my testing condition . now even after trying your suggestion i am getting "Expected: "randomPrivateKey" Received: undefined" error
but it looks like you're still using? fs.readFileSync.mockReturnValue('randomPrivateKey'); If you haven't done so can you please update ALL of the code examples to reflect your implementation of my suggestion?
why is that a problem i am expecting what i mocked only ?
look at my answer again and see how I am mocking using jest.fn(()=>{}) it's how I use jest and it works for me.
can you post full code to get accepted answer, its not working for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.