quizdown-js icon indicating copy to clipboard operation
quizdown-js copied to clipboard

Add unit tests

Open bonartm opened this issue 3 years ago • 5 comments

I'm not familiar with unit testing for typescript/ svelte projects but this projects needs some proper testing. I'm thankful for any examples and first implementations!

bonartm avatar Jul 01 '21 07:07 bonartm

Hi Malte, I've created a unit test for the Button component. If you would give me the proper rights, I could push it into a new branch called issue13_add_unit_tests

kheitkamp avatar Jan 13 '22 23:01 kheitkamp

I used the following tutorial: https://testing-library.com/docs/svelte-testing-library/intro

I had to add the following packages to the package.json to make the tutorial work: "@testing-library/jest-dom": "^5.16.1", "@testing-library/svelte": "^3.0.3",

I also needed to add the following comment at the top of the test file: /**

  • @jest-environment jsdom */

Jest-dom is described here: https://github.com/testing-library/jest-dom#usage

You'll find an overview of testing queries here: https://testing-library.com/docs/queries/byrole/

kheitkamp avatar Jan 14 '22 00:01 kheitkamp

I've created a new fork and added the changes there in the main branch.

kheitkamp avatar Jan 14 '22 10:01 kheitkamp

For better unit testing I would recommend to break the quiz.ts file into a module which imports all the classes currently included in the quiz.ts file. Like with java, where every class has it's own file. It will make your code much more readable and force you to give your classes a clear structure and interdependency while preventing your code to become spaghetti ;-) Also there are a lot of loose functions within the quiz.ts file which mostly seem to be methods that belong to the BaseQuestion class and therefore should be part of it. Quiz just seems to need a bit clean up ;-) Also I would put each type declaration into its own model file which then should be imported by each class that needs it. Drawing a UML class diagram could be very helpful for structuring the classes: https://www.tutorialspoint.com/uml/uml_class_diagram.htm Typescript has a very detailed documentation of how to summarise different classes into one module: https://www.typescriptlang.org/docs/handbook/modules.html

kheitkamp avatar Jan 14 '22 21:01 kheitkamp

Thanks a lot, Kristian, for your work and suggestions. I will look into it in the upcoming weeks.

bonartm avatar Jan 16 '22 15:01 bonartm