Sails.js:玩笑间谍会导致 TypeError:无法分配给只读属性

发布于 2025-01-19 09:54:18 字数 700 浏览 4 评论 0原文

我试图使用Jest的Spyon嘲笑函数调用的实现:

await sails.helpers.models.test.randomFn.with({ ... });
const randomFnSpy = jest.spyOn(sails.helpers.models.test.randomFn, 'with');
randomFnSpy.mockImplementation(() => {});

错误:

typeError:无法分配使用“ of function”函数runfn(_argins,_explicliccbmaybe,_metadata){

我尝试将属性设置为配置wartial> wortable

Object.defineProperty(
  sails.helpers.models.test.randomFn, 
  'with', 
  { configurable: true, writable: true }
);

错误:

TypeError:无法重新定义属性: 在function.defineproperty()

I'm trying to mock the implementation of a function call using Jest's spyOn:

await sails.helpers.models.test.randomFn.with({ ... });
const randomFnSpy = jest.spyOn(sails.helpers.models.test.randomFn, 'with');
randomFnSpy.mockImplementation(() => {});

Error:

TypeError: Cannot assign to read only property 'with' of function 'function runFn(_argins, _explicitCbMaybe, _metadata){

I tried setting the property as configurable and writable:

Object.defineProperty(
  sails.helpers.models.test.randomFn, 
  'with', 
  { configurable: true, writable: true }
);

Error:

TypeError: Cannot redefine property: with
at Function.defineProperty ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-26 09:54:18

对我来说,模拟助手的方法是:

Sails 在内部使用 machine ,其中执行以下操作:

Object.defineProperty(wetMachine, 'with', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: arginStyle === 'named' ? wetMachine : wetMachine.customize({
      arginStyle: 'named',
      execStyle: execStyle,
      extraArginsTactic: extraArginsTactic,
      extraCallbacksTactic: extraCallbacksTactic,
      arginValidationTactic: arginValidationTactic,
      resultValidationTactic: resultValidationTactic,
      finalAfterExec: finalAfterExec,
      defaultMeta: defaultMeta,
      defaultArgins: defaultArgins,
      // Note there is no reason to pass through `implementationSniffingTactic`
      // here, since it would have already applied now (it applies at build-time.)
    })
  });

不是监视 with,而是嘲笑助手的 fn 达到了目的:

jest.mock('../../../../../api/helpers/models/test/random-fn', () => ({ fn: () => {} }));

What worked to mock helpers for me:

Sails internally uses machine which does the following:

Object.defineProperty(wetMachine, 'with', {
    enumerable: false,
    configurable: false,
    writable: false,
    value: arginStyle === 'named' ? wetMachine : wetMachine.customize({
      arginStyle: 'named',
      execStyle: execStyle,
      extraArginsTactic: extraArginsTactic,
      extraCallbacksTactic: extraCallbacksTactic,
      arginValidationTactic: arginValidationTactic,
      resultValidationTactic: resultValidationTactic,
      finalAfterExec: finalAfterExec,
      defaultMeta: defaultMeta,
      defaultArgins: defaultArgins,
      // Note there is no reason to pass through `implementationSniffingTactic`
      // here, since it would have already applied now (it applies at build-time.)
    })
  });

Instead of spying on with, mocking the helper's fn did the trick:

jest.mock('../../../../../api/helpers/models/test/random-fn', () => ({ fn: () => {} }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文