Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 245143
Next
Alex Hales
  • 0
Alex HalesTeacher
Asked: August 17, 20222022-08-17T00:05:29+00:00 2022-08-17T00:05:29+00:00In: JavaScript, jestjs, ReactJS, Unit Testing, unit-testing

javascript – How to mock an exported const in jest

  • 0

[ad_1]

The most common scenario I needed was to mock a constant used by a class (in my case, a React component but it could be any ES6 class really).

@Luke’s answer worked great for this, it just took a minute to wrap my head around it so I thought I’d rephrase it into a more explicit example.

The key is that your constants need to be in a separate file that you import, so that this import itself can be stubbed/mocked by jest.

The following worked perfectly for me.

First, define your constants:

// src/my-component/constants.js

const MY_CONSTANT = 100;

export { MY_CONSTANT };

Next, we have the class that actually uses the constants:

// src/my-component/index.jsx

import { MY_CONSTANT } from './constants';

// This could be any class (e.g. a React component)
class MyComponent {
  constructor() {
    // Use the constant inside this class
    this.secret = MY_CONSTANT;
    console.log(`Current value is ${this.secret}`);
  }
}

export default MyComponent

Lastly, we have the tests. There’s 2 use cases we want to handle here:

  1. Mock the generate value of MY_CONSTANT for all tests inside this file
  2. Allow the ability for a specific test to further override the value of MY_CONSTANT for that single test

The first part is acheived by using jest.mock at the top of your test file.

The second is acheived by using jest.spyOn to further spy on the exported list of constants. It’s almost like a mock on top of a mock.

// test/components/my-component/index.js

import MyComponent from 'src/my-component';
import allConstants from 'src/my-component/constants';

jest.mock('src/my-component/constants', () => ({
  get MY_CONSTANT () {
    return 30;
  }
}));

it('mocks the value of MY_CONSTANT', () => {
  // Initialize the component, or in the case of React, render the component
  new MyComponent();

  // The above should cause the `console.log` line to print out the 
  // new mocked value of 30
});

it('mocks the value of MY_CONSTANT for this test,', () => {
  // Set up the spy. You can then use any jest mocking method
  // (e.g. `mockReturnValue()`) on it
  const mySpy = jest.spyOn(allConstants, 'MY_CONSTANT', 'get')
  mySpy.mockReturnValue(15);

  new MyComponent();

  // The above should cause the `console.log` line to print out the 
  // new mocked value of 15
});

[ad_2]

  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 3 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 5 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 4 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.