Karma & Jasmine

Steps to set up UnitTest for js

  1. According to the angularjs official document here, we select Karma, Jasmine,
    We can write our Test code accroding to the example of Jasmine.

  2. Karma is the Test Runner, Jasmine is the Test Framework. Now, we start our trip of js unit Testing.

  3. Prepare the environment.

. install Karma
we can follow these steps to install and configure karma
step1. install npm, we can download the MSI, current, we chose the v0.12.9,
step2. install karma, karma-Jasmine, karma-chrome-launcher karma-cli. using these commands:

npm install karma --save-dev
npm install karma-jasmine karma-chrome-launcher --save-dev
npm install karma-core --save-dev   // we can install according to our requirements.

step3. we can start to configure the file by running karma init. then we can configure the configure file with the default name: karma.conf.js.

  1. we can use Jasmine to writing our test case.

To show an example,

src.js

function testController($scope) {
    var mySelf = this;
    $scope.test = false;

}
angular.module("App", ["ngDialog"])
        .controller("Test",
        {
            controller: testController,
            controllerAs:t
        });
test.js

describe("myHelloWorld", function () {
    beforeEach(module("App"));
    var tmp,scope;
    beforeEach(inject(function ($rootScope, $controller) {
        // scope = $rootScope.$new();
        scope = { test: true };
        tmp = $controller("Test", { $scope: scope });
    }))
    it("Test", function () {
        expect(scope.test).not.toEqual(true);
    })
})
karma.conf.js


// Karma configuration
// Generated on Tue Nov 01 2016 01:21:48 GMT-0400 (Eastern Daylight Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    Be careful with the order of the files and the dependency relationship between them. as they will be loaded one by one.
    files: [
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/angular.js',
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/angular-mocks.js',
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/ngDialog.js',
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/ngDialog.min.js',
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/App_Client/Components/Common/src.js',
      'ERP_Dev/ERP_Dev/ERP/ERPWeb/App_Client/Components/Orders/test.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
  })
}

posted @   kongshu  阅读(184)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示