Skip to content

Commit 7d11afb

Browse files
committed
initial setup and version 1.0.0 complete
0 parents  commit 7d11afb

File tree

12 files changed

+3920
-0
lines changed

12 files changed

+3920
-0
lines changed

.eslintrc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true,
6+
"jest": true
7+
},
8+
"extends": "eslint:recommended",
9+
"parserOptions": {
10+
"ecmaVersion": 12
11+
},
12+
"rules": {
13+
}
14+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.vscode/launch.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"name": "Unit Test",
10+
"request": "launch",
11+
"program": "${workspaceFolder}/node_modules/.bin/jest",
12+
"args": [
13+
"--runInBand",
14+
"--watchAll=false"
15+
],
16+
"cwd": "${workspaceFolder}",
17+
"console": "integratedTerminal",
18+
"internalConsoleOptions": "neverOpen",
19+
"disableOptimisticBPs": true,
20+
"windows": {
21+
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
22+
}
23+
}
24+
]
25+
}

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# V School Code Talk Challenges
2+
3+
### About
4+
- A playground Node.js environment for testing and debugging pure, single functions.
5+
- Useful for running local debugger and creating custom test cases for code puzzles from Edabit, Code Wars, etc.
6+
7+
### Getting Started
8+
- `npm install`
9+
- `npm run test`
10+
- In VS Code, you can run the debugger using the Jest configuration called `Unit Test` listed in `.vscode/launch.json` file. Hotkey is F5. This will automatically read the `jest.config.js` file
11+
12+
### Creating Custom Challenges
13+
- Within `/challenges` directory, create a file which contains a default export of the function you wish to test. For example:
14+
```js
15+
// challenges/add_func.js
16+
module.exports = (x, y) => { return x + y };
17+
```
18+
19+
- In `test-cases.js` add a property (using same name as the file created above) to the default export object which defines an array containing the set of inputs and expected values. Each test case should look something like this:
20+
21+
```js
22+
// test-cases.js
23+
module.exports = {
24+
add_func: [
25+
[
26+
// arguments to be passed the 'add_func' function:
27+
[1, 2],
28+
// expected output of the 'add_func' function:
29+
3
30+
],
31+
[[0, 0], 0],
32+
[[-1, -2], -3],
33+
],
34+
// etc...
35+
}
36+
```
37+
38+
### Testing Single Challenges
39+
- To run tests for all cases defined for a given challenge, set the property `globals.testSingleChallenge` within `jest.config.js` to the name of the file you wish to test as defined in `/challenges` (omit the `.js` extension). For example:
40+
```js
41+
// jest.config.js
42+
module.exports = {
43+
globals: {
44+
testSingleChallenge: "example"
45+
},
46+
//...
47+
}
48+
```
49+
50+
### Testing Single Test Cases
51+
- To run a test on a single test case for a given challenge, set the property `globals.testSingleTestCase` within `jest.config.js` to the index of the test case you wish to try. The property `globals.testSingleChallenge` must also be defined. For example:
52+
```js
53+
// jest.config.js
54+
module.exports = {
55+
globals: {
56+
testSingleChallenge: "example",
57+
testSingleTestCase: 0
58+
},
59+
// ...
60+
}
61+
```
62+
63+
### [Changelog](./changelog.md)
64+
65+
### Author
66+
- Reach out to [Ben Turner](bbgrabbag@gmail.com) for any questions, bugs, or contribution requests.

challenges/example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = (val) => val;

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#### *v1.0.0* **10/3/2021**
2+
- Initial release
3+
- Introduced ability to test single challenges and test cases via Jest config

jest.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
module.exports = {
7+
// A set of global variables that need to be available in all test environments
8+
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+
11+
// testSingleChallenge: 'example',
12+
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.
14+
15+
// testSingleTestCase: 0
16+
},
17+
18+
// Automatically clear mock calls and instances between every test
19+
clearMocks: true,
20+
21+
// Indicates which provider should be used to instrument code for coverage
22+
coverageProvider: "v8",
23+
24+
};

main.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs');
2+
3+
const files = fs.readdirSync('./challenges');
4+
5+
files.forEach(file => {
6+
const challenge = file.replace('.js', '');
7+
module.exports[challenge] = require('./challenges/' + file);
8+
});

main.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { testCases } = require("./test-cases");
2+
const functions = require("./main");
3+
4+
const testCaseKeys = Object.keys(testCases);
5+
const functionKeys = Object.keys(functions);
6+
7+
try {
8+
for (const k of testCaseKeys) {
9+
if (!functionKeys.includes(k))
10+
throw Error(`No function found for test case '${k}'`);
11+
}
12+
for (const k of functionKeys) {
13+
if (!testCaseKeys.includes(k))
14+
throw Error(`No test case found for function '${k}()'`);
15+
const describer =
16+
global.testSingleChallenge === k
17+
? fdescribe
18+
: describe;
19+
describer(`Unit testing function '${k}()'`, () => {
20+
testCases[k].forEach(([inputs, expected], i) => {
21+
const runner =
22+
global.testSingleChallenge === k && global.testSingleTestCase === i
23+
? fit
24+
: it;
25+
runner(`Should output correct value for test case '${i}'`, () => {
26+
expect(functions[k](...inputs)).toEqual(expected);
27+
});
28+
});
29+
});
30+
}
31+
} catch (e) {
32+
console.error(e.message);
33+
}

0 commit comments

Comments
 (0)