Continuous Integration
In order to avoid breaking the code in the main branch, I added the Continuous Integration to the GitHub workflow. In this workflow, I want it to run on node ver 14.x
whenever there's push or a pull request to the main
branch. There are 2 actions I wanted to include:
- a clean install of all dependencies:
npm ci
- a full test run:
npm test
The yml
file would look like this:
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm test
After setting it up myself, I found that CI is not too hard to use and it also brings lots of conveniences as it can quickly detect if a push or pull request might contain a potential error.
Adding more tests to another project
This week, I also created some test cases for another repo. Adding tests for someone else's code is no doubt for difficult than working on my own code. I had to understand the logic of the code and try to figure out what each function is supposed to do in order to find all possible scenarios.
For Leyang's project, I found that getHtlmlTitleBody()
was not tested yet so I decided to contribute some tests to it. This function accepts the content of the file as a string and a boolean indicating whether it's a text file or not, it then returns an object with 2 properties: title and body.
const getHtmlTitleBody = (file, isTxt) => {
let html = {
title: '',
body: '',
};
let tempTitle = file.match(/^.+(\r?\n\r?\n\r?\n)/);
if (tempTitle) {
html.title = tempTitle[0].trim();
}
if (isTxt) {
html.body = file
.split(/\r?\n\r?\n/)
.map((para) => {
if (para == html.title) {
return `<h1>${para.replace(/\r?\n/, ' ')}</h1>\n`;
} else {
return `<p>${para.replace(/\r?\n/, ' ')}</p>\n`;
}
})
.join('');
} else {
let md = new markdownit();
html.body = md.render(
file.substring(html.title.length).trim()
);
}
return html;
};
After investigating the function carefully, I came up with 3 scenarios to test:
- a text file input with title
- a text file input without title
- a markdown input file
The details of the tests can be found here.
Top comments (0)