资讯专栏INFORMATION COLUMN

JavaScript Testing

Eric / 1936人阅读

Testing framework

both use describe, it functions

Jasmine(Behavior-Driven JavaScript)

spyOn(User, "save") jasmine.createSpy()

the Jasmine framework has almost everything built into it including assertions/expectations and test double utilities (which come in the form of spies). However, it does not have a test runner so you will need to use a tool like Karma for that

describe("", function() {
var foo;
beforEach(function() {
foo = 0;
});
afterEach(function() {
foo = 0;
});

xit("", function() {
expect(true).toBe(true);
});
})

xit, xdescribe(skip test)
其实Jasmine就是JUnit的JavaScript重写版

Jasmine运行环境配置:

运行时环境:这里基于chrome浏览器,通过HTML作为JavaScript载体

源文件:用于实现某种业务逻辑的文件,就是.js文件

测试文件:符合jasmine API的测试js脚本

输出结果: jasmine提供了基于网页的输出结果

直接open SpecRunner.html,就是跑测试了




    
    POS v1 With 3rd Libraries Spec Runner

    
    

    
    
    
    

    
    
    

    
    





Mocha

Mocha includes

test runner

API for setting up your test suite

not include

assertion

test double utilities.

Chai is for assertions when using Mocha.
Sinon is for test doubles in Mocha

Test Runner for JavaScript Karma

Karma just launches an HTTP server, and generates the test runner HTML file.A simple tool that allows you to execute JavaScript code in multiple real browsers.
karma just launches a HTTP server,and generates the test runner HTML file,
karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner)。该工具可用于测试所有主流web浏览器,也可集成到CI工具,也可和其他代码编辑器一起使用,这个测试工具的一个强大特性就是可以监控文件的变化,然后自动执行,通过console.log显示测试结果。

./node_modules/karma/bin/karma init(用来生成karma.conf.js配置文件)
./node_modules/karma/bin/karma start karma.conf.js(run test)


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/*Spec.js"],


    // list of files to exclude
    exclude: ["karma.conf.js"],


    // 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
  })
}
Enzyme

JavaScript Testing utilities for React

Jest

它提供了一种“零配置”的开发体验,并具备诸多开箱即用的功能,比如 mock 和代码覆盖率。你不仅可以将此测试框架应用于 React.js 应用程序,也可以应用于其他 JavaScript 框架。

npm i jest-cli -g
jest src/helpers/__test__/a.js
Testing Assertions

Chai/Expect/Should

UI test

selenium
webdriverio
webdrivercss
https://www.codementor.io/jav...
http://stateofjs.com/2016/tes...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/90121.html

相关文章

  • 为你的JavaScript代码写测试

    摘要:下面会讲解如何使用以及,来为我们的代码编写测试。我们不妨先选择前者综上所述,我们要使用组合来为我们的代码写测试。他们分别会在每个测试的之前和之后执行一次。副本最后还有一个问题是如何结合来为我们的代码编写测试。 下面会讲解如何使用 karama, jasmine 以及 webpack,来为我们的 ES6 代码编写测试。(最后我写了一个可用的例子,请查看 ES2015-Starter-Ki...

    FleyX 评论0 收藏0
  • Javascript CI篇(2)- Karma 基础学习

    摘要:核心功能就是启动一个服务并监听项目文件改变,文件改变后再刷新服务器。 Karma 简介 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma。Karma是一个让人感到非常神秘的名字,表示佛教中的缘分,因果报应,比Cassandra这种名字更让人猜不透! Karma是一个基于Node.js的JavaS...

    Ku_Andrew 评论0 收藏0
  • Awesome JavaScript

    摘要: Awesome JavaScript A collection of awesome browser-side JavaScript libraries, resources and shiny things. Awesome JavaScript Package Managers Loaders Testing Frameworks QA Tools MVC Framew...

    endless_road 评论0 收藏0
  • map every forEach diff javascript - whatIsInAName

    摘要:方法的参数是一个函数,所有数组成员依次执行该函数,返回结果为的成员组成一个新数组返回自己写的代码扶额 https://stackoverflow.com/que... The difference is in the return values. .map() returns a new Array of objects created by taking some action on...

    jhhfft 评论0 收藏0
  • FE.TEST-前端测试初探

    摘要:使用可以快速生成一个项目,其中包含了和以及覆盖率统计的配置参考一个创建测试脚本的快速方法其他参考资料前端自动化测试概览测试之使用对项目进行单元测试 showImg(https://segmentfault.com/img/bVbjfXr?w=600&h=317); 前言 测试可以提供快速反馈,根据测试用例覆盖代码,从而提升代码开发效率和质量。根据投入产出价值,通常迭代较快的业务逻辑不做...

    Travis 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<