Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Haunted House of
Angular unit testing

Haunted House of
Angular unit testing

How to reduce execution time Angular Karma testing

Yosuke Kurami

May 10, 2017
Tweet

More Decks by Yosuke Kurami

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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);
  4. What TestBed does? it(…)ͷ౎౓ɺFooModuleͷશcomponent͕compile͞ΕΔ beforeEach(async(() => { TestBed.configureTestingModule({ imports: [

    FooModule, ], }).compileComponents(); })); it('should ...', () => { /* ... /* }); .component.spec.tsͰimportsΛॻ͘ͱ…
  5. Avoid to compile components componentͷcompileճ਺ΛݮΒͤ͹, ͦͷ෼ߴ଎Խ • moduleΛద੾ͳཻ౓ʹ෼ׂͯ͠ґଘର৅Λখ͘͢͞Δ • componentΛcompile͠ͳ͍.

    ௨ৗͷclassͱͯ͠test͢Δ
 (isolated-component-tests) • ෳࡶͳॲཧΛcomponent͔Βservice΁Ҡߦ͢Δ 
 service testͷׂ߹Λ૿΍͢
  6. 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
  7. How effective ? • ࣮prjͰ͸໿50~60%ͷ࣮ߦ࣌ؒ࡟ݮޮՌ • Pros: • ಋೖ༰қɻtest.tsͷमਖ਼ͷΈͰಈ࡞͢Δ •

    Cons: • Angular 5.xͷࠒʹ͸௨༻͠ͳ͘ͳΔ΍΋… • ୯ମςετͷႈ౳ੑΛߟྀ͢ΔͱΩϫϞϊ…
  8. Improvement techniques ҙ֎ͱڭՊॻʹԊͬͯΔײɿ • Nightmare < - > ߴ଎ͳ࣮ߦج൫ͷಋೖ •

    Parallel Karma < - > ௚ྻॲཧͷฒྻԽ • Zombie Compiler < - > Flyweightύλʔϯద༻ ʮ஗͍͔Βճ͞ͳ͍ʯ͸؁͑ɻͲ͠Ͳ͠ςετॻ͜͏ʂ