From c1fcde7d6f7d61f3038f6170cbf3d5d8034953f8 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 26 Mar 2020 13:31:07 +0100 Subject: [PATCH] killing mutants --- index.js | 14 ++++---- test/main.js | 44 +++++++++++++++++++++++--- test_files/root.sub/all-root.sub.js | 4 +-- test_files/root.sub/delete-root.sub.js | 4 +-- test_files/root.sub/get-root.sub.js | 4 +-- test_files/root.sub/not-root.sub.js | 4 +-- test_files/root.sub/post-root.sub.js | 4 +-- test_files/root.sub/put-root.sub.js | 4 +-- test_files/root/all-root.js | 4 +-- test_files/root/delete-root.js | 4 +-- test_files/root/get-root.js | 4 +-- test_files/root/not-root.js | 4 +-- test_files/root/post-root.js | 4 +-- test_files/root/put-root.js | 4 +-- test_files/sub/all-sub.js | 4 +-- test_files/sub/all-sub.root.js | 4 +-- test_files/sub/delete-sub.js | 4 +-- test_files/sub/get-sub.js | 4 +-- test_files/sub/get-sub.lv1.lv2.lv3.js | 4 +-- test_files/sub/not-sub.js | 4 +-- test_files/sub/post-sub.js | 4 +-- test_files/sub/put-sub.js | 4 +-- test_files/throw/all-root.js | 12 +++++++ 23 files changed, 79 insertions(+), 71 deletions(-) create mode 100644 test_files/throw/all-root.js diff --git a/index.js b/index.js index 81f8efa..5069658 100644 --- a/index.js +++ b/index.js @@ -39,13 +39,13 @@ function get_handler ({ module_folder, file, opts, rethrow }) { // eslint-disable-next-line global-require const handler = require (path.join (process.cwd (), module_folder, file)); - return (req, res, next) => { - new Promise ((resolve) => resolve (handler (req, res, next, opts))) - .catch ((e) => { - if (rethrow) - throw e; - }); - }; + return (req, res, next) => new Promise ( + (resolve) => resolve (handler (req, res, next, opts)) + ) + .catch ((e) => { + if (rethrow) + throw e; + }); } diff --git a/test/main.js b/test/main.js index 1acca47..4c3ce39 100644 --- a/test/main.js +++ b/test/main.js @@ -30,7 +30,7 @@ const mock = { } }; -test ('detect requests on root', (t) => { +test ('detect requests on root', async (t) => { mock.registered = {}; requestor (mock, './test_files/root'); const keys = [ @@ -42,9 +42,14 @@ test ('detect requests on root', (t) => { ]; t.deepEqual (Object.keys (mock.registered), keys); + const res = await Promise.all ( + Object.values (mock.registered) + .map ((val) => val ()) + ); + t.is (res.filter ((val) => val === 'dummy').length, keys.length); }); -test ('detect requests on root.subfolder', (t) => { +test ('detect requests on root.subfolder', async (t) => { mock.registered = {}; requestor (mock, './test_files/root.sub'); const keys = [ @@ -56,9 +61,14 @@ test ('detect requests on root.subfolder', (t) => { ]; t.deepEqual (Object.keys (mock.registered), keys); + const res = await Promise.all ( + Object.values (mock.registered) + .map ((val) => val ()) + ); + t.is (res.filter ((val) => val === 'dummy').length, keys.length); }); -test ('detect requests on subfolder', (t) => { +test ('detect requests on subfolder', async (t) => { mock.registered = {}; requestor (mock, './test_files/sub'); const keys = [ @@ -72,9 +82,14 @@ test ('detect requests on subfolder', (t) => { ]; t.deepEqual (Object.keys (mock.registered), keys); + const res = await Promise.all ( + Object.values (mock.registered) + .map ((val) => val ()) + ); + t.is (res.filter ((val) => val === 'dummy').length, keys.length); }); -test ('build requests with subdirectory', (t) => { +test ('build requests with subdirectory', async (t) => { mock.registered = {}; requestor (mock, './test_files/sub', { subdir: 'test' }); const keys = [ @@ -88,4 +103,25 @@ test ('build requests with subdirectory', (t) => { ]; t.deepEqual (Object.keys (mock.registered), keys); + const res = await Promise.all ( + Object.values (mock.registered) + .map ((val) => val ()) + ); + t.is (res.filter ((val) => val === 'dummy').length, keys.length); +}); + +test ('should rethrow', async (t) => { + mock.registered = {}; + requestor (mock, './test_files/throw'); + + const [ func ] = Object.values (mock.registered); + await t.throwsAsync (func); +}); + +test ('should not rethrow', async (t) => { + mock.registered = {}; + requestor (mock, './test_files/throw', { rethrow: false }); + + const [ func ] = Object.values (mock.registered); + await t.notThrowsAsync (func); }); diff --git a/test_files/root.sub/all-root.sub.js b/test_files/root.sub/all-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/all-root.sub.js +++ b/test_files/root.sub/all-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root.sub/delete-root.sub.js b/test_files/root.sub/delete-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/delete-root.sub.js +++ b/test_files/root.sub/delete-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root.sub/get-root.sub.js b/test_files/root.sub/get-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/get-root.sub.js +++ b/test_files/root.sub/get-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root.sub/not-root.sub.js b/test_files/root.sub/not-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/not-root.sub.js +++ b/test_files/root.sub/not-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root.sub/post-root.sub.js b/test_files/root.sub/post-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/post-root.sub.js +++ b/test_files/root.sub/post-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root.sub/put-root.sub.js b/test_files/root.sub/put-root.sub.js index 520d02c..67bfdf2 100644 --- a/test_files/root.sub/put-root.sub.js +++ b/test_files/root.sub/put-root.sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/all-root.js b/test_files/root/all-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/all-root.js +++ b/test_files/root/all-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/delete-root.js b/test_files/root/delete-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/delete-root.js +++ b/test_files/root/delete-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/get-root.js b/test_files/root/get-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/get-root.js +++ b/test_files/root/get-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/not-root.js b/test_files/root/not-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/not-root.js +++ b/test_files/root/not-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/post-root.js b/test_files/root/post-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/post-root.js +++ b/test_files/root/post-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/root/put-root.js b/test_files/root/put-root.js index 520d02c..67bfdf2 100644 --- a/test_files/root/put-root.js +++ b/test_files/root/put-root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/all-sub.js b/test_files/sub/all-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/all-sub.js +++ b/test_files/sub/all-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/all-sub.root.js b/test_files/sub/all-sub.root.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/all-sub.root.js +++ b/test_files/sub/all-sub.root.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/delete-sub.js b/test_files/sub/delete-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/delete-sub.js +++ b/test_files/sub/delete-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/get-sub.js b/test_files/sub/get-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/get-sub.js +++ b/test_files/sub/get-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/get-sub.lv1.lv2.lv3.js b/test_files/sub/get-sub.lv1.lv2.lv3.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/get-sub.lv1.lv2.lv3.js +++ b/test_files/sub/get-sub.lv1.lv2.lv3.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/not-sub.js b/test_files/sub/not-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/not-sub.js +++ b/test_files/sub/not-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/post-sub.js b/test_files/sub/post-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/post-sub.js +++ b/test_files/sub/post-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/sub/put-sub.js b/test_files/sub/put-sub.js index 520d02c..67bfdf2 100644 --- a/test_files/sub/put-sub.js +++ b/test_files/sub/put-sub.js @@ -7,6 +7,4 @@ 'use strict'; -module.exports = () => { - // dummy endpoint: do nothing -}; +module.exports = () => 'dummy'; diff --git a/test_files/throw/all-root.js b/test_files/throw/all-root.js new file mode 100644 index 0000000..c8a04c1 --- /dev/null +++ b/test_files/throw/all-root.js @@ -0,0 +1,12 @@ +/* + * Copyright (C) Sapphirecode - All Rights Reserved + * This file is part of Requestor which is released under BSD-3-Clause. + * See file 'LICENSE' for full license details. + * Created by Timo Hocker , March 2020 + */ + +'use strict'; + +module.exports = () => { + throw new Error ('foo'); +};