Skip to content

Commit a10c151

Browse files
committed
move to integration test
1 parent 7cfc9d0 commit a10c151

File tree

9 files changed

+142
-62
lines changed

9 files changed

+142
-62
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
('use strict');
8+
9+
if (!Symbol.dispose) {
10+
Object.defineProperty(Symbol, 'dispose', {
11+
get() {
12+
return Symbol.for('nodejs.dispose');
13+
},
14+
});
15+
}
16+
17+
const TestClass = require('../');
18+
const localClass = new TestClass();
19+
20+
it('restores a mock after a test if it is mocked with a `using` declaration', () => {
21+
using mock = jest.spyOn(localClass, 'test').mockImplementation(() => 'ABCD');
22+
expect(localClass.test()).toBe('ABCD');
23+
expect(localClass.test).toHaveBeenCalledTimes(1);
24+
expect(jest.isMockFunction(localClass.test)).toBeTruthy();
25+
});
26+
27+
it('only sees the unmocked class', () => {
28+
expect(localClass.test()).toBe('12345');
29+
expect(localClass.test.mock).toBeUndefined();
30+
expect(jest.isMockFunction(localClass.test)).toBeFalsy();
31+
});
32+
33+
test('also works just with scoped code blocks', () => {
34+
const scopedInstance = new TestClass();
35+
{
36+
using mock = jest
37+
.spyOn(scopedInstance, 'test')
38+
.mockImplementation(() => 'ABCD');
39+
expect(scopedInstance.test()).toBe('ABCD');
40+
expect(scopedInstance.test).toHaveBeenCalledTimes(1);
41+
expect(jest.isMockFunction(scopedInstance.test)).toBeTruthy();
42+
}
43+
expect(scopedInstance.test()).toBe('12345');
44+
expect(scopedInstance.test.mock).toBeUndefined();
45+
expect(jest.isMockFunction(scopedInstance.test)).toBeFalsy();
46+
});
47+
48+
it('jest.fn state should be restored with the `using` keyword', () => {
49+
const mock = jest.fn();
50+
{
51+
using inScope = mock.mockReturnValue(2);
52+
expect(inScope()).toBe(2);
53+
expect(mock()).toBe(2);
54+
}
55+
expect(mock()).not.toBe(2);
56+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = {
9+
plugins: ['@babel/plugin-proposal-explicit-resource-management'],
10+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = class Test {
9+
test() {
10+
return '12345';
11+
}
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"jest": {
3+
"testEnvironment": "node"
4+
},
5+
"dependencies": {
6+
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9"
7+
}
8+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# This file is generated by running "yarn install" inside your project.
2+
# Manual changes might be lost - proceed with caution!
3+
4+
__metadata:
5+
version: 6
6+
cacheKey: 8
7+
8+
"@babel/helper-plugin-utils@npm:^7.22.5":
9+
version: 7.22.5
10+
resolution: "@babel/helper-plugin-utils@npm:7.22.5"
11+
checksum: c0fc7227076b6041acd2f0e818145d2e8c41968cc52fb5ca70eed48e21b8fe6dd88a0a91cbddf4951e33647336eb5ae184747ca706817ca3bef5e9e905151ff5
12+
languageName: node
13+
linkType: hard
14+
15+
"@babel/plugin-proposal-explicit-resource-management@npm:^7.23.9":
16+
version: 7.23.9
17+
resolution: "@babel/plugin-proposal-explicit-resource-management@npm:7.23.9"
18+
dependencies:
19+
"@babel/helper-plugin-utils": ^7.22.5
20+
"@babel/plugin-syntax-explicit-resource-management": ^7.23.3
21+
peerDependencies:
22+
"@babel/core": ^7.0.0-0
23+
checksum: d7a37ea28178e251fe289895cf4a37fee47195122a3e172eb088be9b0a55d16d2b2ac3cd6569e9f94c9f9a7744a812f3eba50ec64e3d8f7a48a4e2b0f2caa959
24+
languageName: node
25+
linkType: hard
26+
27+
"@babel/plugin-syntax-explicit-resource-management@npm:^7.23.3":
28+
version: 7.23.3
29+
resolution: "@babel/plugin-syntax-explicit-resource-management@npm:7.23.3"
30+
dependencies:
31+
"@babel/helper-plugin-utils": ^7.22.5
32+
peerDependencies:
33+
"@babel/core": ^7.0.0-0
34+
checksum: 60306808e4680b180a2945d13d4edc7aba91bbd43b300271b89ebd3d3d0bc60f97c6eb7eaa7b9e2f7b61bb0111c24469846f636766517da5385351957c264eb9
35+
languageName: node
36+
linkType: hard
37+
38+
"core-js@npm:^3.35.1":
39+
version: 3.35.1
40+
resolution: "core-js@npm:3.35.1"
41+
checksum: e246af6b634be3763ffe3ce6ac4601b4dc5b928006fb6c95e5d08ecd82a2413bf36f00ffe178b89c9a8e94000288933a78a9881b2c9498e6cf312b031013b952
42+
languageName: node
43+
linkType: hard
44+
45+
"root-workspace-0b6124@workspace:.":
46+
version: 0.0.0-use.local
47+
resolution: "root-workspace-0b6124@workspace:."
48+
dependencies:
49+
"@babel/plugin-proposal-explicit-resource-management": ^7.23.9
50+
core-js: ^3.35.1
51+
languageName: unknown
52+
linkType: soft

e2e/global-setup/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@babel/core": "^7.0.0",
1111
"@babel/preset-env": "^7.0.0",
1212
"@babel/preset-flow": "^7.0.0",
13+
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9",
1314
"jest-util": "*"
1415
}
1516
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"version": "0.0.0",
55
"devDependencies": {
66
"@babel/core": "^7.11.6",
7-
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9",
87
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
98
"@babel/preset-env": "^7.1.0",
109
"@babel/preset-react": "^7.12.1",

packages/jest-mock/src/__tests__/index.test.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@
1010

1111
import * as util from 'util';
1212
import {type Context, createContext, runInContext, runInNewContext} from 'vm';
13-
import {onNodeVersions} from '@jest/test-utils';
1413
import {ModuleMocker, fn, mocked, spyOn} from '../';
1514

16-
if (!Symbol.dispose) {
17-
Object.defineProperty(Symbol, 'dispose', {
18-
get() {
19-
return Symbol.for('nodejs.dispose');
20-
},
21-
});
22-
}
23-
2415
describe('moduleMocker', () => {
2516
let moduleMocker: ModuleMocker;
2617
let mockContext: Context;
@@ -2441,28 +2432,3 @@ test('`fn` and `spyOn` do not throw', () => {
24412432
spyOn({apple: () => {}}, 'apple');
24422433
}).not.toThrow();
24432434
});
2444-
2445-
onNodeVersions('>=18.0.0', () => {
2446-
describe('Explicit Resource Management', () => {
2447-
it('jest.fn state should be restored with the `using` keyword', () => {
2448-
const mock = jest.fn();
2449-
{
2450-
using inScope = mock.mockReturnValue(2);
2451-
expect(inScope()).toBe(2);
2452-
expect(mock()).toBe(2);
2453-
}
2454-
expect(mock()).not.toBe(2);
2455-
});
2456-
2457-
it('should be restored with the `using` keyword', () => {
2458-
{
2459-
using mockedLog = jest.spyOn(console, 'log');
2460-
expect(jest.isMockFunction(console.log)).toBeTruthy();
2461-
2462-
console.log('test');
2463-
expect(mockedLog).toHaveBeenCalled();
2464-
}
2465-
expect(jest.isMockFunction(console.log)).toBeFalsy();
2466-
});
2467-
});
2468-
});

yarn.lock

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -690,18 +690,6 @@ __metadata:
690690
languageName: node
691691
linkType: hard
692692

693-
"@babel/plugin-proposal-explicit-resource-management@npm:^7.23.9":
694-
version: 7.23.9
695-
resolution: "@babel/plugin-proposal-explicit-resource-management@npm:7.23.9"
696-
dependencies:
697-
"@babel/helper-plugin-utils": ^7.22.5
698-
"@babel/plugin-syntax-explicit-resource-management": ^7.23.3
699-
peerDependencies:
700-
"@babel/core": ^7.0.0-0
701-
checksum: d7a37ea28178e251fe289895cf4a37fee47195122a3e172eb088be9b0a55d16d2b2ac3cd6569e9f94c9f9a7744a812f3eba50ec64e3d8f7a48a4e2b0f2caa959
702-
languageName: node
703-
linkType: hard
704-
705693
"@babel/plugin-proposal-export-default-from@npm:^7.0.0":
706694
version: 7.23.3
707695
resolution: "@babel/plugin-proposal-export-default-from@npm:7.23.3"
@@ -853,17 +841,6 @@ __metadata:
853841
languageName: node
854842
linkType: hard
855843

856-
"@babel/plugin-syntax-explicit-resource-management@npm:^7.23.3":
857-
version: 7.23.3
858-
resolution: "@babel/plugin-syntax-explicit-resource-management@npm:7.23.3"
859-
dependencies:
860-
"@babel/helper-plugin-utils": ^7.22.5
861-
peerDependencies:
862-
"@babel/core": ^7.0.0-0
863-
checksum: 60306808e4680b180a2945d13d4edc7aba91bbd43b300271b89ebd3d3d0bc60f97c6eb7eaa7b9e2f7b61bb0111c24469846f636766517da5385351957c264eb9
864-
languageName: node
865-
linkType: hard
866-
867844
"@babel/plugin-syntax-export-default-from@npm:^7.0.0, @babel/plugin-syntax-export-default-from@npm:^7.23.3":
868845
version: 7.23.3
869846
resolution: "@babel/plugin-syntax-export-default-from@npm:7.23.3"
@@ -3023,7 +3000,6 @@ __metadata:
30233000
resolution: "@jest/monorepo@workspace:."
30243001
dependencies:
30253002
"@babel/core": ^7.11.6
3026-
"@babel/plugin-proposal-explicit-resource-management": ^7.23.9
30273003
"@babel/plugin-transform-modules-commonjs": ^7.1.0
30283004
"@babel/preset-env": ^7.1.0
30293005
"@babel/preset-react": ^7.12.1
@@ -3101,7 +3077,7 @@ __metadata:
31013077
tempy: ^1.0.0
31023078
ts-node: ^10.5.0
31033079
tstyche: ^1.0.0-beta.9
3104-
typescript: ^5.2.2
3080+
typescript: ^5.0.4
31053081
webpack: ^5.68.0
31063082
webpack-node-externals: ^3.0.0
31073083
which: ^4.0.0
@@ -20358,7 +20334,7 @@ __metadata:
2035820334
languageName: node
2035920335
linkType: hard
2036020336

20361-
"typescript@npm:5.3.3, typescript@npm:^5.0.4, typescript@npm:^5.2.2":
20337+
"typescript@npm:5.3.3, typescript@npm:^5.0.4":
2036220338
version: 5.3.3
2036320339
resolution: "typescript@npm:5.3.3"
2036420340
bin:
@@ -20368,7 +20344,7 @@ __metadata:
2036820344
languageName: node
2036920345
linkType: hard
2037020346

20371-
"typescript@patch:typescript@5.3.3#~builtin<compat/typescript>, typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>, typescript@patch:typescript@^5.2.2#~builtin<compat/typescript>":
20347+
"typescript@patch:typescript@5.3.3#~builtin<compat/typescript>, typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>":
2037220348
version: 5.3.3
2037320349
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=e012d7"
2037420350
bin:

0 commit comments

Comments
 (0)