Skip to content

[Testing] Add jest to modules #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn test
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@ See wiki for details: https://github.com/source-academy/modules/wiki

1. Install a stable version of [NodeJS](https://nodejs.org/en/). The active LTS or current version should work fine.
2. Install a stable version of [Yarn](https://yarnpkg.com/).

```
$ npm install -g yarn
```

3. Clone this repository and navigate to it in your command line or shell tool.

```
$ git clone https://github.com/source-academy/modules.git
$ cd modules
```

4. Run `yarn install` to install dependencies.

### Serving the Modules' JavaScript Files

1. Run `yarn run build` to transpile the modules' files from `src` folder into `build` folder.
2. Run `yarn serve` to start the http-server on http://localhost:8022 to serve the `.js` files in `build` folder.
2. Run `yarn serve` to start the http-server on http://localhost:8022 to serve all files in the `build` folder with the same directory structure.

### Using with Cadet Frontend

1. Follow the [setup](https://github.com/source-academy/cadet-frontend#getting-started) instructions on Cadet Frontend.
2. When [setting up](https://github.com/source-academy/cadet-frontend#getting-started) the backend configuration of Cadet Frontend, ensure that `REACT_APP_MODULE_BACKEND_URL=http://localhost:8022` from the subsection above.
3. Ensure that you are serving the modules' javascript files in a local server. Refer to the subsection above for instructions.
4. [Start up](https://github.com/source-academy/cadet-frontend#getting-started) the Cadet Frontend local development server to use the served modules.
2. When [setting up](https://github.com/source-academy/cadet-frontend#getting-started) the backend configuration of Cadet Frontend, ensure that `REACT_APP_MODULE_BACKEND_URL=http://localhost:8022` from the subsection above.
3. Ensure that you are serving the modules' javascript files in a local server. Refer to the subsection above for instructions.
4. [Start up](https://github.com/source-academy/cadet-frontend#getting-started) the Cadet Frontend local development server to use the served modules.

## License

Expand Down
29 changes: 26 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,41 @@
"version": "1.0.0",
"repository": "https://github.com/source-academy/modules.git",
"license": "Apache-2.0",
"scripts-info": {
"module": "Add a new bundle or tab",
"lint": "Check code for errors",
"lint:fix": "Automatically fix lint errors",
"build": "Transpile modules' files from src folder to build folder",
"serve": "Start the http-server to serve all files in the build folder with the same directory structure",
"prepare": "Enable git hooks",
"test": "Run unit tests",
"test:watch": "Watch files for changes and rerun tests related to changed files"
},
"scripts": {
"module": "node ./templates/app.js",
"lint": "./node_modules/.bin/eslint --config ./.eslintrc.js --ext \".ts, .tsx\" src",
"lint:fix": "./node_modules/.bin/eslint --config ./.eslintrc.js --ext \".ts, .tsx\" src --fix",
"build": "yarn lint && ./node_modules/.bin/tsc && rollup --config rollup.config.js",
"serve": "http-server --cors=* -c-1 -p 8022 ./build",
"prepare": "husky install"
"prepare": "husky install",
"test": "jest --verbose",
"test:watch": "jest --watch"
},
"devDependencies": {
"@babel/cli": "^7.13.10",
"@babel/core": "^7.13.10",
"@babel/preset-env": "^7.13.10",
"@babel/core": "^7.13.13",
"@babel/preset-env": "^7.13.12",
"@babel/preset-react": "^7.12.13",
"@babel/preset-typescript": "^7.13.0",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-node-resolve": "^11.2.0",
"@rollup/plugin-typescript": "^8.2.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.14.34",
"@types/react": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^4.17.0",
"@typescript-eslint/parser": "^4.17.0",
"babel-jest": "^26.6.3",
"eslint": "^7.22.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.1.0",
Expand All @@ -37,13 +51,15 @@
"generate-template-files": "^3.0.0",
"http-server": "^0.12.3",
"husky": "5",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"rollup": "^2.41.2",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-inject-process-env": "^1.3.1",
"ts-jest": "^26.5.4",
"typescript": "^4.2.3",
"yarnhook": "^0.5.1"
},
Expand All @@ -60,5 +76,12 @@
},
"peerDependencies": {
"tslib": "^2.1.0"
},
"jest": {
"roots": [
"<rootDir>/src/bundles",
"<rootDir>/src/tabs",
"<rootDir>/templates"
]
}
}
17 changes: 17 additions & 0 deletions src/bundles/repeat/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Repeat from '../index';

// Destructure functions exported from bundle
const { repeat, twice, thrice } = Repeat();

// Test functions
test('repeat works correctly and repeats function n times', () => {
expect(repeat((x: number) => x + 1, 5)(1)).toBe(6);
});

test('twice works correctly and repeats function twice', () => {
expect(twice((x: number) => x + 1)(1)).toBe(3);
});

test('thrice works correctly and repeats function thrice', () => {
expect(thrice((x: number) => x + 1)(1)).toBe(4);
});
27 changes: 23 additions & 4 deletions src/bundles/repeat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,38 @@
* @author Tang Xin Kye, Marcus
*/

// eslint-disable-next-line no-underscore-dangle
function repeat(f: Function, n: any) {
/**
* Repeats function n times
*
* @param f - function to be repeated
* @param n - number of times to be repeated
* @return a function repeated n times
*/
function repeat(f: Function, n: any): Function {
return n === 0 ? (x: any) => x : (x: any) => f(repeat(f, n - 1)(x));
}

function twice(f: any) {
/**
* Repeats function twice
*
* @param f - function to be repeated
* @return a function repeated twice
*/
function twice(f: Function): Function {
return repeat(f, 2);
}

function thrice(f: any) {
/**
* Repeats function thrice
*
* @param f - function to be repeated
* @return a function repeated thrice
*/
function thrice(f: Function): Function {
return repeat(f, 3);
}

// exported functions from the bundle
export default () => ({
repeat,
twice,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/* The target setting changes which JS features are downleveled and which are left intact. */
"target": "es5",
/* By default all visible ”@types” packages are included in your compilation. */
"types": ["node"],
"types": ["node", "jest"],
/* In some cases where no type annotations are present, TypeScript will fall back to a type of any for a variable when it cannot infer the type. */
/* *** TEMPORARILY ADDED UNTIL ALL MODULES HAVE BEEN REFACTORED!!!!!!!!!!! *** */
"noImplicitAny": false,
Expand Down
Loading