mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-04 02:58:08 +08:00
unit test
This commit is contained in:
1
.eslintignore
Normal file
1
.eslintignore
Normal file
@@ -0,0 +1 @@
|
||||
coverage
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -13,3 +13,4 @@ yarn-error.log
|
||||
tmp
|
||||
*.swp
|
||||
*.iml
|
||||
coverage
|
||||
|
||||
@@ -3,3 +3,4 @@ docs/.vuepress/dist
|
||||
package-lock.json
|
||||
.github/
|
||||
renovate.json
|
||||
coverage
|
||||
|
||||
@@ -8,6 +8,10 @@ install:
|
||||
script:
|
||||
- npm run test
|
||||
|
||||
after_script:
|
||||
- npm install codecov
|
||||
- ./node_modules/.bin/codecov
|
||||
|
||||
cache:
|
||||
yarn: true
|
||||
directories:
|
||||
|
||||
@@ -143,3 +143,5 @@ if (config.connect.socket) {
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = app;
|
||||
|
||||
@@ -24,6 +24,7 @@ const logger = winston.createLogger({
|
||||
logger.add(
|
||||
new winston.transports.Console({
|
||||
format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
|
||||
silent: process.env.NODE_ENV === 'test',
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
15
package.json
15
package.json
@@ -10,7 +10,7 @@
|
||||
"docs:build": "vuepress build docs",
|
||||
"format": "eslint \"**/*.js\" --fix && prettier \"**/*.{js,json,md}\" --write",
|
||||
"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": {
|
||||
"type": "git",
|
||||
@@ -32,10 +32,12 @@
|
||||
"eslint": "5.11.0",
|
||||
"eslint-config-prettier": "3.3.0",
|
||||
"eslint-plugin-prettier": "3.0.0",
|
||||
"jest": "^23.6.0",
|
||||
"lint-staged": "8.1.0",
|
||||
"nodemon": "1.18.9",
|
||||
"prettier": "1.15.3",
|
||||
"prettier-check": "2.0.0",
|
||||
"supertest": "3.3.0",
|
||||
"vuepress": "0.14.8",
|
||||
"yorkie": "2.0.0"
|
||||
},
|
||||
@@ -76,5 +78,16 @@
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
},
|
||||
"jest": {
|
||||
"testMatch": [
|
||||
"**/test/*.js"
|
||||
],
|
||||
"coverageReporters": [
|
||||
"text-summary",
|
||||
"lcov"
|
||||
],
|
||||
"bail": true,
|
||||
"testEnvironment": "node"
|
||||
}
|
||||
}
|
||||
|
||||
2
test/.eslintrc
Normal file
2
test/.eslintrc
Normal file
@@ -0,0 +1,2 @@
|
||||
env:
|
||||
jest: true
|
||||
4
test/cases.json
Normal file
4
test/cases.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"text": ["/"],
|
||||
"rss": ["/bilibili/bangumi/21680", "/weibo/user/1195230310"]
|
||||
}
|
||||
23
test/response.js
Normal file
23
test/response.js
Normal 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
7
test/rules/index.js
Normal 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
39
test/rules/rss.js
Normal 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
3
test/rules/status.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = (response) => {
|
||||
expect(response.status).toBe(200);
|
||||
};
|
||||
Reference in New Issue
Block a user