Skip to content

Commit

Permalink
test: introduce Jest to the project, closes covidatlas#43 (covidatlas#44
Browse files Browse the repository at this point in the history
)

* Add jest
* Add tests for parse.js
* Add Github Action workflow for testing
  • Loading branch information
qgolsteyn authored Mar 15, 2020
1 parent 740bd46 commit c03753f
Show file tree
Hide file tree
Showing 8 changed files with 3,791 additions and 35 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test CI

on: push

jobs:
build:
runs-on: [macOS-latest]
steps:
- uses: actions/checkout@v1 # without submodules
with:
persist-credentials: false
- name: Disable the keychain credential helper
run: git config --global credential.helper ""
- name: Enable the local store credential helper
run: git config --global --add credential.helper store
- name: Add credential
run: echo "https://x-access-token:${{ secrets.GIT_SECRET }}@github.com" >> ~/.git-credentials
- name: Tell git to use https instead of ssh whenever it encounters it
run: 'git config --global url."https://github.com/".insteadof [email protected]:'
- name: Update submodules
run: |
git submodule update --init
- name: Use Node.js 13.x
uses: actions/setup-node@v1
with:
node-version: '13.x'
- name: Install
run: |
yarn
- name: Test
run: |
yarn test
2 changes: 1 addition & 1 deletion coronadatascraper-cache
Submodule coronadatascraper-cache updated 43 files
+273 −276 2020-3-14/0ab962e16615857354fc29aa8f09bc3f.html
+711 −0 2020-3-14/13f0b55ea591cacd1d4b35fa73902a99.html
+168 −157 2020-3-14/16df6d16fc317ccefad8ea852583b128.html
+1 −1 2020-3-14/3090496208e0f86be34fd91ee74d2867.html
+11 −15 2020-3-14/3ee115806a6642a9b8ebb89660d8b92b.html
+15 −15 2020-3-14/4022010d5d7944ff73fa7ca8c33aad3b.html
+6 −5 2020-3-14/40bf7ed5117f6152c41b4af585051e3e.html
+1 −1 2020-3-14/41fa0cf66283639520e88f8d09f704f3.html
+29 −56 2020-3-14/47c208234e52be2cff134c5aff44d49b.html
+6 −1,112 2020-3-14/4f4ea7ea47049b7f01588547fa8ac977.html
+2 −2 2020-3-14/512da790ab26264b18929542b816e62e.html
+4 −4 2020-3-14/5210e2c25c21dc74f2a2b9683058f1c3.html
+15 −19 2020-3-14/603e4a438d243ddfc8e0e1aacb774872.html
+139 −147 2020-3-14/645962ea6d00ea0b5d155438720c5f1d.html
+0 −560 2020-3-14/669036da135540949719f7a2eb408663.html
+14 −0 2020-3-14/6a6fd96879ca615faf494dfc19c52224.csv
+1 −0 2020-3-14/70df8c8c9237f70ff8ad9303e15b9722.json
+201 −195 2020-3-14/7363bfcf3097b1056c073041e0067b6e.html
+32 −1,625 2020-3-14/7a0ebaf089c7afc655fee3726d602346.html
+12 −13 2020-3-14/7ce10fdea482492a5aa09447f3305f3c.html
+7 −7 2020-3-14/80691fc678bbc9ff76cdf0f5aabde136.html
+82 −79 2020-3-14/84d831c063d35789316af6d5dceb1467.html
+25 −23 2020-3-14/8ac687e7c10d244eb51d1af81fdf6ef3.html
+422 −0 2020-3-14/a5f9d8d23a20f71890a61b119183a5fb.csv
+2 −2 2020-3-14/a917a1e0ab82cfd9542919e761853d88.html
+3 −3 2020-3-14/ab90340bbef1bf5bfff52949c21fa53a.html
+7 −34 2020-3-14/aeb78ef12dd3c0a7f01797dd987d091a.html
+1 −3 2020-3-14/bd6fff0af113045a29ec0e23498680be.html
+0 −3,024 2020-3-14/befe247d1354aa5e38efbc780cfc512a.html
+165 −258 2020-3-14/c22d0930a6d6c09964c1d7441f2c072a.html
+0 −67 2020-3-14/c3392b39939aba242d859c6c3c073302.csv
+11 −19 2020-3-14/cd810e1893436ff8d30e235f88335ac5.html
+421 −0 2020-3-14/cf1e3fa5950ef69383000f685b0597d0.csv
+8 −17 2020-3-14/d65494b08658c6bfc9f8da490aa4ce6f.html
+19 −11 2020-3-14/e155164e46aefb2e7337a6c18ba40b9c.html
+422 −0 2020-3-14/e20883430a9a4c7502d0a9618e49c1a9.csv
+422 −0 2020-3-14/ead40cbf6519cc41c790c692e8cdf151.csv
+0 −1,382 2020-3-14/f0f6f868b435b0745d1a3414a1381aac.html
+114 −24 2020-3-14/f4fc7854cd7a7ee60556f9cbfafa0c3a.html
+14 −25 2020-3-14/f785173381056ccd00124cbb0568824d.html
+3 −5 2020-3-14/fbb5f5e0c709d2ede46114618f5ad90b.html
+12 −38 2020-3-14/fc01aef5027c72ec50e078038b1812cf.html
+2 −2 2020-3-14/fe6f3362390e73100d0c445e87542a4b.html
2 changes: 1 addition & 1 deletion coronavirus-data-sources
11 changes: 8 additions & 3 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function number(string) {
if (string === '') {
return 0;
}
return parseInt(string.replace(/[^\d]/g, ''), 10);
return parseInt(string.replace(/[^\d\-]/g, ''), 10);
}

/*
Expand All @@ -19,10 +19,15 @@ function float(string) {
}

/*
Remove line breaks, double spaces, zero-width space, asterisk, and trim the provided stirng
Remove line breaks, double spaces, zero-width space, asterisk, and trim the provided string
*/
function string(string) {
return string.replace(/\n/g, ' ').replace(/\s+/, ' ').replace(/\u200B/g,'').replace(/\*/, '').trim();
return string
.replace(/\n/g, ' ')
.replace(/\s+/, ' ')
.replace(/\u200B/g, '')
.replace(/\*/g, '')
.trim();
}

export { number, float, string };
55 changes: 55 additions & 0 deletions lib/parse.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import each from 'jest-each';

import * as parse from './parse';

describe('Parse functions', () => {
describe('number', () => {
const tests = [
['', 0],
['0', 0],
['1', 1],
['-1', -1],
['10', 10],
['1.1', 11],
['-1.1', -11],
['25abc', 25],
['as-12asdasd', -12]
];
each(tests).test('when passed "%s", it returns %d', (str, expected) => {
expect(parse.number(str)).toBe(expected);
});
});

describe('float', () => {
const tests = [
['', 0],
['0', 0],
['1', 1],
['-1', -1],
['10', 10],
['1.1', 1.1],
['-1.1', -1.1],
['25abc', 25],
['as-12asdasd', -12]
];
each(tests).test('when passed "%s", it returns %d', (str, expected) => {
expect(parse.float(str)).toBe(expected);
});
});

describe('string', () => {
const tests = [
['', ''],
['should not change', 'should not change'],
['0', '0'],
['****test', 'test'],
['this is some text\nsecond line', 'this is some text second line'],
['this some test', 'this some test'],
['this is padded text ', 'this is padded text'],
[' this is padded text', 'this is padded text']
];
each(tests).test('when passed "%s", it returns "%s"', (str, expected) => {
expect(parse.string(str)).toBe(expected);
});
});
});
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@
"buildSite": "cp -r site/* dist/",
"deploy": "npm run dateData && npm run deploySite",
"dateData": "DATE=$(date +'%Y-%m-%d') && cp dist/data.json dist/data-$DATE.json && cp dist/features.json dist/features-$DATE.json && cp dist/data.csv dist/data-$DATE.csv",
"deploySite": "npm run buildSite && gh-pages -a -d dist/ -e ."
"deploySite": "npm run buildSite && gh-pages -a -d dist/ -e .",
"test": "jest"
},
"dependencies": {
"@turf/turf": "^5.1.6",
"cheerio": "^1.0.0-rc.3",
"csv": "^5.3.2",
"editorconfig": "^0.15.3",
"gh-pages": "^2.2.0",
"jest-each": "^25.1.0",
"needle": "^2.3.3",
"puppeteer": "^2.1.1",
"yargs": "^15.3.0"
},
"devDependencies": {
"@babel/core": "^7.8.7",
"@babel/preset-env": "^7.8.7",
"babel-jest": "^25.1.0",
"jest": "^25.1.0"
}
}
Loading

0 comments on commit c03753f

Please sign in to comment.