Skip to content

Commit d457158

Browse files
committed
Squashed commit of the following:
commit 3d44f9682a47ff803117096f83c02745ab80598f Author: Ben Turner <bbgrabbag@gmail.com> Date: Thu Oct 7 13:23:28 2021 -0600 added custom tests
1 parent c961adc commit d457158

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = {
4141
// jest.config.js
4242
module.exports = {
4343
globals: {
44-
testSingleChallenge: "example"
44+
testSingleChallenge: "add_func"
4545
},
4646
//...
4747
}
@@ -53,14 +53,19 @@ module.exports = {
5353
// jest.config.js
5454
module.exports = {
5555
globals: {
56-
testSingleChallenge: "example",
56+
testSingleChallenge: "add_func",
5757
testSingleTestCase: 0
5858
},
5959
// ...
6060
}
6161
```
6262

63-
### [Changelog](./changelog.md)
63+
### Custom Tests
64+
- The default workflow in `main.test.js` may not work for some scenarios, so custom tests can be added to `custom.test.js`.
65+
- By default custom tests are switched off. To enable them, set the property `globals.skipCustomTests` within `jest.config.js` to `false`.
66+
- To run only custom tests, set the property `globals.runCustomTestsOnly` within `jest.config.js` to `true`. This will override the other settings.
6467

6568
### Author
6669
- Reach out to [Ben Turner](bbgrabbag@gmail.com) for any questions, bugs, or contribution requests.
70+
71+
### [Changelog](./changelog.md)

custom.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @fileoverview - Custom tests for special scenarios can be added here.
3+
*/
4+
5+
const { setCustomDescriber } = require("./utils");
6+
7+
const describer = setCustomDescriber();
8+
9+
/**************************************************************************************/
10+
describer("Custom Tests", () => {
11+
it("Should run custom test", () => {
12+
expect(true).toBe(true);
13+
});
14+
});

jest.config.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,29 @@
44
*/
55

66
module.exports = {
7-
// A set of global variables that need to be available in all test environments
87
globals: {
9-
// If defined, jest will only run tests on the function exported from this file (omit file extension) as defined in the directory '/challenges':
10-
8+
/*
9+
If defined, jest will only run tests on the function exported from this file (omit file extension) as defined in the directory '/challenges':
10+
*/
1111
// testSingleChallenge: 'example',
1212

13-
// If defined, jest will only run test on the specified test case index. Will do nothing if the above property 'testSingleChallenge' is not also defined.
13+
/*
14+
If defined, jest will only run test on the specified test case index. Will do nothing if 'testSingleChallenge' is not also defined.
15+
*/
16+
// testSingleTestCase: 0,
17+
18+
/*
19+
If set to 'true', only custom tests will run:
20+
*/
21+
runCustomTestsOnly: false,
1422

15-
// testSingleTestCase: 0
23+
/*
24+
If set to 'true', custom tests will not run. Will do nothing if 'runCustomTestsOnly' is set to 'true'
25+
*/
26+
skipCustomTests: true
1627
},
1728

18-
// Automatically clear mock calls and instances between every test
1929
clearMocks: true,
20-
21-
// Indicates which provider should be used to instrument code for coverage
2230
coverageProvider: "v8",
23-
31+
verbose: true,
2432
};

main.test.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { testCases } = require("./test-cases");
22
const functions = require("./main");
3+
const { setMainDescriber, setMainRunner } = require("./utils");
34

45
const testCaseKeys = Object.keys(testCases);
56
const functionKeys = Object.keys(functions);
@@ -12,16 +13,10 @@ try {
1213
for (const k of functionKeys) {
1314
if (!testCaseKeys.includes(k))
1415
throw Error(`No test case found for function '${k}()'`);
15-
const describer =
16-
global.testSingleChallenge === k
17-
? fdescribe
18-
: describe;
16+
const describer = setMainDescriber(k);
1917
describer(`Unit testing function '${k}()'`, () => {
2018
testCases[k].forEach(([inputs, expected], i) => {
21-
const runner =
22-
global.testSingleChallenge === k && global.testSingleTestCase === i
23-
? fit
24-
: it;
19+
const runner = setMainRunner(k, i);
2520
runner(`Should output correct value for test case '${i}'`, () => {
2621
expect(functions[k](...inputs)).toEqual(expected);
2722
});

utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
setCustomDescriber() {
3+
return global.runCustomTestsOnly
4+
? fdescribe
5+
: global.skipCustomTests
6+
? xdescribe
7+
: describe;
8+
},
9+
setMainDescriber(challenge) {
10+
return global.runCustomTestsOnly
11+
? xdescribe
12+
: global.testSingleChallenge === challenge
13+
? fdescribe
14+
: describe;
15+
},
16+
setMainRunner(challenge, testCaseIndex) {
17+
return global.testSingleChallenge === challenge &&
18+
global.testSingleTestCase === testCaseIndex
19+
? fit
20+
: it;
21+
}
22+
};

0 commit comments

Comments
 (0)