-
Notifications
You must be signed in to change notification settings - Fork 464
Update fetch-mock-jest to @fetch-mock/jest #5488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f044dd8
fa3a2be
4ec11d9
d4d8ee6
7aa6c5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,7 +95,13 @@ type APIResultV5 = { | |
| // Make sure that the JSON blob we receive from the API conforms to our flow | ||
| // type definition. | ||
| function _ensureIsAPIResultV5(result: MixedObject): APIResultV5 { | ||
| if (!(result instanceof Object) || !('results' in result)) { | ||
| // It's possible (especially when running tests with Jest) that the parameter | ||
| // inherits from a `Object` global from another realm. By using toString | ||
| // this issue is solved wherever the parameter comes from. | ||
| const isObject = (subject) => | ||
| Object.prototype.toString.call(subject) === '[object Object]'; | ||
|
Comment on lines
+101
to
+102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did we need a function like this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the problem is that the object we get inherits from the Object object in another js environment.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add a comment :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kind of wonder if this might be a bug in jest though. I've seen this in jest's node environment => https://github.com/jestjs/jest/blob/42c8e7de64d1a3f8ee9b7f3b309332ac8f88e8b1/packages/jest-environment-node/src/index.ts#L157-L161 |
||
|
|
||
| if (!isObject(result) || !('results' in result)) { | ||
| throw new Error('Expected an object with property `results`'); | ||
| } | ||
| const results = result.results; | ||
|
|
@@ -104,7 +110,7 @@ function _ensureIsAPIResultV5(result: MixedObject): APIResultV5 { | |
| } | ||
| for (const jobResult of results) { | ||
| if ( | ||
| !(jobResult instanceof Object) || | ||
| !isObject(jobResult) || | ||
| !('found_modules' in jobResult) || | ||
| !('stacks' in jobResult) | ||
| ) { | ||
|
|
@@ -113,7 +119,7 @@ function _ensureIsAPIResultV5(result: MixedObject): APIResultV5 { | |
| ); | ||
| } | ||
| const found_modules = jobResult.found_modules; | ||
| if (!(found_modules instanceof Object)) { | ||
| if (!isObject(found_modules)) { | ||
| throw new Error('Expected `found_modules` to be an object'); | ||
| } | ||
| const stacks = jobResult.stacks; | ||
|
|
@@ -125,7 +131,7 @@ function _ensureIsAPIResultV5(result: MixedObject): APIResultV5 { | |
| throw new Error('Expected `stack` to be an array'); | ||
| } | ||
| for (const frameInfo of stack) { | ||
| if (!(frameInfo instanceof Object)) { | ||
| if (!isObject(frameInfo)) { | ||
| throw new Error('Expected `frameInfo` to be an object'); | ||
| } | ||
| if ( | ||
|
|
@@ -165,7 +171,7 @@ function _ensureIsAPIResultV5(result: MixedObject): APIResultV5 { | |
| throw new Error('Expected `inlines` to be an array'); | ||
| } | ||
| for (const inlineFrame of inlines) { | ||
| if (!(inlineFrame instanceof Object)) { | ||
| if (!isObject(inlineFrame)) { | ||
| throw new Error('Expected `inlineFrame` to be an object'); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
| // @flow | ||
| // | ||
| import { TestEnvironment } from 'jest-environment-jsdom'; | ||
| import { TextDecoder, TextEncoder } from 'util'; | ||
|
|
||
| // This class registers various globals coming from node in test environments. | ||
| export default class CustomTestEnvironment extends TestEnvironment { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comes from https://www.wheresrhys.co.uk/fetch-mock/docs/wrappers/jest#jsdom-campatibility, adapted for our use case. |
||
| async setup() { | ||
| await super.setup(); | ||
|
|
||
| // Register TextDecoder and TextEncoder with the global scope. | ||
| // These are now available globally in nodejs, but not when running with jsdom | ||
| // in jest apparently. | ||
| // Still let's double check that they're from the global scope as expected, so | ||
| // that this can be removed once it's implemented. | ||
| if ('TextDecoder' in this.global) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not fully sure this condition works, TBH... I don't know if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In perfcompare I'm using a newer jsdom that has Request, the followng check actually worked, so it looks like this works after all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see jest v30 was just released 2 days ago, this file will probably need to be adjusted with that update.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Nice, thanks for checking!
Ah, do we have TextDecoder in v30?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TextDecoder I don't know, but Request, Response, fetch are there (but not ReadableStream surprisingly!) |
||
| throw new Error( | ||
| 'TextDecoder is already present in the global scope, please update custom-environment.js.' | ||
| ); | ||
| } | ||
|
|
||
| this.global.TextDecoder = TextDecoder; | ||
| this.global.TextEncoder = TextEncoder; | ||
|
|
||
| // Register Request and friends with the global scope. | ||
| // These are now available globally in nodejs, but not when running with jsdom | ||
| // in jest apparently. | ||
| // Still let's double check that they're from the global scope as expected, so | ||
| // that this can be removed once it's implemented. | ||
| if ('Request' in this.global) { | ||
| throw new Error( | ||
| 'Request is already present in the global scope, please update custom-environment.js.' | ||
| ); | ||
| } | ||
|
|
||
| this.global.fetch = fetch; | ||
| this.global.Request = Request; | ||
| this.global.Response = Response; | ||
| this.global.ReadableStream = ReadableStream; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,7 @@ | |
| import '@testing-library/jest-dom'; | ||
|
|
||
| // This installs jest matchers as a side effect as well. | ||
| import fetchMock from 'fetch-mock-jest'; | ||
| import { Headers, Request, Response } from 'node-fetch'; | ||
| import { TextDecoder, TextEncoder } from 'util'; | ||
| import fetchMock from '@fetch-mock/jest'; | ||
| import crypto from 'crypto'; | ||
|
|
||
| jest.mock('../utils/worker-factory'); | ||
|
|
@@ -22,28 +20,8 @@ if (process.env.TZ !== 'UTC') { | |
| throw new Error('Jest must be run from `yarn test`'); | ||
| } | ||
|
|
||
| // Register TextDecoder and TextEncoder with the global scope. | ||
| // These are now available globally in nodejs, but not when running with jsdom | ||
| // in jest apparently. | ||
| // Still let's double check that they're from the global scope as expected, so | ||
| // that this can be removed once it's implemented. | ||
| if ('TextDecoder' in global) { | ||
| throw new Error( | ||
| 'TextDecoder is already present in the global scope, please update setup.js.' | ||
| ); | ||
| } | ||
|
|
||
| global.TextDecoder = TextDecoder; | ||
| global.TextEncoder = TextEncoder; | ||
|
|
||
| beforeEach(function () { | ||
| // Install fetch and fetch-related objects globally. | ||
| // Using the sandbox ensures that parallel tests run properly. | ||
| global.fetch = fetchMock.sandbox(); | ||
| global.Headers = Headers; | ||
| global.Request = Request; | ||
| global.Response = Response; | ||
| }); | ||
| fetchMock.mockGlobal(); | ||
| global.fetchMock = fetchMock; | ||
|
Comment on lines
+23
to
+24
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was afraid that this could be an issue when running tests in parallel, but I couldn't make it fail. I think that nowadays jest (or is it the jsdom environment, or just new versions of jsdom) isolates the globals better than it used to. |
||
|
|
||
| afterEach(function () { | ||
| // This `__shutdownWorkers` function only exists in the mocked test environment, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I can't seem to remember this. Why do we need this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because our current configuration doesn't deal with esm import and export when running in jest (I think it should be possible to make it work easily nowadays...).
This configuration property configures jest to not run babel on some files, by default this is all of node_modules. We override this line to run babel on some npm packages that are using export/import.