Search code examples
phplaravelphpunitmocha.jstdd

Applying TDD in an Laravel + React SPA app


I am aware that testing libraries such as mocha, chai are widely used in Javscript, but in PHP, phpunit, phpspec are widely used. I am developing a Laravel + React SPA app, which mean it is API based. So I am considering that I use only mocha, chai without phpunit, phpspec. Is it a widely used method? or, should I use phpunit, phpspec?


Solution

  • It's better to test php specific things with phpunit/phpspec, because it allow you to better test specific happy, alternative or error paths. According to the testing triangle you should write a lot of unit tests (phpunit) and a few acceptance tests (mocha).

    Testing triangle

    Let's any an endpoint can optionally sort and filter on 3 values. If you only test the API you should write 16 (4 x 4) tests if you want to test all different scenario's. Using only API based tests, this will take up quite a lot of time. It's better to test those paths at a lower level instead. That way you only have to write 1 test for you API and a few unit tests which are way lighter than the API tests.