|
| 1 | +# Jest bug sharing config instances between projects |
| 2 | + |
| 3 | +If a single config instance ([`jest.config.base.js`](jest.config.base.js#L7)) is shared between multiple projects by [re-exporting the same module](project-b/jest.config.js#L1), Jest will set `rootDir` on the object, causing it to only run the first project, once for each project. |
| 4 | + |
| 5 | +The [project-b tests](project-b/test.js) should fail, but they aren't run. The [project-a suite](project-a/test.js) is run twice: |
| 6 | + |
| 7 | +``` |
| 8 | +$ ./node_modules/.bin/jest |
| 9 | + PASS project-a/test.js |
| 10 | + PASS project-a/test.js |
| 11 | +
|
| 12 | +Test Suites: 2 passed, 2 total |
| 13 | +Tests: 2 passed, 2 total |
| 14 | +Snapshots: 2 passed, 2 total |
| 15 | +Time: 0.155 s, estimated 1 s |
| 16 | +Ran all test suites in 2 projects. |
| 17 | +``` |
| 18 | + |
| 19 | +The location in the Jest source where it mutates the config to set `rootDir` can be found by [freezing the config object](jest.config.base.js#L10). |
| 20 | + |
| 21 | +``` |
| 22 | +$ BASE_CONFIG_TYPE=frozen ./node_modules/.bin/jest |
| 23 | +TypeError: Cannot add property rootDir, object is not extensible |
| 24 | + at readConfigFileAndSetRootDir (/Users/ryan/work/jest-project-shared-config-bug/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:166:26) |
| 25 | + at async readConfig (/Users/ryan/work/jest-project-shared-config-bug/node_modules/jest-config/build/index.js:233:18) |
| 26 | + at async Promise.all (index 0) |
| 27 | + at async readConfigs (/Users/ryan/work/jest-project-shared-config-bug/node_modules/jest-config/build/index.js:441:27) |
| 28 | + at async runCLI (/Users/ryan/work/jest-project-shared-config-bug/node_modules/@jest/core/build/cli/index.js:132:59) |
| 29 | + at async Object.run (/Users/ryan/work/jest-project-shared-config-bug/node_modules/jest-cli/build/cli/index.js:155:37) |
| 30 | +``` |
| 31 | + |
| 32 | +The bug can be worked around by [creating a new object](jest.config.base.js#L13) for each config. Using the config with the environment variable `BASE_CONFIG_TYPE=individual` demonstrates this: |
| 33 | + |
| 34 | +```$ BASE_CONFIG_TYPE=individual ./node_modules/.bin/jest |
| 35 | + FAIL project-b/test.js |
| 36 | + ● project b |
| 37 | +
|
| 38 | + expect(received).toBe(expected) // Object.is equality |
| 39 | +
|
| 40 | + Expected: false |
| 41 | + Received: true |
| 42 | +
|
| 43 | + 1 | test("project b", () => { |
| 44 | + > 2 | expect(true).toBe(false); |
| 45 | + | ^ |
| 46 | + 3 | }); |
| 47 | + 4 | |
| 48 | +
|
| 49 | + at Object.<anonymous> (test.js:2:16) |
| 50 | +
|
| 51 | + PASS project-a/test.js |
| 52 | +
|
| 53 | +Test Suites: 1 failed, 1 passed, 2 total |
| 54 | +Tests: 1 failed, 1 passed, 2 total |
| 55 | +Snapshots: 1 passed, 1 total |
| 56 | +Time: 0.169 s, estimated 1 s |
| 57 | +``` |
0 commit comments