switch unit test framework to jest,

remove framework unittest,
add helper module to create unit testing environment
This commit is contained in:
Arthur Lu 2022-11-06 05:46:23 +00:00
parent d5a36a913e
commit ec305e667d
6 changed files with 127 additions and 169 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
**/.devcontainer/*
**/node_modules/*
**/package-lock.json

10
testenv/package.json Normal file
View File

@ -0,0 +1,10 @@
{
"name": "testenv",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"dependencies": {
"jest": "24"
}
}

28
testenv/testenv.js Normal file
View File

@ -0,0 +1,28 @@
module.exports = {environment};
function environment () {
const localStorageMock = (function () {
let store = {};
return {
getItem(key) {
return store[key];
},
setItem(key, value) {
store[key] = value;
},
clear() {
store = {};
},
removeItem(key) {
delete store[key];
},
getAll() {
return store;
},
};
})();
let window = {};
Object.defineProperty(window, "localStorage", { value: localStorageMock });
return window;
}

86
testenv/testenv.test.js Normal file
View File

@ -0,0 +1,86 @@
const {environment} = require("./testenv.js");
function saveToLocal (k, v) {
window.localStorage.setItem(k, v);
}
function getFromLocal (k) {
return window.localStorage.getItem(k);
}
function removeFromLocal (k) {
window.localStorage.removeItem(k);
}
function clearLocal () {
window.localStorage.clear();
}
describe("test localStorage mock", () => {
test("test save and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
expect(getFromLocal("testkey1")).toBe("testvalue1");
expect(getFromLocal("testkey2")).toBe("testvalue2");
expect(getFromLocal("testkey3")).toBe("testvalue3");
expect(getFromLocal("testkey4")).toBe("testvalue4");
saveToLocal("testkey6", "testvalue5");
expect(getFromLocal("testkey6")).toBe("testvalue5");
});
test("test delete and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
removeFromLocal("testkey3");
expect(getFromLocal("testkey1")).toBe("testvalue1");
expect(getFromLocal("testkey2")).toBe("testvalue2");
expect(getFromLocal("testkey3")).toBe(null);
expect(getFromLocal("testkey4")).toBe("testvalue4");
removeFromLocal("testkey1");
expect(getFromLocal("testkey1")).toBe(null);
expect(getFromLocal("testkey2")).toBe("testvalue2");
expect(getFromLocal("testkey3")).toBe(null);
expect(getFromLocal("testkey4")).toBe("testvalue4");
removeFromLocal("testkey4");
expect(getFromLocal("testkey1")).toBe(null);
expect(getFromLocal("testkey2")).toBe("testvalue2");
expect(getFromLocal("testkey3")).toBe(null);
expect(getFromLocal("testkey4")).toBe(null);
removeFromLocal("testkey2");
expect(getFromLocal("testkey1")).toBe(null);
expect(getFromLocal("testkey2")).toBe(null);
expect(getFromLocal("testkey3")).toBe(null);
expect(getFromLocal("testkey4")).toBe(null);
});
test("test delete and fetch", () => {
let window = environment();
saveToLocal("testkey1", "testvalue1");
saveToLocal("testkey2", "testvalue2");
saveToLocal("testkey3", "testvalue3");
saveToLocal("testkey4", "testvalue4");
clearLocal();
expect(getFromLocal("testkey1")).toBe(null);
expect(getFromLocal("testkey2")).toBe(null);
expect(getFromLocal("testkey3")).toBe(null);
expect(getFromLocal("testkey4")).toBe(null);
});
});

View File

@ -1,59 +0,0 @@
module.exports = {test, item, assert};
var itemsFailed;
/*
* Calls multiple item functions.
*
* param name string name of the test
* param items func callable function that contains item calls
*
* return bool whether the test passed or failed
*/
function test(name, items) {
console.log(name);
items();
}
/*
* Contain multiple assertions to test specific behavior
*
* param name string name of the specific item
* param items func multple assertion calls
*
* return bool whether the item passed or failed
*/
function item(name, method) {
try{
method();
console.log('\t\x1b[32m%s\x1b[0m', '\u2714 ' + name);
itemsFailed += 0;
return true;
}
catch (error) {
console.log('\t\x1b[31m%s\x1b[0m', '\u2718 ' + name);
itemsFailed += 1;
return false;
}
}
/*
* Given two inputs, comapres if they equivalent.
*
* param input any output of some function to be tested.
* param expected any expected output of the function
*
* return bool whether input = expected
*/
function assert(input, expected) {
let result;
if (typeof(input) === 'object') {
result = JSON.stringify(input) === JSON.stringify(expected);
}
else {
result = input === expected;
}
if(!result){
throw new Error();
}
}

View File

@ -1,110 +0,0 @@
const unittest = require("./unittest.js");
const localStorageMock = (function () {
let store = {};
return {
getItem(key) {
return store[key];
},
setItem(key, value) {
store[key] = value;
},
clear() {
store = {};
},
removeItem(key) {
delete store[key];
},
getAll() {
return store;
},
};
})();
let window = {};
Object.defineProperty(window, "localStorage", { value: localStorageMock });
function assertion_test () {
try { unittest.assert(1, 1); }
catch (error) { console.log("Failed assert(1,1)") }
try { unittest.assert(65536, 65536); }
catch (error) { console.log("Failed assert(655366,65536)") }
try { unittest.assert(-50, 50); console.log("Failed assert(-50,50)") }
catch (error) { }
try { unittest.assert(0, 1); console.log("Failed assert(0,1)") }
catch (error) { }
try { unittest.assert(true, true); }
catch (error) { console.log("Failed assert(true,true)") }
try { unittest.assert(false, false); }
catch (error) { console.log("Failed assert(false,false)") }
try { unittest.assert(true, false); console.log("Failed assert(true,false)") }
catch (error) { }
try { unittest.assert(false, true); console.log("Failed assert(false,true)") }
catch (error) { }
try { unittest.assert("Test", "Test"); }
catch (error) { console.log("Failed assert('Test','Test')") }
try { unittest.assert("Test", "Yay"); console.log("Failed assert('Test','Yay')") }
catch (error) { }
try { unittest.assert({one: 1, two: 2}, {one: 1, two: 2}); }
catch (error) { console.log("Failed assert({one: 1, two: 2},{one: 1, two: 2})") }
try { unittest.assert({one: 1, two: 2}, {two: 2, three: 3}); console.log("Failed assert({one: 1, two: 2}, {two: 2, three: 3})") }
catch (error) { }
}
assertion_test();
function item_test () {
unittest.item("passing item", () => {
unittest.assert(1, 1);
});
unittest.item("failing item", () => {
unittest.assert(1, 2);
});
}
item_test();
function test_test () {
unittest.test("example test", () => {
unittest.item("assert 1 = 1", () => {
unittest.assert(1, 1);
});
unittest.item("assert 1 = 2", () => {
unittest.assert(1, 2);
})
});
}
test_test();
function environment_test () {
function testfunc1 () {
window.localStorage.clear();
window.localStorage.setItem("testkey", "testvalue");
return window.localStorage.getItem("testkey");
}
function testfunc2 (){
window.localStorage.removeItem("testkey");
}
unittest.test("test add item", () => {
unittest.item("testfunc1", () => {
unittest.assert(testfunc1(), "testvalue");
});
});
unittest.test("test delete item", () => {
unittest.item("testfunc2", () => {
unittest.assert(testfunc2(), undefined);
});
});
}
environment_test();