Skip to content

Instantly share code, notes, and snippets.

@diveddie
Created February 8, 2020 19:25
Show Gist options
  • Save diveddie/207b62c1dfdf9ee82188a0d09040f8d5 to your computer and use it in GitHub Desktop.
Save diveddie/207b62c1dfdf9ee82188a0d09040f8d5 to your computer and use it in GitHub Desktop.
Code Test for Theorem

This project was created using the Webstorm Node boilerplate, but you should be able to get it up and running in pretty much any code editor. I used Node v10.15.3. The only dependency for this project is Jasmine, which is what I used for the unit testing. Here is a step-by-step to get up and running to test this project.

To get started with Jasmine, taken from (https://jasmine.github.io/pages/getting_started.html):

  • Add Jasmine to your package.json: npm install --save-dev jasmine

  • Initialize Jasmine in your project: node node_modules/jasmine/bin/jasmine init

  • Set jasmine as your test script in your package.json (If you copied the package.json file from the gist you should be ok): "scripts": { "test": "jasmine" }

  • To get Jasmine up and running quickly I ran the following command, this should give you the spec directory: jasmine examples

  • I renamed the playerSpec.js file to testSpec.js.

  • Deleted everything in that file and replace with the contents from the testSpec.js file from the gist.

  • If you don't already have an index.js file in your project, create it.

  • Once you have an index.js file, copy and paste the contents of the index.js file from the gist into your index.js file.

  • To run the index.js file from the command line, make sure you are in the root directory and run node index.js

  • If you need to debug the output, you can replace the return statement with console.log([].concat.apply([],tempArray));

  • To run the unit test from the command line, use npm test.

  • If you have any trouble with installing and testing this gist, please email eddie@divelement.io

Directory Structure should look something like this:

 .
 ├── node_modules                  // Node Modules library root
 ├── spec                          // Jasmine Config
      │   ├── jasmine_examples     // Directory that contains testSpec.js 
          │   ├── testSpec.js      // This is the unit test file (required for tests)
      │   ├── support              // Directory that contains default Jasmine config
          │   ├── jasmine.json     // This is the default Jasmine config (required)
 ├── index.js                      // Contains the flatten array function (required)
 ├── package.json   
 ├── package-lock.json             // npm lock file
 ├── README.md                     // this file
const array = [[1,2,[3]], 4];
module.exports = {
flattenArray: flattenArray = (a) => {
let tempArray = [].concat.apply([], a);
return [].concat.apply([],tempArray);
}
};
flattenArray(array);
{
"name": "theorem-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jasmine"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"jasmine": "^3.5.0"
}
}
describe("Flatten Array Test", function() {
let testInterface = require('../../test-interface');
let testFunction = testInterface.flattenArray;
let arrayToTest = [[1,2,[3]], 4];
let testArray = [1, 2, 3, 4];
it("should return a flat array", function() {
expect(testFunction(arrayToTest)).toEqual(testArray);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment