Skip to content
This repository was archived by the owner on Apr 2, 2023. It is now read-only.

Commit 5d9d824

Browse files
committed
reproduce jest bug
0 parents  commit 5d9d824

File tree

9 files changed

+2615
-0
lines changed

9 files changed

+2615
-0
lines changed

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
```

jest.config.base.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const baseConfigType = process.env.BASE_CONFIG_TYPE || "shared";
2+
3+
if (baseConfigType === "shared") {
4+
// the default, to demonstrate the jest bug.
5+
// every project will use the same config object.
6+
// jest mutates the object to set rootDir when not set
7+
module.exports = {};
8+
} else if (baseConfigType === "frozen") {
9+
// produces a stack trace where jest tries to mutate the object to set rootDir
10+
module.exports = Object.freeze({});
11+
} else if (baseConfigType === "individual") {
12+
// work around the bug by returning a function that generates a distinct config object for each project
13+
module.exports = () => ({});
14+
} else {
15+
throw new Error(`Unknown BASE_CONFIG_TYPE: ${baseConfigType}`);
16+
}

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
projects: ["<rootDir>/project-a/", "<rootDir>/project-b/"],
3+
};

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"jest": "27.5.1"
4+
}
5+
}

project-a/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("../jest.config.base");

project-a/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test("project a", () => {
2+
expect("project a").toMatchInlineSnapshot(`"project a"`);
3+
});

project-b/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("../jest.config.base");

project-b/test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test("project b", () => {
2+
expect(true).toBe(false);
3+
});

yarn.lock

+2,526
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)