Skip to content

[Bug]: not working globals in watch mode #15613

Open
@gretmn102

Description

@gretmn102

Version

29.7.0

Steps to reproduce

  • Init package.json:

    npm init -y
  • Install Jest:

    npm i -D jest@~29.7.0
  • Create jest.config.js:

    // @ts-check
    
    /** @type {import('jest').Config} */
    module.exports = {
      globals: {
        sum: (a, b) => a + b,
      }
    };
  • Create global.test.js with:

    test("sum(2, 2) === 4", () => {
      expect(sum(2, 2)).toBe(4)
    })
  • Run Jest to make sure everything's okay:

    npx jest
  • Initialize empty git (because Jest not work with --watch without it)

    git init
    
  • Run Jest in --watch mode:

    npx jest --watch

    Result:

     FAIL  ./global.test.js
      × sum(2, 2) === 4 (13 ms)
    
       sum(2, 2) === 4
    
        ReferenceError: sum is not defined
    
          1 | test("sum(2, 2) === 4", () => {
        > 2 |   expect(sum(2, 2)).toBe(4)
            |   ^
          3 | })
          4 |
    
          at Object.expect (global.test.js:2:3)
    
    Test Suites: 1 failed, 1 total
    Tests:       1 failed, 1 total
    Snapshots:   0 total
    Time:        1.828 s, estimated 2 s
    Ran all test suites related to changed files.
  • Run Jest in --watchAll mode:

    npx jest --watchAll

    Result:

    FAIL  ./global.test.js
      × sum(2, 2) === 4 (13 ms)
    
       sum(2, 2) === 4
    
        ReferenceError: sum is not defined
    
          1 | test("sum(2, 2) === 4", () => {
        > 2 |   expect(sum(2, 2)).toBe(4)
            |   ^
          3 | })
          4 |
    
          at Object.expect (global.test.js:2:3)
    
    Test Suites: 1 failed, 1 total
    Tests:       1 failed, 1 total
    Snapshots:   0 total
    Time:        1.813 s
    Ran all test suites.

Expected behavior

I expect watch mode to be the same as normal mode, i.e. with globals working.

Actual behavior

globals not working in watch mode.

Additional context

#15555 is probably a related issue, but without simple reproduction steps, it's hard to figure it out.

Environment

System:
  OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
  CPU: (1) x64 AMD Athlon(tm) X4 880K Quad Core Processor
Binaries:
  Node: 18.14.1 - /usr/bin/node
  npm: 9.3.1 - /usr/bin/npm
npmPackages:
  jest: ~29.7.0 => 29.7.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions