Skip to content

Commit 73e862e

Browse files
committed
docs: update 1.test.js with stubbing and proxyquire examples
1 parent 7498ddd commit 73e862e

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

README.md

Lines changed: 2 additions & 0 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

test/1.test.js

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,98 @@
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+
const { readFileSync } = require('fs');
19+
20+
const expect = chai.expect;
521

622
chai.use(chaiHttp);
7-
chai.should();
823

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

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

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

95+
// Runs after all tests
2296
after(async function () {
2397
await service.httpServer.close();
2498
});

website/docs/development/testing.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ Here is an example of a function that resets both of these after each test (`tes
168168
const clearCache = (sandbox) => {
169169
delete require.cache[require.resolve('../src/proxy/processors')];
170170
delete require.cache[require.resolve('../src/proxy/chain')];
171-
sandbox.reset();
171+
sandbox.restore();
172172
};
173173

174174
...
@@ -212,6 +212,10 @@ expect(authMethods[0].type).to.equal('local');
212212

213213
If test coverage is still insufficient after writing your tests, check out the [CodeCov report](https://app.codecov.io/gh/finos/git-proxy) after making the PR and take a look at which lines are missing coverage.
214214

215+
#### More examples
216+
217+
Check out [test/1.test.js](https://github.com/finos/git-proxy/blob/main/test/1.test.js) for another example on how to write unit tests.
218+
215219
### UI testing with Cypress
216220

217221
Although coverage is currently low, we have introduced Cypress testing to make sure that end-to-end flows are working as expected with every added feature.

0 commit comments

Comments
 (0)