Skip to content

Commit fed97cd

Browse files
committed
Add "fetch" polyfill for tests (not working, issue with jest setupFilesAfterEnv being empty regardless of package.json:jest config)
1 parent 22c9494 commit fed97cd

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

jest.setup.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import fetch from 'node-fetch';
2+
3+
/**
4+
* @see https://github.com/vercel/next.js/discussions/13678 How to use built-in fetch in tests?
5+
* @see https://nextjs.org/blog/next-9-4#improved-built-in-fetch-support Next.js Blog - Improved Built-in Fetch Support
6+
* @see https://jestjs.io/docs/en/configuration#setupfilesafterenv-array About setupFilesAfterEnv usage
7+
*/
8+
// Polyfill "fetch" for node.js, so that our tests may replicate the built-in "fetch" provided by Next.js
9+
global.fetch = fetch;
10+
11+
console.info('Jest env has been enhanced - See "./jest.setup.js"')

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
"test": "NODE_ENV=test jest --watchAll",
4141
"test:once": "NODE_ENV=test jest",
4242
"test:coverage": "NODE_ENV=test jest --coverage",
43+
"test:config": "NODE_ENV=test jest --showConfig",
4344
"security:audit": "yarn audit",
4445
"packages:upgrade": "yarn upgrade-interactive --latest"
4546
},
4647
"jest": {
4748
"setupFilesAfterEnv": [
48-
"jest-extended"
49+
"jest-extended",
50+
"./jest.setup.js"
4951
]
5052
},
5153
"dependencies": {
@@ -128,6 +130,7 @@
128130
"@types/lodash.some": "4.6.6",
129131
"@types/lodash.startswith": "4.2.6",
130132
"@types/lodash.xorby": "4.7.6",
133+
"@types/node-fetch": "2.5.7",
131134
"@types/popper.js": "1.11.0",
132135
"@types/react": "16.9.35",
133136
"@types/reactstrap": "8.4.2",
@@ -149,6 +152,7 @@
149152
"eslint-watch": "6.0.1",
150153
"jest": "26.0.1",
151154
"jest-extended": "0.11.5",
155+
"node-fetch": "2.6.0",
152156
"node-mocks-http": "1.8.1",
153157
"now": "17.1.1",
154158
"react-test-renderer": "16.13.1",

yarn.lock

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,6 +2146,14 @@
21462146
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea"
21472147
integrity sha512-NYrtPht0wGzhwe9+/idPaBB+TqkY9AhTvOLMkThm0IoEfLaiVQZwBwyJ5puCkO3AUCWrmcoePjp2mbFocKy4SQ==
21482148

2149+
2150+
version "2.5.7"
2151+
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.7.tgz#20a2afffa882ab04d44ca786449a276f9f6bbf3c"
2152+
integrity sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw==
2153+
dependencies:
2154+
"@types/node" "*"
2155+
form-data "^3.0.0"
2156+
21492157
"@types/node@*":
21502158
version "13.7.6"
21512159
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.7.6.tgz#cb734a7c191472ae6a2b3a502b4dfffcea974113"
@@ -3825,7 +3833,7 @@ [email protected]:
38253833
color "3.0.x"
38263834
text-hex "1.0.x"
38273835

3828-
combined-stream@^1.0.6, combined-stream@~1.0.6:
3836+
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
38293837
version "1.0.8"
38303838
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
38313839
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
@@ -5657,6 +5665,15 @@ [email protected]:
56575665
tapable "^1.0.0"
56585666
worker-rpc "^0.1.0"
56595667

5668+
form-data@^3.0.0:
5669+
version "3.0.0"
5670+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
5671+
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
5672+
dependencies:
5673+
asynckit "^0.4.0"
5674+
combined-stream "^1.0.8"
5675+
mime-types "^2.1.12"
5676+
56605677
form-data@~2.3.2:
56615678
version "2.3.3"
56625679
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"

0 commit comments

Comments
 (0)