這是Youtube影片(Techniques and practices for testing Angular 2)的觀看筆記
這影片前半段是在介紹Angular2內可以使用的測試方法,由簡單的小範例逐一介紹各方法
影片筆記
工具
-
karma - test runner
-
jasmine - test library
1
2
3
4
5
6
7
8
9// syntax example
describe('Component: payment', ()=>{
it('should instantiate a component', ()=>{
execpt(1+1).toEqual(2);
})
it('should instantiate a component', ()=>{
execpt(1+1).toEqual(2);
})
}) -
wallaby.js - not free
display unit test result and status inline the code.
-
angular-cli
- has test ability build-in
- test command
- ng test
- ng e2e
-
protractor(e2e)
為什麼要測試
因為越來愈多的商業邏輯都往前端走,所以前端的測試就越來越重要了
測試的種類
-
Unit tests: Test certain functions, areas of units of code
-
e2e test: Runs the real application in the browser and simulateds user behavior
Unit Tests的型態
-
Isolated:
- No HTML template
-
Shallow:
- HTML template
- No Child components
-
integrated
-
Test the entire app
-
Isolated tests
- jasmine test file is : *.spec.ts(js)
1 | describe('Component: payment component', ()=>{ |
1 | import { PaymentComponent } from './payment.component'; |
Shollow tests - Mocking and Spy’s
-
Mock Service
1
2
3mockPaymentService = {
processPayment : ()=>{};
} -
Jasmine Spy
- spyOn(Object, 『method name』)
- .and.
- except(spy).toHaveBeenCalled, etc.
1 | import { PaymentComponent } from './payment.component'; |
Shollow test - TestBed and Dom access
1 | // 基本設定 |
範例程式
1 | import { PaymentComponent } from './payment.component'; |
e2e
1 | import { element, by, browser } from 'protractor'; |
其他筆記
設定wallaby.js環境
-
在專案下新增wallaby.js檔案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54var wallabyWebpack = require('wallaby-webpack');
var webpackPostprocessor = wallabyWebpack({
entryPatterns: [
'src/wallabyTest.js',
'src/**/*spec.js'
],
module: {
loaders: [
{test: /\.css$/, loader: 'raw-loader'},
{test: /\.html$/, loader: 'raw-loader'},
{test: /\.js$/, loader: 'angular2-template-loader', exclude: /node_modules/},
{test: /\.json$/, loader: 'json-loader'},
{test: /\.styl$/, loaders: ['raw-loader', 'stylus-loader']},
{test: /\.less$/, loaders: ['raw-loader', 'less-loader']},
{test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'sass-loader']},
{test: /\.(jpg|png)$/, loader: 'url-loader?limit=128000'}
]
}
});
var compilerOptions = require('./src/tsconfig.json').compilerOptions;
module.exports = function (wallaby) {
return {
files: [
{pattern: 'src/**/*.ts', load: false},
{pattern: 'src/**/*.d.ts', ignore: true},
{pattern: 'src/**/*.css', load: false},
{pattern: 'src/**/*.html', load: false},
{pattern: 'src/**/*spec.ts', ignore: true}
],
tests: [
{pattern: 'src/**/*spec.ts', load: false}
],
testFramework: 'jasmine',
compilers: {
'**/*.ts': wallaby.compilers.typeScript(compilerOptions)
},
postprocessor: webpackPostprocessor,
setup: function () {
window.__moduleBundler.loadTests();
},
debug: true
};
}; -
在
src
資料夾下新增wallabyTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import './polyfills.ts';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
var testing = require('@angular/core/testing');
var testingBrowser = require('@angular/platform-browser-dynamic/testing');
testing.getTestBed().initTestEnvironment(
testingBrowser.BrowserDynamicTestingModule,
testingBrowser.platformBrowserDynamicTesting()); -
執行
npm install wallaby-webpack angular2-template-loader --save-dev
. -
在Visual Studio Code安裝Wallaby.ts的Extension
-
執行Wallaby