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

javascript - How to use a Global Variable in Jest - Stack Overflow

programmeradmin0浏览0评论

I've got my Yarn package.json set up like this, where I create a global variable called localPath.

{
  "jest": {
    "globals": {
      "localPath": "Users/alex/Git/mytodolist"
    }
  }
}

Then, in one of my spec tests, I run

console.log(localPath)

but get this error.

ReferenceError: localPath is not defined

      5 | 
    > 6 |   console.log(localPath)

Does anyone know how to call the global variable you set up? I can only find articles on creating the variable, but not on how to call it.

Source:

Edit: Thanks to @slideshowp2 for the correct answer below. Turns out I didn't need to use a global variable in the end, as you can dynamically grab the execution path at run time. However, this will certainly be useful in the future.

beforeAll(async () => {
  await page.goto('file:///'+process.cwd()+'/index.html')
})

I've got my Yarn package.json set up like this, where I create a global variable called localPath.

{
  "jest": {
    "globals": {
      "localPath": "Users/alex/Git/mytodolist"
    }
  }
}

Then, in one of my spec tests, I run

console.log(localPath)

but get this error.

ReferenceError: localPath is not defined

      5 | 
    > 6 |   console.log(localPath)

Does anyone know how to call the global variable you set up? I can only find articles on creating the variable, but not on how to call it.

Source: https://jestjs.io/docs/en/configuration#globals-object

Edit: Thanks to @slideshowp2 for the correct answer below. Turns out I didn't need to use a global variable in the end, as you can dynamically grab the execution path at run time. However, this will certainly be useful in the future.

beforeAll(async () => {
  await page.goto('file:///'+process.cwd()+'/index.html')
})
Share Improve this question edited Dec 29, 2019 at 17:05 alex asked Dec 20, 2019 at 13:36 alexalex 1,0894 gold badges18 silver badges34 bronze badges 5
  • Did you try the same in jest.config.js? – Matvii Hodovaniuk Commented Dec 22, 2019 at 20:49
  • Could you try global.localPath in your test? – Matvii Hodovaniuk Commented Dec 22, 2019 at 20:51
  • Yeah, that doesn't work. Gives the same error. – alex Commented Dec 22, 2019 at 21:11
  • What you have above is supposed to work just fine, are you sure you don't have any other jest config overriding what is in the package.json? And how are you running the test spec? – Tunmise Ogunniyi Commented Dec 22, 2019 at 21:50
  • The above snippet is the extent of my package.json. I am running the test with yarn jest test. – alex Commented Dec 22, 2019 at 23:11
Add a ment  | 

1 Answer 1

Reset to default 6 +50

It should work. Here is a minimal working example:

./src/index.js:

export function sum(a, b) {
  return a + b;
}

./src/__tests__/index.spec.js:

import { sum } from "../";

it("should sum", () => {
  // eslint-disable-next-line
  console.log("localPath: ", localPath);
  const actualValue = sum(1, 2);
  expect(actualValue).toBe(3);
});

jest.config.js:

module.exports = {
  preset: "ts-jest/presets/js-with-ts",
  testEnvironment: "node",
  coverageReporters: ["json", "text", "lcov", "clover"],
  testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[jt]s?(x)"],
  globals: {
    localPath: "Users/alex/Git/mytodolist"
  }
};

Unit test result:

 PASS  src/__tests__/index.spec.js
  ✓ should sum (4ms)

  console.log src/__tests__/index.spec.js:5
    localPath:  Users/alex/Git/mytodolist

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.233s
Ran all test suites.

As you can see, the value of global variable localPath has been set. Please print the global object and check in your tests.

Codesandbox: https://codesandbox.io/s/unruffled-feistel-byfcc

发布评论

评论列表(0)

  1. 暂无评论