[Unit Testing] What is Unit Testing. 1 - install Karma and test

What is Unit Testing:

Unit tests try to answer questions such as "Did I think about the logic correctly?" or "Does the sort funciton order the list in the right order?"

 

----------------------Install Karma--------------

1. using cmd, type:

npm install -g Karma
npm install -g karma-cli

 

2. In webstrom, open Terminal and type:

karma init

 

3. You will get a karma.config.js file,

put the name of the javascirpt files which you want to test into files array:

复制代码
// Karma configuration
// Generated on Mon Sep 01 2014 12:35:38 GMT+0300 (FLE 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
    files: [
        "test/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
  });
};
复制代码

 

4. In test.js we add some code:

describe("Hello",function(){
   it("should work",function(){
        expect(false).toBe(true)
    }) 
})

 

5. Click karma.config.js to "run" the file. We will get a error, because we expect false to be true.

"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/

    Expected false to be true.
    Error: Expected false to be true.
        at null.<anonymous> (C:/Users/Answer1215/WebstormProjects/angular/Kama_Demo/test/test.js:3:23)


Process finished with exit code 0

 

6. Change the code in test.js:

describe("hello",function(){
    it("Should work",function(){
        expect(false).toBe(false);
    })
})

 

7. Run again , we pass the test.

"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/

Process finished with exit code 0

 

posted @   Zhentiw  阅读(390)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示