【问题标题】:Error Cannot tween a null target in Unit test with GSAP-TweenMax, Jest and VueJs错误无法在使用 GSAP-TweenMax、Jest 和 VueJs 的单元测试中补间空目标
【发布时间】:2020-03-24 08:43:27
【问题描述】:

我在尝试使用 Jest 对 VueJs 中具有使用 TweenMax GSAP 制作的动画的组件执行单元测试时出错。 错误是:Cannot tween a null target.

在 ztButton.spec.js 中

jest.mock('gsap/TweenMax')

it('Component must to emit event on click', () => {
    const wrapper = shallowMount(ztButton)
    const spy = sinon.spy()
    wrapper.setMethods({ clickButton: spy })
    wrapper.find('.zt-button').trigger('click')
    expect(spy.called).toBe(true)
  })

在我的项目目录中

在 mock 目录的 TweenMax.js 中

module.exports = {
  TweenMax: class {
    static to(selector, time, options) {
      return jest.fn()
    }
  }
}

在测试目录中

有些事情我不明白,或者我做得不好。有点困惑。

更新: 这就是我在组件中生成动画并在挂载中调用的方法

mounted() {
    this.componentId = this._uid
    this._addButtonRipple()
  },
methods: {
    _addButtonRipple() {
      const $button = this.$refs.button
      $button.addEventListener('click', event => {
        const rect = $button.getBoundingClientRect(),
          x = event.clientX - rect.left,
          y = event.clientY - rect.top
        let $ripple = $button.querySelector('.zt-button-ripple')
        TweenMax.set($ripple, {
          x: x,
          y: y,
          scaleX: 0,
          scaleY: 0,
          opacity: 1
        })
        TweenMax.to($ripple, 1.5, {
          scaleX: 1,
          scaleY: 1,
          opacity: 0,
          ease: Expo.easeOut
        })
      })
    },
    clickButton(event) {
      this.$emit('click', event)
      this.isRipple = true
      setTimeout(() => {
        this.isRipple = false
      }, 300)
    }
}

在计算中

computed: {
    listeners() {
      return {
        ...this.$listeners,
        click: event => this.clickButton(event)
      }
    }

在html标签中

<button v-on="listeners"></button>

这是我的文件 jest.config.js 的配置

module.exports = {
  verbose: true,
  moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
  transform: {
    '^.+\\.vue$': 'vue-jest',
    '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$':
      'jest-transform-stub',
    '^.+\\.jsx?$': 'babel-jest'
  },
  transformIgnorePatterns: ['/node_modules/'],
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
    '^src/(.*)$': '<rootDir>/src/$1',
    '^src/component/(.*)$': '<rootDir>/src/components/atomic/$1'
  },
  snapshotSerializers: ['jest-serializer-vue'],
  testMatch: [
    '**/tests/unit/components/atomic/**/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'
  ],
  testURL: 'http://localhost/'
}

【问题讨论】:

  • 你能添加产生问题的组件行吗?只需 GSAP 库的导入语句和您正在使用的 GSAP 方法就足够了。
  • 错误在这里代码行: wrapper.find('.zt-button').trigger('click') in ztButton.spec.js 文件 (ztButton.spec.js:101:32 )
  • 我明白了,但这不是问题,问题在于单击按钮时运行的回调。如果您想获得帮助,您应该将该脚本添加到您的问题中,否则我们不知道您对 TweenMax 的真正用途。
  • 更新帖子
  • 信息够不够,还需要补充吗?

标签: unit-testing vue.js jestjs gsap


【解决方案1】:

Cannot tween a null target 表示不模拟 TweenMax 方法。事实上你只是在嘲笑TweenMax.to 方法。

请以这种方式更新您的模拟:

module.exports = {
  TweenMax: class {
    static to(selector, time, options) {
      return jest.fn()
    }
    static set(selector, options) {
      return jest.fn()
    }
  }
}

如果解决了问题,请告诉我。

【讨论】:

  • 更新模拟,但错误仍然存​​在。我认为他没有检测到模拟。在我的导入中,还有一类“gsap”,即 Expo
  • 你能从你的测试顶部删除jest.mock('gsap/TweenMax')吗?我想知道这是否迫使 Jest 绕过模拟。见jestjs.io/docs/en/bypassing-module-mocks
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2018-03-04
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多