unit test

This commit is contained in:
DIYgod
2018-12-30 14:38:11 +08:00
parent 38a90e29b0
commit 21877b1f28
14 changed files with 1378 additions and 40 deletions

1
.eslintignore Normal file
View File

@@ -0,0 +1 @@
coverage

1
.gitignore vendored
View File

@@ -13,3 +13,4 @@ yarn-error.log
tmp tmp
*.swp *.swp
*.iml *.iml
coverage

View File

@@ -3,3 +3,4 @@ docs/.vuepress/dist
package-lock.json package-lock.json
.github/ .github/
renovate.json renovate.json
coverage

View File

@@ -8,6 +8,10 @@ install:
script: script:
- npm run test - npm run test
after_script:
- npm install codecov
- ./node_modules/.bin/codecov
cache: cache:
yarn: true yarn: true
directories: directories:

View File

@@ -143,3 +143,5 @@ if (config.connect.socket) {
process.exit(); process.exit();
}); });
} }
module.exports = app;

View File

@@ -24,6 +24,7 @@ const logger = winston.createLogger({
logger.add( logger.add(
new winston.transports.Console({ new winston.transports.Console({
format: winston.format.combine(winston.format.colorize(), winston.format.simple()), format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
silent: process.env.NODE_ENV === 'test',
}) })
); );

View File

@@ -10,7 +10,7 @@
"docs:build": "vuepress build docs", "docs:build": "vuepress build docs",
"format": "eslint \"**/*.js\" --fix && prettier \"**/*.{js,json,md}\" --write", "format": "eslint \"**/*.js\" --fix && prettier \"**/*.{js,json,md}\" --write",
"lint": "eslint \"**/*.js\" && prettier-check \"**/*.{js,json,md}\"", "lint": "eslint \"**/*.js\" && prettier-check \"**/*.{js,json,md}\"",
"test": "npm run lint && CONNECT_DISABLED=1 node lib/index.js" "test": "npm run lint && NODE_ENV=test jest --coverage --runInBand --forceExit"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -32,10 +32,12 @@
"eslint": "5.11.0", "eslint": "5.11.0",
"eslint-config-prettier": "3.3.0", "eslint-config-prettier": "3.3.0",
"eslint-plugin-prettier": "3.0.0", "eslint-plugin-prettier": "3.0.0",
"jest": "^23.6.0",
"lint-staged": "8.1.0", "lint-staged": "8.1.0",
"nodemon": "1.18.9", "nodemon": "1.18.9",
"prettier": "1.15.3", "prettier": "1.15.3",
"prettier-check": "2.0.0", "prettier-check": "2.0.0",
"supertest": "3.3.0",
"vuepress": "0.14.8", "vuepress": "0.14.8",
"yorkie": "2.0.0" "yorkie": "2.0.0"
}, },
@@ -76,5 +78,16 @@
}, },
"engines": { "engines": {
"node": ">=8.0.0" "node": ">=8.0.0"
},
"jest": {
"testMatch": [
"**/test/*.js"
],
"coverageReporters": [
"text-summary",
"lcov"
],
"bail": true,
"testEnvironment": "node"
} }
} }

2
test/.eslintrc Normal file
View File

@@ -0,0 +1,2 @@
env:
jest: true

4
test/cases.json Normal file
View File

@@ -0,0 +1,4 @@
{
"text": ["/"],
"rss": ["/bilibili/bangumi/21680", "/weibo/user/1195230310"]
}

23
test/response.js Normal file
View File

@@ -0,0 +1,23 @@
const supertest = require('supertest');
const app = require('../lib/index');
const request = supertest(app.callback());
const cases = require('./cases.json');
const statusCheck = require('./rules/status');
const check = require('./rules/index');
describe('response', () => {
cases.text.forEach((url) => {
it(`GET ${url}`, async () => {
const response = await request.get(url);
statusCheck(response);
});
});
cases.rss.forEach((url) => {
it(`GET ${url}`, async () => {
const response = await request.get(url);
await check(response);
});
});
});

7
test/rules/index.js Normal file
View File

@@ -0,0 +1,7 @@
const status = require('./status');
const rss = require('./rss');
module.exports = async (response) => {
status(response);
await rss(response);
};

39
test/rules/rss.js Normal file
View File

@@ -0,0 +1,39 @@
const Parser = require('rss-parser');
const parser = new Parser();
function checkDate(date) {
expect(date).toEqual(expect.any(String));
expect(Date.parse(date)).toEqual(expect.any(Number));
expect(new Date() - new Date(date)).toBeGreaterThan(0);
// date must be in 1 year
expect(new Date() - new Date(date)).toBeLessThan(1000 * 60 * 60 * 24 * 30 * 12);
}
module.exports = async (response) => {
const parsed = await parser.parseString(response.text);
expect(parsed).toEqual(expect.any(Object));
expect(parsed.title).toEqual(expect.any(String));
expect(parsed.title).not.toBe('RSSHub');
expect(parsed.description).toEqual(expect.any(String));
expect(parsed.link).toEqual(expect.any(String));
expect(parsed.lastBuildDate).toEqual(expect.any(String));
expect(parsed.items).toEqual(expect.any(Array));
checkDate(parsed.lastBuildDate);
// check items
const guids = [];
parsed.items.forEach((item) => {
expect(item).toEqual(expect.any(Object));
expect(item.title).toEqual(expect.any(String));
expect(item.link).toEqual(expect.any(String));
expect(item.content).toEqual(expect.any(String));
expect(item.pubDate).toEqual(expect.any(String));
expect(item.guid).toEqual(expect.any(String));
checkDate(item.pubDate);
// guid must be unique
expect(guids).not.toContain(item.guid);
guids.push(item.guid);
});
};

3
test/rules/status.js Normal file
View File

@@ -0,0 +1,3 @@
module.exports = (response) => {
expect(response.status).toBe(200);
};

1315
yarn.lock
View File

File diff suppressed because it is too large Load Diff