DEV Community

Amazing
Amazing

Posted on

Lab 8: TESTING !!!!

We are on our way completing our project piece by piece. This week, we will adding test to our project. You might have wonder why would we need a test if our program is running and not crashing? From my point of view, testing not only figure it out the bugs, make sure very branches is tested but also help you with the designing of your software. When making test for this week, I have to actually refactor my code a bit more so as to be able to test it out such as exposing proccessMarkdown() in html-maker so as to test it out

module.exports.processingFile = processingFile;
module.exports.processMarkdown = processMarkdown;
Enter fullscreen mode Exit fullscreen mode

The test I have written is very simple since my program is still very small.

Jest installation

To be able to use Jest proficiently with Typescript, we will need help from ts-jest

npm i -D jest 
npm i -D ts-jest @types/jest
npx ts-jest config:init
Enter fullscreen mode Exit fullscreen mode

jest.config.js will give you an option to customize Jest. But when I was writting test using Jest. Eslint always warn me about describe and it or test is not defined and that is because Typescript is not know about these exposed objects yet so I need to add a few change in my .eslintrc.json to tunr off those warnings from Eslint

 "env": {
    "browser": true,
    "es2021": true,
    "node": true,
    "jest/globals": true
  },
Enter fullscreen mode Exit fullscreen mode

Unit test

I got 2 modules to do unit test and they are pretty simple where I test my html-maker to see if I have the right output for the markdown and whether if it will return a blank string if I inpout the wrong file type

const { processingFile, processMarkdown } = require('../html-maker');

describe('testing HTML generator', () => {
  it('should return blank with invalid file extension', () => {
    const fileName = 'test.dat';
    expect(processingFile(fileName)).toEqual('');
  });``

  it('should return correct markdown for mock content', () => {
    const mockData = '## Testing testing';
    expect(processMarkdown(mockData)).toMatch('<h2>Testing testing</h2>');
  });
});


Enter fullscreen mode Exit fullscreen mode

I have a difficulty when try toEqual(string) but when I check how they would test a string. It turn out that I need to use toMatch(string)

Another point of testing is to make sure your code work with a define set of inputs (When you expand your project, users or testers might inject, discover more invalid inputs by accident)

Discussion (0)