Add vi.spyOn and clean up some mock function binding calls (#3376)

* Add vi.spyOn and clean up some binding calls

* add vi.restoreAllMocks

* remove junk file

---------

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
dave caruso
2023-06-24 02:24:05 -04:00
committed by GitHub
parent 5ae8e5d773
commit ceec1afec2
6 changed files with 48 additions and 23 deletions

View File

@@ -391,6 +391,7 @@ void JSMockFunction::visitChildrenImpl(JSCell* cell, Visitor& visitor)
visitor.append(fn->instances);
visitor.append(fn->returnValues);
visitor.append(fn->invocationCallOrder);
visitor.append(fn->spyOriginal);
fn->mock.visit(visitor);
}
DEFINE_VISIT_CHILDREN(JSMockFunction);
@@ -526,13 +527,13 @@ extern "C" void JSMock__resetSpies(Zig::GlobalObject* globalObject)
globalObject->mockModule.activeSpies.clear();
}
extern "C" EncodedJSValue jsFunctionResetSpies(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callframe)
extern "C" EncodedJSValue JSMock__jsRestoreAllMocks(JSC::JSGlobalObject* globalObject, JSC::CallFrame* callframe)
{
JSMock__resetSpies(jsCast<Zig::GlobalObject*>(globalObject));
return JSValue::encode(jsUndefined());
}
extern "C" EncodedJSValue JSMock__spyOn(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callframe)
extern "C" EncodedJSValue JSMock__jsSpyOn(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callframe)
{
auto& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
@@ -963,7 +964,7 @@ JSC_DEFINE_CUSTOM_GETTER(jsMockFunctionGetter_protoImpl, (JSC::JSGlobalObject *
return JSValue::encode(jsUndefined());
}
JSC_DEFINE_HOST_FUNCTION(jsMockFunctionConstructor, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callframe))
extern "C" EncodedJSValue JSMock__jsMockFn(JSC::JSGlobalObject* lexicalGlobalObject, JSC::CallFrame* callframe)
{
auto& vm = lexicalGlobalObject->vm();
auto* globalObject = jsCast<Zig::GlobalObject*>(lexicalGlobalObject);
@@ -997,11 +998,6 @@ JSC_DEFINE_HOST_FUNCTION(jsMockFunctionConstructor, (JSC::JSGlobalObject * lexic
return JSValue::encode(thisObject);
}
extern "C" EncodedJSValue JSMockFunction__createObject(Zig::GlobalObject* globalObject)
{
auto& vm = globalObject->vm();
return JSValue::encode(JSC::JSFunction::create(vm, globalObject, 0, "mock"_s, jsMockFunctionConstructor, ImplementationVisibility::Public));
}
extern "C" EncodedJSValue JSMockFunction__getCalls(EncodedJSValue encodedValue)
{
JSValue value = JSValue::decode(encodedValue);

View File

@@ -333,7 +333,7 @@ pub const Jest = struct {
pub fn Bun__Jest__createTestModuleObject(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
JSC.markBinding(@src());
const module = JSC.JSValue.createEmptyObject(globalObject, 11);
const module = JSC.JSValue.createEmptyObject(globalObject, 12);
const test_fn = JSC.NewFunction(globalObject, ZigString.static("test"), 2, TestScope.call, false);
module.put(
@@ -431,31 +431,32 @@ pub const Jest = struct {
Expect.getConstructor(globalObject),
);
const mock_fn = JSMockFunction__createObject(globalObject);
const spyOn = JSC.NewFunction(globalObject, ZigString.static("spyOn"), 2, JSMock__spyOn, false);
const restoreAllMocks = JSC.NewFunction(globalObject, ZigString.static("restoreAllMocks"), 2, jsFunctionResetSpies, false);
module.put(globalObject, ZigString.static("mock"), mock_fn);
const mockFn = JSC.NewFunction(globalObject, ZigString.static("fn"), 1, JSMock__jsMockFn, false);
const spyOn = JSC.NewFunction(globalObject, ZigString.static("spyOn"), 2, JSMock__jsSpyOn, false);
const restoreAllMocks = JSC.NewFunction(globalObject, ZigString.static("restoreAllMocks"), 2, JSMock__jsRestoreAllMocks, false);
module.put(globalObject, ZigString.static("mock"), mockFn);
const jest = JSValue.createEmptyObject(globalObject, 3);
jest.put(globalObject, ZigString.static("fn"), mock_fn);
jest.put(globalObject, ZigString.static("fn"), mockFn);
jest.put(globalObject, ZigString.static("spyOn"), spyOn);
jest.put(globalObject, ZigString.static("restoreAllMocks"), restoreAllMocks);
module.put(globalObject, ZigString.static("jest"), jest);
module.put(globalObject, ZigString.static("spyOn"), spyOn);
const vi = JSValue.createEmptyObject(globalObject, 1);
vi.put(globalObject, ZigString.static("fn"), mock_fn);
const vi = JSValue.createEmptyObject(globalObject, 3);
vi.put(globalObject, ZigString.static("fn"), mockFn);
vi.put(globalObject, ZigString.static("spyOn"), spyOn);
vi.put(globalObject, ZigString.static("restoreAllMocks"), restoreAllMocks);
module.put(globalObject, ZigString.static("vi"), vi);
return module;
}
extern fn JSMockFunction__createObject(*JSC.JSGlobalObject) JSC.JSValue;
extern fn Bun__Jest__testPreloadObject(*JSC.JSGlobalObject) JSC.JSValue;
extern fn Bun__Jest__testModuleObject(*JSC.JSGlobalObject) JSC.JSValue;
extern fn jsFunctionResetSpies(*JSC.JSGlobalObject, *JSC.CallFrame) JSC.JSValue;
extern fn JSMock__spyOn(*JSC.JSGlobalObject, *JSC.CallFrame) JSC.JSValue;
extern fn JSMock__jsMockFn(*JSC.JSGlobalObject, *JSC.CallFrame) JSC.JSValue;
extern fn JSMock__jsRestoreAllMocks(*JSC.JSGlobalObject, *JSC.CallFrame) JSC.JSValue;
extern fn JSMock__jsSpyOn(*JSC.JSGlobalObject, *JSC.CallFrame) JSC.JSValue;
pub fn call(
_: void,

Binary file not shown.

View File

@@ -2,7 +2,16 @@
* This file is meant to be runnable in both Jest and Bun.
* `bunx jest mock-fn.test.js`
*/
var { isBun, test, describe, expect, jest, vi, mock, bunTest, spyOn } = require("./test-interop.js")();
var { isBun, expect, jest, vi, mock, spyOn } = require("./test-interop.js")();
// if you want to test vitest, comment the above and uncomment the below
// import { expect, describe, test, vi } from "vitest";
// const isBun = false;
// const jest = { fn: vi.fn, restoreAllMocks: vi.restoreAllMocks };
// const spyOn = vi.spyOn;
// import * as extended from "jest-extended";
// expect.extend(extended);
async function expectResolves(promise) {
expect(promise).toBeInstanceOf(Promise);
@@ -434,7 +443,6 @@ describe("mock()", () => {
return "3";
},
);
expect(result).toBe(undefined);
expect(fn()).toBe("1");
});
test("withImplementation (async)", async () => {

View File

@@ -20,6 +20,25 @@ module.exports = () => {
vi: bunTest.vi,
spyOn: bunTest.spyOn,
};
} else if (process.env.VITEST) {
const vi = require("vitest");
return {
isBun: false,
bunTest: null,
test: vi.test,
describe: vi.describe,
it: vi.it,
expect: vi.expect,
beforeEach: vi.beforeEach,
afterEach: vi.afterEach,
beforeAll: vi.beforeAll,
afterAll: vi.afterAll,
jest: { fn: vi.fn },
mock: null,
vi,
spyOn: vi.spyOn,
};
} else {
const globals = require("@jest/globals");
const extended = require("jest-extended");

View File

@@ -22,7 +22,8 @@
"supertest": "^6.1.6",
"svelte": "^3.55.1",
"typescript": "^5.0.2",
"undici": "^5.20.0"
"undici": "^5.20.0",
"vitest": "^0.32.2"
},
"private": true,
"scripts": {