Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overcome jest "Cannot access before initialization" problem?

settings.js

export default {
  web: {
    a: 1
  },
  mobile: {
    b: 2
  }
};

getSetting.js

import settings from "./settings";

export const getSetting = platform => {
  return settings[platform];
};

getSettings.test.js

import { getSetting } from "./getSetting";

const TEST_SETTINGS = { c: 3 };

jest.mock("./settings", () => {
  return {
    test: TEST_SETTINGS
  };
});

test("getSetting", () => {
  expect(getSetting("test")).toEqual(TEST_SETTINGS);
});

Error

ReferenceError: Cannot access 'TEST_SETTINGS' before initialization

I believe this has something to do with hoisting. Is there a way to overcome this issue? Does jest provide any other means to achieve this?

I don't want to do this. This is not good when the mock data is large and used in multiple tests.

jest.mock("./settings", () => {
  return {
    test: { c: 3 }
  };
});

expect(getSetting("test")).toEqual({ c: 3 });
like image 941
lch Avatar asked May 16 '20 21:05

lch


2 Answers

jest.mock is automatically hoisted, this results in evaluating mocked module before TEST_SETTINGS is declared.

Also, this results in ./settings being mocked with test named export, while it's expected to have default export.

It shouldn't use temporary variable, the value is available when it's being imported:

import settings from "./settings";

jest.mock("./settings", () => {
  return { default: {
    test: { c: 3 }
  } };
});

...

expect(getSetting("test")).toBe(settings.test);
like image 167
Estus Flask Avatar answered Nov 14 '22 16:11

Estus Flask


Or, use Dynamic Imports import().

E.g.

const TEST_SETTINGS = { c: 3 };

jest.mock('./settings', () => {
  return {
    test: TEST_SETTINGS,
  };
});

test('getSetting', async () => {
  const { getSetting } = await import('./getSetting');
  expect(getSetting('test')).toEqual(TEST_SETTINGS);
});

test result:

 PASS  examples/61843762/getSettings.test.js
  ✓ getSetting (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        3.25 s, estimated 4 s

jestjs version: "jest": "^26.6.3"

like image 31
slideshowp2 Avatar answered Nov 14 '22 17:11

slideshowp2