Skip to content

Commit afbac09

Browse files
authored
Merge pull request finos#1147 from jescalada/enforce-80-percent-coverage-ci
chore: add testing documentation and coverage checks
2 parents 1232fa5 + 0ffb79c commit afbac09

File tree

9 files changed

+752
-17
lines changed

9 files changed

+752
-17
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ customize for your environment, see the [project's documentation](https://git-pr
9191
- [Quickstart](https://git-proxy.finos.org/docs/category/quickstart/)
9292
- [Installation](https://git-proxy.finos.org/docs/installation)
9393
- [Configuration](https://git-proxy.finos.org/docs/category/configuration)
94+
- [Contributing](https://git-proxy.finos.org/docs/development/contributing)
95+
- [Testing](https://git-proxy.finos.org/docs/development/testing)
9496

9597
## Contributing
9698

@@ -116,6 +118,6 @@ If you can't access Slack, you can also [subscribe to our mailing list](mailto:g
116118

117119
Otherwise, if you have a deeper query or require more support, please [raise an issue](https://github.com/finos/git-proxy/issues) 🧵
118120

119-
🤝 Join our [fortnightly Zoom meeting](https://zoom-lfx.platform.linuxfoundation.org/meeting/95849833904?password=99413314-d03a-4b1c-b682-1ede2c399595) on Monday, 4PM BST (odd week numbers).
120-
🌍 [Convert to your local time](https://www.timeanddate.com/worldclock)
121+
🤝 Join our [fortnightly Zoom meeting](https://zoom-lfx.platform.linuxfoundation.org/meeting/95849833904?password=99413314-d03a-4b1c-b682-1ede2c399595) on Monday, 4PM BST (odd week numbers).
122+
🌍 [Convert to your local time](https://www.timeanddate.com/worldclock)
121123
📅 [Click here](https://calendar.google.com/calendar/event?action=TEMPLATE&tmeid=MTRvbzM0NG01dWNvNGc4OGJjNWphM2ZtaTZfMjAyNTA2MDJUMTUwMDAwWiBzYW0uaG9sbWVzQGNvbnRyb2wtcGxhbmUuaW8&tmsrc=sam.holmes%40control-plane.io&scp=ALL) for the recurring Google Calendar meeting invite. Alternatively, send an e-mail to [help@finos.org](https://zoom-lfx.platform.linuxfoundation.org/meeting/95849833904?password=99413314-d03a-4b1c-b682-1ede2c399595#:~:text=Need-,an,-invite%3F) to get a calendar invitation.

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ coverage:
22
status:
33
project:
44
default:
5+
target: 80
56
informational: true
67
patch:
78
default:
9+
target: 80
810
informational: true

nyc.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const opts = {
2-
branches: 80,
2+
checkCoverage: true,
33
lines: 80,
4-
functions: 80,
5-
statements: 80,
64
};
75

86
console.log('nyc config: ', opts);

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@
149149
"last 1 safari version"
150150
]
151151
},
152+
"engines": {
153+
"node": ">=20.19.2"
154+
},
152155
"lint-staged": {
153156
"*.{js,jsx,ts,tsx,json,md,yml,yaml,css,scss}": [
154157
"prettier --write"

test/1.test.js

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,97 @@
1-
// This test needs to run first
1+
/*
2+
Template test file. Demonstrates how to:
3+
- Use chai-http to test the server
4+
- Initialize the server
5+
- Stub dependencies with sinon sandbox
6+
- Reset stubs after each test
7+
- Use proxyquire to replace modules
8+
- Clear module cache after a test
9+
*/
10+
211
const chai = require('chai');
312
const chaiHttp = require('chai-http');
13+
const sinon = require('sinon');
14+
const proxyquire = require('proxyquire');
15+
416
const service = require('../src/service');
17+
const db = require('../src/db');
18+
19+
const expect = chai.expect;
520

621
chai.use(chaiHttp);
7-
chai.should();
822

9-
// Use this test as a template
10-
describe('init', async () => {
23+
const TEST_REPO = {
24+
project: 'finos',
25+
name: 'db-test-repo',
26+
url: 'https://github.com/finos/db-test-repo.git',
27+
};
28+
29+
describe('init', () => {
1130
let app;
31+
let sandbox;
32+
33+
// Runs before all tests
1234
before(async function () {
13-
app = await service.start(); // pass in proxy if testing config loading or administration of proxy routes
35+
// Start the service (can also pass config if testing proxy routes)
36+
app = await service.start();
37+
});
38+
39+
// Runs before each test
40+
beforeEach(function () {
41+
// Create a sandbox for stubbing
42+
sandbox = sinon.createSandbox();
43+
44+
// Example: stub a DB method
45+
sandbox.stub(db, 'getRepo').resolves(TEST_REPO);
1446
});
1547

16-
it('should not be logged in', async function () {
48+
// Example test: check server is running
49+
it('should return 401 if not logged in', async function () {
1750
const res = await chai.request(app).get('/api/auth/profile');
51+
expect(res).to.have.status(401);
52+
});
53+
54+
// Example test: check db stub is working
55+
it('should get the repo from stubbed db', async function () {
56+
const repo = await db.getRepo('finos/db-test-repo');
57+
expect(repo).to.deep.equal(TEST_REPO);
58+
});
59+
60+
// Example test: use proxyquire to override the config module
61+
it('should return an array of enabled auth methods when overridden', async function () {
62+
const fsStub = {
63+
readFileSync: sandbox.stub().returns(
64+
JSON.stringify({
65+
authentication: [
66+
{ type: 'local', enabled: true },
67+
{ type: 'ActiveDirectory', enabled: true },
68+
{ type: 'openidconnect', enabled: true },
69+
],
70+
}),
71+
),
72+
};
73+
74+
const config = proxyquire('../src/config', {
75+
fs: fsStub,
76+
});
77+
config.initUserConfig();
78+
const authMethods = config.getAuthMethods();
79+
expect(authMethods).to.have.lengthOf(3);
80+
expect(authMethods[0].type).to.equal('local');
81+
expect(authMethods[1].type).to.equal('ActiveDirectory');
82+
expect(authMethods[2].type).to.equal('openidconnect');
83+
84+
// Clear config module cache so other tests don't use the stubbed config
85+
delete require.cache[require.resolve('../src/config')];
86+
});
1887

19-
res.should.have.status(401);
88+
// Runs after each test
89+
afterEach(function () {
90+
// Restore all stubs in this sandbox
91+
sandbox.restore();
2092
});
2193

94+
// Runs after all tests
2295
after(async function () {
2396
await service.httpServer.close();
2497
});

0 commit comments

Comments
 (0)