Skip to content

Commit

Permalink
move to integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Feb 10, 2024
1 parent 7cfc9d0 commit a10c151
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 62 deletions.
56 changes: 56 additions & 0 deletions e2e/explicit-resource-management/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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.
*/
('use strict');

if (!Symbol.dispose) {
Object.defineProperty(Symbol, 'dispose', {
get() {
return Symbol.for('nodejs.dispose');
},
});
}

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);
});
10 changes: 10 additions & 0 deletions e2e/explicit-resource-management/babel.config.js
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'],
};
12 changes: 12 additions & 0 deletions e2e/explicit-resource-management/index.js
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';
}
};
8 changes: 8 additions & 0 deletions e2e/explicit-resource-management/package.json
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"
}
}
52 changes: 52 additions & 0 deletions e2e/explicit-resource-management/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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

"core-js@npm:^3.35.1":
version: 3.35.1
resolution: "core-js@npm:3.35.1"
checksum: e246af6b634be3763ffe3ce6ac4601b4dc5b928006fb6c95e5d08ecd82a2413bf36f00ffe178b89c9a8e94000288933a78a9881b2c9498e6cf312b031013b952
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
core-js: ^3.35.1
languageName: unknown
linkType: soft
1 change: 1 addition & 0 deletions e2e/global-setup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9",
"jest-util": "*"
}
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"version": "0.0.0",
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-proposal-explicit-resource-management": "^7.23.9",
"@babel/plugin-transform-modules-commonjs": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.12.1",
Expand Down
34 changes: 0 additions & 34 deletions packages/jest-mock/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,8 @@

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

if (!Symbol.dispose) {
Object.defineProperty(Symbol, 'dispose', {
get() {
return Symbol.for('nodejs.dispose');
},
});
}

describe('moduleMocker', () => {
let moduleMocker: ModuleMocker;
let mockContext: Context;
Expand Down Expand Up @@ -2441,28 +2432,3 @@ test('`fn` and `spyOn` do not throw', () => {
spyOn({apple: () => {}}, 'apple');
}).not.toThrow();
});

onNodeVersions('>=18.0.0', () => {
describe('Explicit Resource Management', () => {
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);
});

it('should be restored with the `using` keyword', () => {
{
using mockedLog = jest.spyOn(console, 'log');
expect(jest.isMockFunction(console.log)).toBeTruthy();

console.log('test');
expect(mockedLog).toHaveBeenCalled();
}
expect(jest.isMockFunction(console.log)).toBeFalsy();
});
});
});
30 changes: 3 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,6 @@ __metadata:
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-proposal-export-default-from@npm:^7.0.0":
version: 7.23.3
resolution: "@babel/plugin-proposal-export-default-from@npm:7.23.3"
Expand Down Expand Up @@ -853,17 +841,6 @@ __metadata:
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

"@babel/plugin-syntax-export-default-from@npm:^7.0.0, @babel/plugin-syntax-export-default-from@npm:^7.23.3":
version: 7.23.3
resolution: "@babel/plugin-syntax-export-default-from@npm:7.23.3"
Expand Down Expand Up @@ -3023,7 +3000,6 @@ __metadata:
resolution: "@jest/monorepo@workspace:."
dependencies:
"@babel/core": ^7.11.6
"@babel/plugin-proposal-explicit-resource-management": ^7.23.9
"@babel/plugin-transform-modules-commonjs": ^7.1.0
"@babel/preset-env": ^7.1.0
"@babel/preset-react": ^7.12.1
Expand Down Expand Up @@ -3101,7 +3077,7 @@ __metadata:
tempy: ^1.0.0
ts-node: ^10.5.0
tstyche: ^1.0.0-beta.9
typescript: ^5.2.2
typescript: ^5.0.4
webpack: ^5.68.0
webpack-node-externals: ^3.0.0
which: ^4.0.0
Expand Down Expand Up @@ -20358,7 +20334,7 @@ __metadata:
languageName: node
linkType: hard

"typescript@npm:5.3.3, typescript@npm:^5.0.4, typescript@npm:^5.2.2":
"typescript@npm:5.3.3, typescript@npm:^5.0.4":
version: 5.3.3
resolution: "typescript@npm:5.3.3"
bin:
Expand All @@ -20368,7 +20344,7 @@ __metadata:
languageName: node
linkType: hard

"typescript@patch:[email protected]#~builtin<compat/typescript>, typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>, typescript@patch:typescript@^5.2.2#~builtin<compat/typescript>":
"typescript@patch:[email protected]#~builtin<compat/typescript>, typescript@patch:typescript@^5.0.4#~builtin<compat/typescript>":
version: 5.3.3
resolution: "typescript@patch:typescript@npm%3A5.3.3#~builtin<compat/typescript>::version=5.3.3&hash=e012d7"
bin:
Expand Down

0 comments on commit a10c151

Please sign in to comment.