zoukankan      html  css  js  c++  java
  • 前端单元测试工具karma和jest

      1.Jasmine是一个很好的单元测试框架,它有漂亮简单的API

      

    describe('you can group test cases in "describe" blocks...', function() { 
        describe('...which can also be nested', function() {
            it('test cases are in "it" blocks', function() {
            var string = 'where we can run arbitrary JavaScript code...'; // ...and make assertions about results using "expect": 
            expect(string).toEqual('expected string');
          }); 
        });
    });

      

     Karma 是一个集成了像 Jasmine(基于 BDD 的测试框架),PhantomJS(无界面的浏览器)等的测试工具。

     npm安装好后,就要写karma的配置文件

    //karma.conf.js
    module.exports = function(config) { config.set({
        frameworks: ['jasmine'],
        files: [
          'src/**/*.js',
          'test/**/*_spec.js'
        ],
        preprocessors: {
          'test/**/*.js': ['jshint','browserify'],
        'src/**/*.js': ['jshint','browserify']
        },
         /**
             ** ChromeDebugging,ChromeDebugging可以打开chrome devtool,出来的画面,点击DEBUG按钮,调试测试用例,
             ** PhantomJS需要安装对应的phantomjs和karma-phantomjs-laucher
             */
            browsers: ['ChromeDebugging'],
            customLaunchers: {
                ChromeDebugging: {
                    base: 'Chrome',
                    flags: ['--remote-debugging-port=9333']
                }
    
      })
    }

    这里定义了要测试的文件路径,同时定义了跑测试前,一个jshint的预处理。(触发JSHint)

    //.jshintrc
    {
     "browser": true,
    "browserify": true,
    "devel": true,
    "globals": { "jasmine": false,
            "describe": false,
            "it": false,
            "expect": false,
            "beforeEach": false,
            "afterEach": false        }
    }

     2. Jest是facebook后来使用的一个测试框架,集成了mocha等等,使用很方便
      
      

    npm install -S jest jest-watch-typeahead ts-jest

          因为我要使用ts,所以多安装了ts-jest

      根目录建立一个jest.config.js文件
      

    module.exports = {
      testMatch: ['<rootDir>/test/**/*.ts'],
      preset: 'ts-jest',
      testEnvironment: 'node',
    };

    package.json下添加脚本:

    "test": "jest"

    然后就可以在test文件夹下写测试用例了

  • 相关阅读:
    做开发的童鞋应该都了解这几款软件
    给文件对比工具自定义快捷键的方法
    C/C++ 编程有哪些值得推荐的辅助工具
    如何用Beyond Compare修改对比文件颜色
    据说这些工具可以提高程序员的工作效率
    遇到Beyond Compare禁止编辑该怎么办
    BZOJ
    周三
    大总结
    周二上午
  • 原文地址:https://www.cnblogs.com/johnzhu/p/7874413.html
Copyright © 2011-2022 走看看