使用Jest对Angular Directives进行单元测试

cap*_*lam 4 angularjs angular-mock jestjs

在这个极其简化的角度指令单元测试中,我觉得我缺少一些关键的东西:

import * as angular from 'angular'
import 'angular-mocks'

const app = angular.module('my-app', [])

app.directive('myDirective', () => ({
    template: 'this does not work either',
    link: (scope, element) => { // have also tried compile fn
        console.log('This does not log')
        element.html('Hi!')
    }
}))

describe('myDirective', () => {
    var element, scope

    beforeEach(app)

    beforeEach(inject(($rootScope, $compile) => {
        scope = $rootScope.$new()
        element = $compile('<my-directive />')(scope)
        scope.$digest()
    }))

    it('should actually do something', () => {
        expect(element.html()).toEqual('Hi!')
    })
})
Run Code Online (Sandbox Code Playgroud)

当jest运行时,看起来该指令尚未链接/编译/无论如何

 FAIL  test/HtmlToPlaintextDirective.spec.js
  ? myDirective › should actually do something

    expect(received).toEqual(expected)

    Expected value to equal:
      "Hi!"
    Received:
      ""
Run Code Online (Sandbox Code Playgroud)

Sly*_*nal 10

更新的答案:

在单个文件中导入所有内容时,正确的事情无法按预期工作.

挖掘事物看起来你正在遇到Babel/Jest支持依赖于全局变量的浏览器脚本(如AngularJS)的一些魔力.

发生了什么事是你的模块的angular变量是一样的全局angular变量,它是对角嘲笑可见.

您可以通过在其中一个测试的顶部运行此操作来检查:

import * as angular from 'angular'
import 'angular-mocks'

console.log(angular === window.angular); // `false` in Jest!

console.log(angular.mock); // undefined
console.log(window.angular.mock); // `{...}` defined
Run Code Online (Sandbox Code Playgroud)

要解决此问题,您只需angular在测试中使用全局变量.

src/__ test __/all-in-one.test.js:

import "angular";
import "angular-mocks";

/*
Work around Jest's window/global mock magic.

Use the global version of `angular` that has been augmented by angular-mocks.
*/
var angular = window.angular;


export var app = angular.module('app', []);

app.directive('myDirective', () => ({
    link: (scope, element) => {
        console.log('This does log');
        scope.content = 'Hi!';
    },
    template: 'content: {{content}}'
}));


describe('myDirective', function(){
    var element;
    var scope;

    beforeEach(function(){
        angular.mock.module(app.name);
    });

    it('should do something', function(){
        inject(function(
            $rootScope,
            $compile
        ){
            scope = $rootScope.$new();
            element = $compile('<my-directive></my-directive>')(scope);
            scope.$digest();
        });

        expect(element.html()).toEqual('content: Hi!');
    });
});
Run Code Online (Sandbox Code Playgroud)

原始答案:(这很有效,因为我不小心使用了angular我测试中的全局版本.)

测试中的Angular模块未在测试中正确初始化.

你的电话beforeEach(app)不正确.

相反,您需要使用angular.mock.module("moduleName")初始化模块.

describe('myDirective', () => {
    var element, scope

    // You need to pass the module name to `angular.mock.module()`
    beforeEach(function(){
        angular.mock.module(app.name);
    });


    // Then you can set up and run your tests as normal:
    beforeEach(inject(($rootScope, $compile) => {
        scope = $rootScope.$new()
        element = $compile('<my-directive></my-directive>')(scope)
        scope.$digest()
    }))

    it('should actually do something', () => {
        expect(element.html()).toEqual('Hi!')
    })
});
Run Code Online (Sandbox Code Playgroud)

然后你的测试按我的预期工作:

 PASS  src\__test__\app.test.js
  myDirective
    ? should do something (46ms)
Run Code Online (Sandbox Code Playgroud)

作为参考,这是完整的应用程序和测试:

src/app/app.module.js:

import * as angular from 'angular'

export var app = angular.module('app', []);

app.directive('myDirective', () => ({
    link: (scope, element) => {
        console.log('This does log');
        scope.content = 'Hi!';
    },
    template: 'content: {{content}}'
}))
Run Code Online (Sandbox Code Playgroud)

src/__ test __/app.test.js:

import {app} from "../app/app.module";
import "angular-mocks";

describe('myDirective', function(){
    var element;
    var scope;

    beforeEach(function(){
        angular.mock.module(app.name);
    });

    beforeEach(inject(function(
        $rootScope,
        $compile
    ){
        scope = $rootScope.$new();
        element = $compile('<my-directive></my-directive>')(scope);
        scope.$digest();
    }));

    it('should do something', function(){
        expect(element.html()).toEqual('content: Hi!');
    });
});
Run Code Online (Sandbox Code Playgroud)