Skip to content

Commit c9829cd

Browse files
committed
docs: add testing.mdx with unit testing section
1 parent 0240ea8 commit c9829cd

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Testing
3+
---
4+
5+
## Testing
6+
7+
As of v1.19.2, GitProxy uses [Mocha](https://mochajs.org/) (`ts-mocha`) as the test runner, and [Chai](https://www.chaijs.com/) for unit test assertions. End-to-end (E2E) tests are written in [Cypress](https://docs.cypress.io), and some fuzz testing is done with [`fast-check`](https://fast-check.dev/).
8+
9+
### Unit testing with Mocha and Chai
10+
11+
Here's an example unit test that uses Chai for testing (`test/testAuthMethods.test.js`):
12+
13+
```js
14+
// Import all the test dependencies we need
15+
const chai = require('chai');
16+
const sinon = require('sinon');
17+
const proxyquire = require('proxyquire');
18+
19+
// Import module that contains the function we want to test
20+
const config = require('../src/config');
21+
22+
// Allows using chain-based expect calls
23+
chai.should();
24+
const expect = chai.expect;
25+
26+
describe('auth methods', async () => {
27+
it('should return a local auth method by default', async function () {
28+
const authMethods = config.getAuthMethods();
29+
expect(authMethods).to.have.lengthOf(1);
30+
expect(authMethods[0].type).to.equal('local');
31+
});
32+
33+
it('should return an error if no auth methods are enabled', async function () {
34+
const newConfig = JSON.stringify({
35+
authentication: [
36+
{ type: 'local', enabled: false },
37+
{ type: 'ActiveDirectory', enabled: false },
38+
{ type: 'openidconnect', enabled: false },
39+
],
40+
});
41+
42+
const fsStub = {
43+
existsSync: sinon.stub().returns(true),
44+
readFileSync: sinon.stub().returns(newConfig),
45+
};
46+
47+
const config = proxyquire('../src/config', {
48+
fs: fsStub,
49+
});
50+
51+
// Initialize the user config after proxyquiring to load the stubbed config
52+
config.initUserConfig();
53+
54+
expect(() => config.getAuthMethods()).to.throw(Error, 'No authentication method enabled');
55+
});
56+
57+
it('should return an array of enabled auth methods when overridden', async function () {
58+
const newConfig = JSON.stringify({
59+
authentication: [
60+
{ type: 'local', enabled: true },
61+
{ type: 'ActiveDirectory', enabled: true },
62+
{ type: 'openidconnect', enabled: true },
63+
],
64+
});
65+
66+
const fsStub = {
67+
existsSync: sinon.stub().returns(true),
68+
readFileSync: sinon.stub().returns(newConfig),
69+
};
70+
71+
const config = proxyquire('../src/config', {
72+
fs: fsStub,
73+
});
74+
75+
// Initialize the user config after proxyquiring to load the stubbed config
76+
config.initUserConfig();
77+
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+
});
85+
```
86+
87+
Core concepts to keep in mind when unit testing JS/TS modules with Chai:

0 commit comments

Comments
 (0)