gpt4 book ai didi

javascript - jest.mock() 等同于 sinon.stub()

转载 作者:行者123 更新时间:2023-11-29 15:11:58 25 4
gpt4 key购买 nike

在 Firebase SDK for Cloud Functions Quickstart 的所有示例测试中,都使用了 Mocha/Chai 和 Sinon...试图改用 Jest,我想知道 sinon.stub() 的正确等价物是什么?

我尝试了以下(仅用于测试 jest.mock() 验收...

const sinon = require('sinon');

const jest = require('jest');

describe('Cloud Functions', () => {

before(() => {
let myFunctions, adminInitStub;
let adminInitMock;

adminInitStub = sinon.stub(admin, 'initializeApp');
adminInitMock = jest.mock(admin, 'initializeApp');

myFunctions = require('../index');
myFunctions = require('../index');

但是我得到一个错误:

1 失败

1) 云功能 “首先”钩子(Hook): TypeError: jest.mock 不是函数

我哪里错了......但我无法理解感谢反馈

最佳答案

已解决...

mocking-es-and-commonjs-modules-with-jest-mock上明白了

However, when using export with require we would see an error such as:

TypeError: ourCode is not a function

The CommonJS module does not understand the ES Module it is trying to require. This is easily fixed, by adding a .functionNameBeingExported to the require, which for a default export is default.

externalModule.js
const ourCode = () => 'result';
export default ourCode;

testSubject.js
const ourCode = require('./externalModule').default;
// use ourCode()

关于javascript - jest.mock() 等同于 sinon.stub(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53608880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com