2

My question is about how to mock jest function importing from another file as default.

What I want to test is that component using function to enabled Feature (Features.js)

I mocked this function using jest.fn() and tried to change value using mockReturnValueOnce

It looks like below.

mocks/features.js

export default function isFeatureEnabled (featureName) {
  return true // want to test both true/false cases 
}

test.spec.js

jest.mock('__mocks__/features.js', () => ({
  isFeatureEnabled: jest.fn()
}))

describe('isFeatureEnabled', () => {
  it('isFeatureEnabled=[true]', () => {
    isFeatureEnabled.mockReturnValueOnce(true)
   // some tests
  })
  it('isFeatureEnabled=[false]', () => {
    isFeatureEnabled.mockReturnValueOnce(false)
   // some tests
  })
})

When I run the test, I got an error says mockReturnValueOnce is not a function. This stackoverflow question inspired me to implement in this way, however still I cannot figure it out how to make it working.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
aaayumi
  • 1,224
  • 8
  • 27
  • 47

2 Answers2

4

You're close.

Here is a simple working example that demonstrates what you are trying to do:


features.js

export default function isFeatureEnabled (featureName) {
  // this is the actual implementation...
  return true  // ...but for the example just return true
}

__mock__/features.js

export default jest.fn(() => true);  // default export is mock that returns true

code.js

import isFeatureEnabled from './features';

export const funcToTest = () => isFeatureEnabled() ? 'enabled' : 'not enabled';

code.test.js

import { funcToTest } from './code';
import isFeatureEnabled from './features';

jest.mock('./features');  // use the manual mock

describe('funcToTest', () => {
  it('isFeatureEnabled=[true]', () => {
    isFeatureEnabled.mockReturnValueOnce(true);
    expect(funcToTest()).toBe('enabled');  // Success!
  })
  it('isFeatureEnabled=[false]', () => {
    isFeatureEnabled.mockReturnValueOnce(false);
   expect(funcToTest()).toBe('not enabled');  // Success!
  })
})
Brian Adams
  • 43,011
  • 9
  • 113
  • 111
  • 1
    thank you so much! I didn't expect that I should use `export default jest.fn(() => true)` in the mock. – aaayumi Apr 01 '19 at 12:34
1

I think you should test result of you function. Import isFeatureEnabled and test what it returns. I don't get it why you use mock.

import isFeatureEnabled from './isFeatureEnabled'

describe('Testing features', () => {

    it('isFeatureEnabled should return true if "add" feature is enabled', () => {
        const feature = 'add'
        const result = isFeatureEnabled(feature)
        expect(result).toEqual(true)
      })
});
miuosh
  • 836
  • 3
  • 12
  • 35
  • thanks, sorry for my lack of explanation, what I want to test is not isFeatureEnabled function but other function relying on isFeaturedEnabled function. – aaayumi Mar 29 '19 at 15:02
  • Ok. So your mock is manual mock in `__mocks__` folder. In `test.spec.js` simply add `jest.mock()` – miuosh Mar 29 '19 at 15:09
  • Please provide funtion you want to test – miuosh Mar 29 '19 at 15:23