-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
add support for Explicit Resource Management to mocked functions #14895
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f1ea1f7
add support for Explicit Resource Management to mocked functions
phryneas 7cfc9d0
review feedback
phryneas a10c151
move to integration test
phryneas e5cf3d2
remove global babel plugin
phryneas 82c82b1
remove accidental line
phryneas fe13eee
add some docs
phryneas b117fad
changelog
phryneas b8e9f02
prettier
phryneas 46031c1
Update CHANGELOG.md
SimenB 37401b9
Apply suggestions from code review
SimenB 9ebd286
Merge branch 'main' into pr/jest-using
SimenB ce99d2d
run new tests
SimenB 832523f
Update e2e/explicit-resource-management/__tests__/index.js
SimenB da7d505
bah
SimenB 6c9f0e6
skip test on old nodes
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {resolve} from 'path'; | ||
import {onNodeVersions} from '@jest/test-utils'; | ||
import {runYarnInstall} from '../Utils'; | ||
import runJest from '../runJest'; | ||
|
||
const DIR = resolve(__dirname, '../explicit-resource-management'); | ||
|
||
beforeAll(() => { | ||
runYarnInstall(DIR); | ||
}); | ||
|
||
onNodeVersions('^18.18.0 || >=20.4.0', () => { | ||
test('Explicit resource management is supported', () => { | ||
const result = runJest(DIR); | ||
expect(result.exitCode).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const TestClass = require('../'); | ||
const localClass = new TestClass(); | ||
|
||
it('restores a mock after a test if it is mocked with a `using` declaration', () => { | ||
using mock = jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD'); | ||
expect(localClass.test()).toBe('ABCD'); | ||
expect(localClass.test).toHaveBeenCalledTimes(1); | ||
expect(jest.isMockFunction(localClass.test)).toBeTruthy(); | ||
}); | ||
|
||
it('only sees the unmocked class', () => { | ||
expect(localClass.test()).toBe('12345'); | ||
expect(localClass.test.mock).toBeUndefined(); | ||
expect(jest.isMockFunction(localClass.test)).toBeFalsy(); | ||
}); | ||
|
||
test('also works just with scoped code blocks', () => { | ||
const scopedInstance = new TestClass(); | ||
{ | ||
using mock = jest | ||
.spyOn(scopedInstance, 'test') | ||
.mockImplementation(() => 'ABCD'); | ||
expect(scopedInstance.test()).toBe('ABCD'); | ||
expect(scopedInstance.test).toHaveBeenCalledTimes(1); | ||
expect(jest.isMockFunction(scopedInstance.test)).toBeTruthy(); | ||
} | ||
expect(scopedInstance.test()).toBe('12345'); | ||
expect(scopedInstance.test.mock).toBeUndefined(); | ||
expect(jest.isMockFunction(scopedInstance.test)).toBeFalsy(); | ||
}); | ||
|
||
it('jest.fn state should be restored with the `using` keyword', () => { | ||
const mock = jest.fn(); | ||
{ | ||
using inScope = mock.mockReturnValue(2); | ||
expect(inScope()).toBe(2); | ||
expect(mock()).toBe(2); | ||
} | ||
expect(mock()).not.toBe(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
module.exports = { | ||
plugins: ['@babel/plugin-proposal-explicit-resource-management'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
module.exports = class Test { | ||
test() { | ||
return '12345'; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "node" | ||
}, | ||
"dependencies": { | ||
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# This file is generated by running "yarn install" inside your project. | ||
# Manual changes might be lost - proceed with caution! | ||
|
||
__metadata: | ||
version: 6 | ||
cacheKey: 8 | ||
|
||
"@babel/helper-plugin-utils@npm:^7.22.5": | ||
version: 7.22.5 | ||
resolution: "@babel/helper-plugin-utils@npm:7.22.5" | ||
checksum: c0fc7227076b6041acd2f0e818145d2e8c41968cc52fb5ca70eed48e21b8fe6dd88a0a91cbddf4951e33647336eb5ae184747ca706817ca3bef5e9e905151ff5 | ||
languageName: node | ||
linkType: hard | ||
|
||
"@babel/plugin-proposal-explicit-resource-management@npm:^7.23.9": | ||
version: 7.23.9 | ||
resolution: "@babel/plugin-proposal-explicit-resource-management@npm:7.23.9" | ||
dependencies: | ||
"@babel/helper-plugin-utils": ^7.22.5 | ||
"@babel/plugin-syntax-explicit-resource-management": ^7.23.3 | ||
peerDependencies: | ||
"@babel/core": ^7.0.0-0 | ||
checksum: d7a37ea28178e251fe289895cf4a37fee47195122a3e172eb088be9b0a55d16d2b2ac3cd6569e9f94c9f9a7744a812f3eba50ec64e3d8f7a48a4e2b0f2caa959 | ||
languageName: node | ||
linkType: hard | ||
|
||
"@babel/plugin-syntax-explicit-resource-management@npm:^7.23.3": | ||
version: 7.23.3 | ||
resolution: "@babel/plugin-syntax-explicit-resource-management@npm:7.23.3" | ||
dependencies: | ||
"@babel/helper-plugin-utils": ^7.22.5 | ||
peerDependencies: | ||
"@babel/core": ^7.0.0-0 | ||
checksum: 60306808e4680b180a2945d13d4edc7aba91bbd43b300271b89ebd3d3d0bc60f97c6eb7eaa7b9e2f7b61bb0111c24469846f636766517da5385351957c264eb9 | ||
languageName: node | ||
linkType: hard | ||
|
||
"root-workspace-0b6124@workspace:.": | ||
version: 0.0.0-use.local | ||
resolution: "root-workspace-0b6124@workspace:." | ||
dependencies: | ||
"@babel/plugin-proposal-explicit-resource-management": ^7.23.9 | ||
languageName: unknown | ||
linkType: soft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
oopsie 😅