Jestjs 开玩笑地嘲笑移相器

Jestjs 开玩笑地嘲笑移相器,jestjs,phaser-framework,ts-jest,Jestjs,Phaser Framework,Ts Jest,我一直在尝试将jest设置为一个使用Phaser的项目的测试框架,我一直在尝试模仿Phaser本身。我第一次遇到丢失的画布,我可以从链接中解析它。但现在我得到另一个错误“无法读取未定义的属性‘position’” jest.config.js: module.exports = { verbose: true, roots: ['./src'], transform: { '^.+\\.tsx?$': 'ts-jest', }, testRegex: '(/__tes

我一直在尝试将jest设置为一个使用Phaser的项目的测试框架,我一直在尝试模仿Phaser本身。我第一次遇到丢失的画布,我可以从链接中解析它。但现在我得到另一个错误“无法读取未定义的属性‘position’”

jest.config.js:

module.exports = {
  verbose: true,
  roots: ['./src'],
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  moduleNameMapper: {
    '\\.(css|less|scss)$': 'identity-obj-proxy'
  },
  setupFiles: ['jest-canvas-mock']
}
__mocks_;/phaser.js:

const phaser = jest.genMockFromModule('phaser');

module.exports = phaser;
错误消息:

TypeError: Cannot read property 'position' of undefined

      1 | 
    > 2 | const phaser = jest.genMockFromModule('phaser');
        |                     ^
      3 | 
      4 | module.exports = phaser;

      at Image.get [as x] (node_modules/phaser/src/physics/matter-js/components/Transform.js:36:30)
          at Array.forEach (<anonymous>)
          at Array.forEach (<anonymous>)
          at Array.forEach (<anonymous>)
          at Array.forEach (<anonymous>)
          at Array.forEach (<anonymous>)
      at Object.<anonymous> (src/__mocks__/phaser.js:2:21)
      at Object.<anonymous> (src/main.ts:3:1)
      at Object.<anonymous> (src/main.spec.ts:3:1)

还有其他人有这个问题吗?我希望我只是有一些配置错误。

我想感谢您与Phaser 3共享运行Jest的配置

关于您的问题,似乎
Phaser.Physics.Matter.Commponents.Trasnform
组件与
jest.genMockFromModule()
的工作方式不兼容

您只需在
phaser/src/physics/matter js/components/Trasnform.js
中为
Transform
实例的
body
属性创建一个默认值即可破解它:

var转换={
body:body.create({}),
// ...
虽然我不知道它会如何影响生产代码

如果你不在游戏中使用Matter.js物理引擎,也许你可以创建一个新的物理引擎

get: function ()
{
  return this.body.position.x;
},