Skip to content

Commit

Permalink
Add a script to run some automated tests
Browse files Browse the repository at this point in the history
Needs more tests! but this is a very good start
  • Loading branch information
pineapplemachine committed Mar 28, 2018
1 parent 68ce0d5 commit b93d3fd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,3 @@ typings/

# dotenv environment variables file
.env

# Project-specific
*test.js
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
docs/
*mkdocs.yml
*test.js
.gitignore
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"name": "Timo Rüppell",
"email": "timo.ruppell@mapita.fi"
}
]
],
"scripts": {
"test": "node test.js"
}
}
75 changes: 75 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const canary = require("./canary");
const assert = require("assert");

// Extremely basic test runner to test the test runner
const allTests = [];
function addTest(test){
allTests.push(test);
}
async function runTests(){
console.log(`Running ${allTests.length} tests...`);
for(let test of allTests){
try{
canary.removeAllTests();
canary.reset();
canary.silent();
await test();
}catch(error){
console.log("Error while running test:");
console.log(error);
process.exit(1);
}
}
console.log("All tests ran successfully!");
process.exit(0);
}

addTest(
async function simpleSynchronousTests(){
// Set up the test
const simplePassingTest = canary.test("Example passing test", function(){
// do nothing
});
const simpleSkippedTest = canary.test("Example skipped test", function(){
this.ignore();
});
const simpleFailingTest = canary.test("Example failing test", function(){
throw new Error("Simple test failure");
});
// Run canary
await canary.run();
// Verify correct results
assert(simplePassingTest.success);
assert(simpleSkippedTest.skipped);
assert(simpleFailingTest.aborted);
assert(simpleFailingTest.getErrors().length === 1);
assert(simpleFailingTest.getErrors()[0] instanceof canary.Error);
assert(simpleFailingTest.getErrors()[0].message === "Simple test failure");
}
);

addTest(
async function simpleAsynchronousTests(){
// Set up the test
const simplePassingTest = canary.test("Example passing test", async function(){
// do nothing
});
const simpleSkippedTest = canary.test("Example skipped test", async function(){
this.ignore();
});
const simpleFailingTest = canary.test("Example failing test", async function(){
throw new Error("Simple test failure");
});
// Run canary
await canary.run();
// Verify correct results
assert(simplePassingTest.success);
assert(simpleSkippedTest.skipped);
assert(simpleFailingTest.aborted);
assert(simpleFailingTest.getErrors().length === 1);
assert(simpleFailingTest.getErrors()[0] instanceof canary.Error);
assert(simpleFailingTest.getErrors()[0].message === "Simple test failure");
}
);

runTests();

0 comments on commit b93d3fd

Please sign in to comment.