-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Explicit Resource Management to mocked functions (
- Loading branch information
Showing
11 changed files
with
215 additions
and
4 deletions.
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