Haunted House of
Angular unit testing

Haunted House of
Angular unit testing

How to reduce execution time Angular Karma testing

893f54413c2bd9ba41d11d753aacaf2c?s=128

Yosuke Kurami

May 10, 2017
Tweet

Transcript

  1. 10.
  2. 12.

    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
  3. 22.

    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
  4. 28.

    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);
  5. 32.

    What TestBed does? it(…)ͷ౎౓ɺFooModuleͷશcomponent͕compile͞ΕΔ beforeEach(async(() => { TestBed.configureTestingModule({ imports: [

    FooModule, ], }).compileComponents(); })); it('should ...', () => { /* ... /* }); .component.spec.tsͰimportsΛॻ͘ͱ…
  6. 33.

    Avoid to compile components componentͷcompileճ਺ΛݮΒͤ͹, ͦͷ෼ߴ଎Խ • moduleΛద੾ͳཻ౓ʹ෼ׂͯ͠ґଘର৅Λখ͘͢͞Δ • componentΛcompile͠ͳ͍.

    ௨ৗͷclassͱͯ͠test͢Δ
 (isolated-component-tests) • ෳࡶͳॲཧΛcomponent͔Βservice΁Ҡߦ͢Δ 
 service testͷׂ߹Λ૿΍͢
  7. 34.
  8. 38.

    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
  9. 40.

    How effective ? • ࣮prjͰ͸໿50~60%ͷ࣮ߦ࣌ؒ࡟ݮޮՌ • Pros: • ಋೖ༰қɻtest.tsͷमਖ਼ͷΈͰಈ࡞͢Δ •

    Cons: • Angular 5.xͷࠒʹ͸௨༻͠ͳ͘ͳΔ΍΋… • ୯ମςετͷႈ౳ੑΛߟྀ͢ΔͱΩϫϞϊ…
  10. 41.
  11. 42.

    Improvement techniques ҙ֎ͱڭՊॻʹԊͬͯΔײɿ • Nightmare < - > ߴ଎ͳ࣮ߦج൫ͷಋೖ •

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