UNPKG

2.85 kBMarkdownView Raw
1Asynchronous test tools for rxjs and Angular
2
3Requires @angular/core since the tooling relies on zones to schedule asynchronous actions
4
5## Installation
6Install rl-async-testing from npm:
7```
8npm install rl-async-testing --save
9```
10Configure with Angular-CLI and SystemJs:
11```
12/** Map relative paths to URLs. */
13const map: any = {
14 'rl-async-testing': 'vendor/rl-async-testing'
15};
16
17/** User packages configuration. */
18const packages: any = {
19 'rl-async-testing': {
20 main: 'index.js'
21 }
22};
23```
24Modify angular-cli-build.js by adding this line to vendorNpmFiles:
25```
26'rl-async-testing/**/*.+(js|js.map)'
27```
28
29## Usage
30The rlFakeAsync zone can be used in place of the angular fakeAsync zone. The mock tools can be used in conjunction or independently of this, but if combined, the zone will track requests against the mocks and fail the test if there are any requests pending.
31```
32import { rlFakeAsync, mock, IMockedPromise, IMockedRequest } from 'rl-async-testing';
33
34interface IMockDataService {
35 getWithPromise: IMockedPromise<number>;
36 getWithObservable: IMockedRequest<number>;
37}
38
39// mock definition
40let dataService: IMockDataService = {
41 getWithPromise: mock.promise(3),
42 getWithObservable: mock.request(3),
43};
44// or
45let dataService: IMockDataService = {
46 getWithPromise: mock.promise(x => x.value),
47 getWithObservable: mock.request(x => x.value),
48};
49
50
51it('should schedule mock async requests synchronously', rlFakeAsync(() => {
52 dataService.getWithPromise();
53 dataService.getWithObservable();
54
55 // IMockedPromise and IMockedRequest extend Sinon.SinonSpy
56 sinon.assert.calledOnce(getWithPromise);
57 sinon.assert.calledOnce(getWithObservable);
58
59 // assert data not loaded
60
61 dataService.getWithPromise.flush();
62 dataService.getWithObservable.flush();
63
64 // assert data loaded
65}));
66```
67### rxjs scheduling
68The rlFakeAsync zone hooks into the rxjs async scheduler to schedule observables based on 'fake' time. This requires using the rlTick wrapper, which calls into angular's tick, but also tracks time for the rxjs scheduler.
69```
70import { rlFakeAsync, rlTick, flushMicrotasks } from 'rl-async-testing';
71
72it('should schedule rxjs observables synchronously', rlFakeAsync(() => {
73 Observable.of(3).delay(1000);
74
75 // assert data not loaded
76
77 rlTick(1000);
78 flushMicrotasks();
79
80 // assert data loaded
81}));
82```
83
84## Tests
85This project uses systemjs and karma to run unit tests.
86```
87npm install
88npm run build
89npm test
90```
91Alternately:
92```
93npm install
94npm run build-test
95```
96If you encounter an issue and want to debug it:
97```
98npm run test.debug
99```
100or
101```
102npm run build-test.watch
103```
104(Runs tsc in watch mode and concurrently runs the test debugger)
\No newline at end of file