Skip to content

ionutpetrache/frontend-guild-testing

Repository files navigation

Examples of setting up testing on a Javascript project

Unit testing (Jasmine, Karma, Mocha)

Jasmine ES6 in-browser unit tests - ready setup

  • Copy the whole directory unit-tests/karma-jasmine_in_chrome.
  • Get into the directory and execute npm install.
  • Load index.html in the browser and verify that all but one sample tests run successfully.
  • Import your code and start writing your own tests.

Mocha ES6 in-browser unit tests - ready setup

  • Copy the whole directory unit-tests/requirejs_es6_in-browser.
  • Get into the directory and execute npm install.
  • Execute 'npm run compile_es6'; this is a sample watcher that will pause and run again every time you modify one of the relevant files.
  • You may now check inside package.json the syntax of the watcher, the directories where it watches files, etc.
  • Load Mocha_test_page.html in the browser and verify that the sample test runs successfully.
  • Import your code and start writing your own tests. NB: every time you create or copy a new file into the setup you need to restart the watcher!!

Mocha ES6 server-side unit tests - ready setup

  • Copy the whole directory unit_tests/es6_in-server.
  • Get into the directory and execute npm install.
  • Execute 'npm run run_es6_tests' and verify that the sample test runs successfully.
  • You may now check inside package.json the syntax of the compiling command, the directories where it reads/writes files, etc.
  • Import your code and start writing your own tests.

Coverage report with Karma and Istanbul

  1. Run the unit test example with coverage: npm install;npm test
  2. The Istanbul code coverage report will be generated in unit-tests/karma-jasmine_in_chrome/coverage folder

End to end testing (NightwatchJS or TestCafe)

How to run the e2e examples

  1. Start the server to have a basic page to run the tests against it:
    npm install;npm start
  2. Start the NightwatchJS end to end tests: cd e2e-tests/nightwatch;npm install;npm test
  3. Start the TestCafe end to end tests: cd e2e-tests/testcafe;npm install;npm test
  4. Start TestCafe end to end on a mobile device: cd e2e-tests/testcafe;npm install;npm run test-mobile
  5. Start end to end tests with Puppeteer: cd e2e-tests/puppeteer;npm install;npm test
  6. Start CodeceptJS end to end tests: cd e2e-tests/codecept;npm install;npm run install:drivers;npm run start:selenium;npm test

TestCafe further info

  • In order to invoke the tests on a specific browsers or on remote browsers have a look on the package.json

Visual validation

  • With visual validation you can create a baseline of images of your application or specific components / elements store them and during testing you generate another set. At the end you will compare the newly generated set with the baseline and you will have an image with the differences. This kind of testing can be use in order to spot the CSS issues as the other types of testing are focused more on functionality.

  • In the example that I've implemented together with the Puppeteer example I am using Resemble to do the visual difference between the generated images. The diff image can be found after running the test in the e2e-tests/puppeteer/output folder.

Using random test data

  • In some of the test examples I've used a random test data generator called Faker.js

About

Working examples of unit tests and e2e tests with various runners and frameworks

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published