refactor: testing

This commit is contained in:
cha0s 2021-01-05 11:25:46 -06:00
parent 394a3038c4
commit 36fdb62bf5
2 changed files with 61 additions and 60 deletions

View File

@ -1,72 +1,70 @@
import {expect} from 'chai'; import {expect} from 'chai';
import Middleware from '../src/middleware'; import Middleware from '../src/middleware';
describe('@latus/core', () => { describe('middleware', () => {
describe('middleware', () => { it('should construct with middleware', (done) => {
it('should construct with middleware', (done) => { let called = false;
let called = false; const middleware = new Middleware([
(next) => {
called = true;
next();
},
]);
middleware.dispatch(() => {
expect(called).to.be.true;
done();
});
});
it('should pass args', (done) => {
const middleware = new Middleware([
(arg, next) => {
expect(arg).to.equal(69);
next();
},
]);
middleware.dispatch(69, done);
});
describe('error handling', () => {
it('should skip when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([ const middleware = new Middleware([
(next) => { (next) => {
called = true; progress += 1;
next();
},
(error, next) => {
throw new Error();
},
(next) => {
progress += 1;
next(); next();
}, },
]); ]);
middleware.dispatch(() => { middleware.dispatch((error) => {
expect(called).to.be.true; expect(error).to.equal(undefined);
expect(progress).to.equal(2);
done(); done();
}); });
}); });
it('should pass args', (done) => { it('should use when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([ const middleware = new Middleware([
(arg, next) => { (next) => {
expect(arg).to.equal(69); progress += 1;
next(new Error());
},
(error, next) => {
next(error);
},
(next) => {
progress += 1;
next(); next();
}, },
]); ]);
middleware.dispatch(69, done); middleware.dispatch((error) => {
}); expect(error).to.not.equal(undefined);
describe('error handling', () => { expect(progress).to.equal(1);
it('should skip when necessary', (done) => { done();
let progress = 0;
const middleware = new Middleware([
(next) => {
progress += 1;
next();
},
(error, next) => {
throw new Error();
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch((error) => {
expect(error).to.equal(undefined);
expect(progress).to.equal(2);
done();
});
});
it('should use when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([
(next) => {
progress += 1;
next(new Error());
},
(error, next) => {
next(error);
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch((error) => {
expect(error).to.not.equal(undefined);
expect(progress).to.equal(1);
done();
});
}); });
}); });
}); });

View File

@ -26,7 +26,10 @@ const client = {
neutrino.options.root = fs.realpathSync(root); neutrino.options.root = fs.realpathSync(root);
neutrino.options.source = 'client'; neutrino.options.source = 'client';
neutrino.options.mains.index = 'index'; neutrino.options.mains.index = 'index';
neutrino.options.mains.tests = {entry: './client/tests'}; neutrino.options.mains.tests = {
entry: './client/tests',
title: 'Mocha tests',
};
const output = 'build'; const output = 'build';
neutrino.options.output = join( neutrino.options.output = join(
isAbsolute(output) isAbsolute(output)
@ -85,11 +88,11 @@ const client = {
'};', '};',
].join('\n')); ].join('\n'));
const testPaths = paths const testPaths = paths
.map((path) => Latus.runtimePath(`${path}/test`)) .map((path) => [path, Latus.runtimePath(`${path}/test`)])
.filter((path) => !!path); .filter(([, path]) => !!path);
plugin.writeModule(`node_modules/@latus/core/tests`, [ plugin.writeModule(`node_modules/@latus/core/tests`, testPaths.map(
testPaths.map((path) => `require('${path}')`), ([original, path]) => `describe('${original}', () => require('${path}'));`
].join('\n')); ).join(''));
}); });
} }