【问题标题】:Snapshot testing on Angular 1.x with Jest使用 Jest 在 Angular 1.x 上进行快照测试
【发布时间】:2018-04-15 22:22:13
【问题描述】:

我想使用 Jest 对我的 Angular 1.x 指令进行快照测试。 我已经有了一个可以使用 jest 的测试环境,但我不确定如何(如果可以的话)对我的指令/组件进行快照测试。

我认为我不能使用此示例中使用的渲染器对象(看起来像一个特定于反应的对象)http://facebook.github.io/jest/docs/en/snapshot-testing.html#content,我不确定如何使用 .toJSON() 函数来序列化我的指令/组件。

这是我在 Jest+Angular 1.x 用法上找到的唯一链接: https://medium.com/aya-experience/testing-an-angularjs-app-with-jest-3029a613251 我找不到任何关于快照测试的答案。

提前致谢,

费德里科

【问题讨论】:

  • 你有什么发现吗?我也会感兴趣
  • 托比亚斯,很遗憾没有答案,也没有时间自己找出任何新答案

标签: angularjs testing snapshot jestjs


【解决方案1】:

有效。

test.js

const angular = require('angular');
require('angular-mocks');

describe('Renderer', () => {
  let element;
  let scope;

  beforeEach(
    angular.mock.inject(($rootScope, $compile) => {
      scope = $rootScope.$new();
      element = $compile(
        '<div><label ng-show="label.show">1</label><label ng-hide="label.show">2</label></div>'
      )(scope);
      scope.$digest();
    })
  );

  it('should render the element', () => {
    expect(element).toBeDefined();
    expect(element[0]).toMatchSnapshot();
  });
});

快照

exports[`Renderer should render the element 1`] = `
<div
  class="ng-scope"
>
  <label
    class="ng-hide"
    ng-show="label.show"
  >
    1
  </label>
  <label
    ng-hide="label.show"
  >
    2
  </label>
</div>
`;

【讨论】:

  • 在您的示例中,您正在为模板使用硬编码字符串。如何导入和使用 HTML 模板?如何将变量注入模板?
  • 模板取决于您的设置。您可以在 $compile 调用中要求 html 或使用 $ngTemplateCache。我已经在我的模板中使用变量label$scope 上的一个变量。 $compile 调用的第二个大括号中需要范围
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 2017-10-01
  • 2020-05-20
  • 1970-01-01
  • 2017-08-13
  • 2018-08-01
  • 2020-01-19
  • 2020-05-04
相关资源
最近更新 更多