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 Middleware from '../src/middleware';
describe('@latus/core', () => {
describe('middleware', () => {
it('should construct with middleware', (done) => {
let called = false;
describe('middleware', () => {
it('should construct with middleware', (done) => {
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([
(next) => {
called = true;
progress += 1;
next();
},
(error, next) => {
throw new Error();
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch(() => {
expect(called).to.be.true;
middleware.dispatch((error) => {
expect(error).to.equal(undefined);
expect(progress).to.equal(2);
done();
});
});
it('should pass args', (done) => {
it('should use when necessary', (done) => {
let progress = 0;
const middleware = new Middleware([
(arg, next) => {
expect(arg).to.equal(69);
(next) => {
progress += 1;
next(new Error());
},
(error, next) => {
next(error);
},
(next) => {
progress += 1;
next();
},
]);
middleware.dispatch(69, done);
});
describe('error handling', () => {
it('should skip when necessary', (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();
});
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.source = 'client';
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';
neutrino.options.output = join(
isAbsolute(output)
@ -85,11 +88,11 @@ const client = {
'};',
].join('\n'));
const testPaths = paths
.map((path) => Latus.runtimePath(`${path}/test`))
.filter((path) => !!path);
plugin.writeModule(`node_modules/@latus/core/tests`, [
testPaths.map((path) => `require('${path}')`),
].join('\n'));
.map((path) => [path, Latus.runtimePath(`${path}/test`)])
.filter(([, path]) => !!path);
plugin.writeModule(`node_modules/@latus/core/tests`, testPaths.map(
([original, path]) => `describe('${original}', () => require('${path}'));`
).join(''));
});
}