angularjs - 使用 Jest 在 Angular 1.x 上进行快照测试

标签 angularjs testing snapshot jestjs

我想使用 Jest 来快照测试我的 angular 1.x 指令。 我已经有了一个 Jest 工作测试环境,但我不确定如何(以及是否可以)对我的指令/组件进行快照测试。

我不认为我可以使用此示例中使用的渲染器对象(看起来像特定于 react 的对象)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我找不到任何关于快照测试的答案。

提前致谢

费德里科

最佳答案

有效。

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>
`;

关于angularjs - 使用 Jest 在 Angular 1.x 上进行快照测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44567061/

相关文章:

node.js - 为什么 mocha 不显示通过的测试?像开玩笑。它只是说 5 测试通过没有更多细节

Python 看门狗 : what is the 'empty' directory snapshot?

java - HtmlUnit 没有并行运行所有 HTTP 请求?

javascript - 动态快照测试

angularjs - 带有服务注入(inject)的测试 Controller

angularjs - 在 angular ui-router URL 中添加可选属性

javascript - 具有插值的 ng 样式不渲染背景图像?

testing - 如何测试 `if @generated` 的两边?

javascript - Angular : ng-model directive not working with `<span>` element

node.js - 无法在 Node js 上使用 mocha 运行 PhantomJS