Javascript 的测试, 不管在用 jasmine 还是 mocha, 都是很头疼的事情. 但是自从有了 jest, 一口气写7个测试, 腰也不疼了, 头也不疼了.
只需要 3 个理由
在说用 jest 测为什么好之前,我们先来看我们要测的一个例子.
| var Path = require("path"); | |
| var webpack = require('webpack'); | |
| // 如果有傳入 -d 代表 dev 模式,反之如果有 -p 代表 production | |
| // 此參數是給 $ webpack 用的,與 webpack-dev-server 無關 | |
| /*var isDev = process.argv.indexOf('-d') > -1; | |
| // 特例,如果是執行 $ webpack-dev-server 要強迫進入 isDev 模式,才能啟動 HMR | |
| isDev = process.env.DEV || isDev; |
| # Hexo Configuration | |
| ## Docs: http://hexo.io/docs/configuration.html | |
| ## Source: https://github.com/hexojs/hexo/ | |
| # Site | |
| title: Infinity Road | |
| subtitle: Be a better Programmer | |
| description: Note something I had done | |
| author: Wayne Lee | |
| language: zh-TW |
| jest.dontMock('../InputBox.js'); | |
| var React = require('react/addons'); | |
| var TestUtils = React.addons.TestUtils; | |
| var InputBox = require('../InputBox.js'); | |
| describe('InputBox', function () { | |
| it('type will change the value and state', function () { | |
| var inputBox = TestUtils.renderIntoDocument(<InputBox />); |
| var React = require('react'); | |
| var Yo = require('./Yo.jsx'); | |
| var App = React.createClass({ | |
| render: function(){ | |
| return ( | |
| <div>Hello wrold | |
| <Yo /> | |
| </div>); | |
| } |
| <link rel="import" href="../core-scaffold/core-scaffold.html"> | |
| <link rel="import" href="../core-header-panel/core-header-panel.html"> | |
| <link rel="import" href="../core-menu/core-menu.html"> | |
| <link rel="import" href="../core-item/core-item.html"> | |
| <link rel="import" href="../core-icon-button/core-icon-button.html"> | |
| <link rel="import" href="../core-toolbar/core-toolbar.html"> | |
| <link rel="import" href="../core-menu/core-submenu.html"> | |
| <link rel="import" href="../paper-calculator/paper-calculator.html"> | |
| <link rel="import" href="../paper-toast/paper-toast.html"> |
| <VirtualHost *:80> | |
| ServerName localhost | |
| DocumentRoot /Library/WebServer/Documents/ | |
| </VirtualHost> | |
| <VirtualHost *:80> | |
| ServerName waynelkh.com | |
| ServerAlias www.waynelkh.com | |
| DocumentRoot /Users/waynelkh/Sites/ | |
| ErrorLog "/private/var/log/apache2.waynelkh.error.log" |
| # Setting the prefix from `C-b` to `C-a`. | |
| # By remapping the `CapsLock` key to `Ctrl`, | |
| # you can make triggering commands more comfottable! | |
| set -g prefix C-a | |
| # Free the original `Ctrl-b` prefix keybinding. | |
| unbind C-b | |
| # Ensure that we can send `Ctrl-a` to other apps. | |
| bind C-a send-prefix |
| <?php | |
| $lang['alpha_dash_dot'] = '%s 只能填寫英文字母、數字、底線(_)、破折號(-)或句號(.)。'; | |
| $lang['alpha_slash_dot'] = '%s 只能填寫英文字母、數字、底線(_)、斜線(/)或句號(.)。'; | |
| $lang['min_date'] = '%s 必須晚於 %s。'; | |
| $lang['max_date'] = '%s 必須早於 %s。'; | |
| $lang['min_size'] = '%s 必須大於 %s。'; | |
| $lang['max_size'] = '%s 必需小於 %s。'; | |
| $lang['transaction'] = '%s 交易失敗 %s。'; | |
| $lang['unique'] = '%s 您所提供的已經存在。'; |