最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access local variable in jest - Stack Overflow

programmeradmin5浏览0评论

Currently, I am trying to write a test. I want to use a local variable in the test, but unfortunately, I don't manage to mock or use it. The variable and the function which uses it, aren't exported.

How do I access the local variable authToken in utils.test.js? So I can change authToken to another value.

I've tried to use rewire (), but this didn't work. authToken is still undefined.

utils.js

const { auth } = require('./auth.js');

let authToken = undefined;

const checkIfTokenIsValid = async () => {
    if (authToken) {
        authToken = await auth();
    }
};

module.exports = {
    // some other functions
}

utils.test.js

const _rewire = require('rewire');
const utils = _rewire('../../lib/resources/utils');
utils.__set__('authToken', () => true);


describe('api auth', () => {
    // some tests
});

Currently, I am trying to write a test. I want to use a local variable in the test, but unfortunately, I don't manage to mock or use it. The variable and the function which uses it, aren't exported.

How do I access the local variable authToken in utils.test.js? So I can change authToken to another value.

I've tried to use rewire (https://www.npmjs./package/rewire), but this didn't work. authToken is still undefined.

utils.js

const { auth } = require('./auth.js');

let authToken = undefined;

const checkIfTokenIsValid = async () => {
    if (authToken) {
        authToken = await auth();
    }
};

module.exports = {
    // some other functions
}

utils.test.js

const _rewire = require('rewire');
const utils = _rewire('../../lib/resources/utils');
utils.__set__('authToken', () => true);


describe('api auth', () => {
    // some tests
});
Share Improve this question edited Feb 3, 2020 at 6:37 Lin Du 103k136 gold badges334 silver badges566 bronze badges asked Aug 21, 2019 at 14:18 f4zrf4zr 701 gold badge1 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Here is the unit test solution using rewire module.

utils.js:

let { auth } = require('./auth');

let authToken = undefined;

const checkIfTokenIsValid = async () => {
  if (authToken) {
    authToken = await auth();
  }
};

module.exports = {
  checkIfTokenIsValid,
};

auth.js:

async function auth() {
  return 'real auth response';
}
module.exports = { auth };

utils.spec.js:

const rewire = require('rewire');
const utils = rewire('./utils');

describe('utils', () => {
  describe('#checkIfTokenIsValid', () => {
    test('should not check token', async () => {
      const authMock = jest.fn();
      utils.__set__({
        auth: authMock,
        authToken: undefined,
      });
      await utils.checkIfTokenIsValid();
      expect(authMock).not.toBeCalled();
    });

    test('should check token', async () => {
      const authMock = jest.fn().mockResolvedValueOnce('abc');
      utils.__set__({
        auth: authMock,
        authToken: 123,
      });
      await utils.checkIfTokenIsValid();
      expect(authMock).toBeCalledTimes(1);
      expect(utils.__get__('authToken')).toBe('abc');
    });
  });
});

Unit test results:

 PASS  src/stackoverflow/57593767-todo/utils.spec.js
  utils
    #checkIfTokenIsValid
      ✓ should not check token (7ms)
      ✓ should check token (2ms)

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        4.468s

Source code: https://github./mrdulin/jest-codelab/tree/master/src/stackoverflow/57593767

发布评论

评论列表(0)

  1. 暂无评论