Slide 1

Slide 1 text

Haunted House of
 Angular unit testing by @Quramy #ng_sake 2017.05.10

Slide 2

Slide 2 text

About me

Slide 3

Slide 3 text

ͦΕ͸ڈ೥ͷࢣ૸ͷ͜ͱ…

Slide 4

Slide 4 text

feature branch࡞ͬͯͶɺ ŝƄŕŰŲƄʹcommitͨ͠ΜͰ͢Α

Slide 5

Slide 5 text

ͦͨ͠ΒCI͕૸Γग़ͯ͠Ͷɺ

Slide 6

Slide 6 text

Ͱ΋Ͷɺ ͳʔΜ͔༷ࢠ͕͓͔͍͠

Slide 7

Slide 7 text

มͩͳʔɺා͍ͳʔ

Slide 8

Slide 8 text

ڪΔڪΔCIͷ؅ཧը໘Λ ։͍ͯΈΔͱ…

Slide 9

Slide 9 text

ςετ͕ऴΘΒͳ͍ΜͰ͢ Α͓͓͓͓͒͒͒͒͒

Slide 10

Slide 10 text

Angular

Slide 11

Slide 11 text

Scale of our product: ~130,000 loc ~30 angular modules ~700 components ~1,000 cases

Slide 12

Slide 12 text

1. Docker pull, run container 2. Webpack bundle 3. Run Karma (with capture screenshots) 4. Upload screenshot images to AWS S3 Workflow of unit test on CI 1 2 3 4

Slide 13

Slide 13 text

Question: workflow࣮ߦʹͲΕ͘Βֻ͍͔Δͷ͔

Slide 14

Slide 14 text

Changes of duration ྘ઢ: CIͰͷ୯ମςετ࣮ߦ࣌ؒ [msec]: ࠨ࣠, 
 ੨ઢ: masterͷιʔείʔυߦ਺ [loc]: ӈ࣠

Slide 15

Slide 15 text

Changes of duration ྘ઢ: CIͰͷ୯ମςετ࣮ߦ࣌ؒ [msec]: ࠨ࣠, 
 ੨ઢ: masterͷιʔείʔυߦ਺ [loc]: ӈ࣠ 1 upgrade ng 4 3 2

Slide 16

Slide 16 text

ɹͰԿΛͨ͠ͷ͔?

Slide 17

Slide 17 text

Today’s theme: 
 ୯ମςετߴ଎ԽςΫχοΫ
 (ςετίʔυͷॻ͖ํͱ͔͸Ұ੾આ໌͠ͳ͍Α!)

Slide 18

Slide 18 text

1. Good bye Phantom, welcome to Nightmare

Slide 19

Slide 19 text

Unit testing on CI • Angularͷ୯ମςετͰ͸KarmaΛ༻͍Δ • Karmaͷಈ࡞ʹ͸ϒϥ΢β͕ඞཁ • CIͰ͸headlessʹಈ࡞ͤ͞ͳͯ͘͸ͳΒͳ͍

Slide 20

Slide 20 text

PhantomJS • Prjॳظ͸karma-phantomjs-launcher Λར༻ • PhantomJSࣗମɺChrome౳ͱൺ΂Δͱ஗͍ • ࠷େͷܽ఺͸ͦͷෆ҆ఆੑɻ
 “๢ྶ”ͱ͔ݴ͏ׂʹ΍ͨΒࢮ͵ɻ

Slide 21

Slide 21 text

PhantomJS -> hangs up

Slide 22

Slide 22 text

Nightmare • karma-nightmare[1] ʹ৐Γ׵͑ • Nightmare: Electronϕʔεͷscraping tool • Ҏલ͸PhantomJSϕʔεͰ͕͋ͬͨɺੑೳվ ળͷͨΊʹElectronϕʔεʹ੾Γସ͑ΒΕͨ[2] <>https://github.com/bokuweb/karma-nightmare <>https://github.com/segmentio/nightmare/issues/484#issuecomment-184519591

Slide 23

Slide 23 text

How effective ? • ໿10%~20%ͷ࣮ߦ࣌ؒ࡟ݮޮՌ • Pros: • ϋϯάൃੜ͕ແ͘ͳΓɺίϯελϯτʹςε τ͕׬ྃ͢ΔΑ͏ʹͳͬͨ • ಋೖ͕༰қ. karma-confͷઃఆ + Ծ૝ϑϨʔ ϜόοϑΝઃఆͰCIಈ࡞͢Δ

Slide 24

Slide 24 text

2. More Karma, more and more…!

Slide 25

Slide 25 text

Multiple cores, single thread • Karma͸ϒϥ΢β্ͰςετΛ࣮ߦ͢Δ • جຊతʹγϯάϧεϨου • ϚϧνίΞ؀ڥͰ͸ଞͷίΞ͕༡ͼ͕ͪʹ • ͋Εɺϓϩηε෼͚Ε͹Α͘Ͷʁ

Slide 26

Slide 26 text

CPU Load ϩʔΧϧ(MBP, 4 core)ͰͷCPUෛՙ ۀ ۀ

Slide 27

Slide 27 text

ͦ͏ɺզʑʹ͸ ۀ͕଍Γ͍ͯͳ͔ͬͨͷͩ

Slide 28

Slide 28 text

Split test bundles • test bundleΛmodule୯ҐͰ෼ׂ,
 bundle୯ҐͰKarmaϓϩηεΛىಈ /* test.ts */ const context = require.context('./', true, /\.spec\.ts$/); context.keys().map(context); /* test-a.ts */ const context = require.context('./module-a', true, /\.spec\.ts$/); context.keys().map(context); /* test-b.ts */ const context = require.context('./module-b', true, /\.spec\.ts$/); context.keys().map(context);

Slide 29

Slide 29 text

How effective ? • ໿50%~60%ͷ࣮ߦ࣌ؒ࡟ݮޮՌ • Cons: • ʮCPUΛ࢖͑Δ͚ͩ࢖͏ʯઓུͷͨΊɺ CI(Wercker)ͷՔಇঢ়گ࣍ୈͳ෦෼ΞϦ • ಋೖোนߴΊɻbundle࡞੒ॲཧΛΠδΔͨΊɺ
 angular-cliͷ৔߹͸ཁng eject

Slide 30

Slide 30 text

3. Zombie compiler

Slide 31

Slide 31 text

What does make tests slow? JiT Compiler.
 CPU profile with ~300 components, 15 test cases

Slide 32

Slide 32 text

What TestBed does? it(…)ͷ౎౓ɺFooModuleͷશcomponent͕compile͞ΕΔ beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ FooModule, ], }).compileComponents(); })); it('should ...', () => { /* ... /* }); .component.spec.tsͰimportsΛॻ͘ͱ…

Slide 33

Slide 33 text

Avoid to compile components componentͷcompileճ਺ΛݮΒͤ͹, ͦͷ෼ߴ଎Խ • moduleΛద੾ͳཻ౓ʹ෼ׂͯ͠ґଘର৅Λখ͘͢͞Δ • componentΛcompile͠ͳ͍. ௨ৗͷclassͱͯ͠test͢Δ
 (isolated-component-tests) • ෳࡶͳॲཧΛcomponent͔Βservice΁Ҡߦ͢Δ 
 service testͷׂ߹Λ૿΍͢

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

΋͏গ͠଍ૡ͍ͯΈΑ͏

Slide 36

Slide 36 text

TestingCompiler impl • JiT Compiler͸ΩϟογϡػߏΛඋ͍͑ͯΔ • Compiler Lifecycle͸it(…)ຖͰ͋ΔͨΊɺςε τέʔεΛލ͙ͱΩϟογϡ͝ͱഁغ͞ΕΔ • it(“should do A”) ͱ it(“should do B”) Ͱ
 Ωϟογϡ͕࠶ར༻͞ΕΔࣄ͸ͳ͍

Slide 37

Slide 37 text

Ͱ΋ςετର৅ͷఆٛͳΜͯɺ Karma࣮ߦத͸ෆม͡ΌͶʁ
 ※ overrideComponent౳ͷར༻͕ແ͍ͱ͍͏લఏͰ

Slide 38

Slide 38 text

Make compiler “undead” ngx-zombie-compiler[1] • ςετέʔεΛލ͍Ͱ΋ʮࢮͳͳ͍ʯcompiler • (ՄೳͳݶΓ)cacheͨ͠compile݁ՌΛ࢖͍ճͨ͢Ίɺߴ଎ import { ZOMBIE_COMPILER_PROVIDERS } from "ngx-zombie-compiler"; beforeEach(() => { TestBed.configureCompiler({ providers: ZOMBIE_COMPILER_PROVIDERS, }); }); [1] https://github.com/Quramy/ngx-zombie-compiler

Slide 39

Slide 39 text

Demonstration normal with zombie

Slide 40

Slide 40 text

How effective ? • ࣮prjͰ͸໿50~60%ͷ࣮ߦ࣌ؒ࡟ݮޮՌ • Pros: • ಋೖ༰қɻtest.tsͷमਖ਼ͷΈͰಈ࡞͢Δ • Cons: • Angular 5.xͷࠒʹ͸௨༻͠ͳ͘ͳΔ΍΋… • ୯ମςετͷႈ౳ੑΛߟྀ͢ΔͱΩϫϞϊ…

Slide 41

Slide 41 text

Summary

Slide 42

Slide 42 text

Improvement techniques ҙ֎ͱڭՊॻʹԊͬͯΔײɿ • Nightmare < - > ߴ଎ͳ࣮ߦج൫ͷಋೖ • Parallel Karma < - > ௚ྻॲཧͷฒྻԽ • Zombie Compiler < - > Flyweightύλʔϯద༻ ʮ஗͍͔Βճ͞ͳ͍ʯ͸؁͑ɻͲ͠Ͳ͠ςετॻ͜͏ʂ

Slide 43

Slide 43 text

Thank you!