2016-10-21 15:48:52 +00:00
|
|
|
/* ------------------------------------------
|
|
|
|
* This module tests the compiled TypeScript files
|
|
|
|
* -------------------------------------------- */
|
2018-04-08 23:03:39 +00:00
|
|
|
import plugins = require('./mod.plugins');
|
|
|
|
import paths = require('../npmts.paths');
|
2016-10-02 18:35:13 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
import * as q from 'smartq';
|
2016-10-02 18:35:13 +00:00
|
|
|
|
2017-07-28 15:16:14 +00:00
|
|
|
// interfaces
|
2018-04-08 23:03:39 +00:00
|
|
|
import { INpmtsConfig } from '../npmts.config';
|
|
|
|
import { Smartfile } from 'smartfile';
|
2016-03-26 13:08:48 +00:00
|
|
|
|
2017-10-05 12:58:49 +00:00
|
|
|
let testTypeScriptConfig = {
|
|
|
|
target: 'ES5',
|
|
|
|
emitDecoratorMetadata: true,
|
|
|
|
experimentalDecorators: true,
|
2018-04-08 23:03:39 +00:00
|
|
|
lib: ['DOM', 'ESNext']
|
|
|
|
};
|
2017-10-05 12:58:49 +00:00
|
|
|
|
2016-03-29 23:32:41 +00:00
|
|
|
/**
|
2016-09-25 18:22:10 +00:00
|
|
|
* runs mocha
|
|
|
|
* @returns INpmtsConfig
|
2016-03-29 23:32:41 +00:00
|
|
|
*/
|
2018-04-08 23:03:39 +00:00
|
|
|
let tap = function(configArg: INpmtsConfig) {
|
|
|
|
let done = q.defer();
|
2016-11-25 12:03:41 +00:00
|
|
|
|
2017-03-04 20:49:10 +00:00
|
|
|
/**
|
|
|
|
* the TabBuffer for npmts
|
|
|
|
*/
|
2018-04-08 23:03:39 +00:00
|
|
|
let npmtsTapBuffer = new plugins.tapbuffer.TabBuffer();
|
2016-11-25 12:03:41 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
npmtsTapBuffer.setConfig(configArg.testConfig);
|
2017-06-17 08:47:07 +00:00
|
|
|
|
2017-03-04 20:49:10 +00:00
|
|
|
/**
|
|
|
|
* handle the testable files
|
|
|
|
*/
|
|
|
|
let testableFilesSmartstream = new plugins.smartstream.Smartstream([
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.smartgulp.src([plugins.path.join(paths.cwd, './ts/**/*.ts')]),
|
2017-03-04 20:49:10 +00:00
|
|
|
plugins.gulpSourcemaps.init(),
|
2017-10-05 12:58:49 +00:00
|
|
|
plugins.gulpTypeScript(testTypeScriptConfig),
|
2017-05-13 08:21:55 +00:00
|
|
|
plugins.gulpSourcemaps.write(),
|
2017-03-04 20:49:10 +00:00
|
|
|
npmtsTapBuffer.pipeTestableFiles(),
|
|
|
|
plugins.smartstream.cleanPipe()
|
2018-04-08 23:03:39 +00:00
|
|
|
]);
|
2017-03-04 20:49:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* handle the test files
|
|
|
|
*/
|
|
|
|
let testFilesSmartstream = new plugins.smartstream.Smartstream([
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.smartgulp.src([plugins.path.join(paths.cwd, 'test/*.ts')]),
|
2017-05-13 08:21:55 +00:00
|
|
|
plugins.gulpSourcemaps.init(),
|
2017-10-05 12:58:49 +00:00
|
|
|
plugins.gulpTypeScript(testTypeScriptConfig),
|
2017-05-13 08:21:55 +00:00
|
|
|
plugins.gulpSourcemaps.write(),
|
2017-03-04 20:49:10 +00:00
|
|
|
npmtsTapBuffer.pipeTestFiles(),
|
|
|
|
plugins.smartstream.cleanPipe()
|
2018-04-08 23:03:39 +00:00
|
|
|
]);
|
2017-03-04 20:49:10 +00:00
|
|
|
|
|
|
|
// lets run the smartstream
|
2018-04-08 23:03:39 +00:00
|
|
|
Promise.all([testableFilesSmartstream.run(), testFilesSmartstream.run()]).then(
|
2017-03-04 20:49:10 +00:00
|
|
|
async () => {
|
2018-04-08 23:03:39 +00:00
|
|
|
configArg.runData.coverageLcovInfo = await npmtsTapBuffer.runTests();
|
|
|
|
done.resolve(configArg);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
plugins.beautylog.error('Tests failed!');
|
|
|
|
console.log(err);
|
2017-03-04 20:49:10 +00:00
|
|
|
if (configArg.watch) {
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(configArg);
|
2017-03-04 20:49:10 +00:00
|
|
|
} else {
|
2018-04-08 23:03:39 +00:00
|
|
|
process.exit(1);
|
2017-03-04 20:49:10 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
}
|
|
|
|
);
|
2017-03-04 20:49:10 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|
2016-02-09 04:39:31 +00:00
|
|
|
|
2017-03-31 17:18:18 +00:00
|
|
|
let handleCoverageData = async (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
let coverageResult: number = 0; // the coverage in percent
|
2017-05-04 22:01:21 +00:00
|
|
|
if (configArg.runData.coverageLcovInfo) {
|
|
|
|
coverageResult = await plugins.smartcov.get.percentageFromLcovString(
|
|
|
|
configArg.runData.coverageLcovInfo,
|
|
|
|
2
|
2018-04-08 23:03:39 +00:00
|
|
|
);
|
2017-05-04 22:01:21 +00:00
|
|
|
} else {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(
|
|
|
|
'Hey... Did your tests import and use your module that you are trying to test?'
|
|
|
|
);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 17:18:18 +00:00
|
|
|
if (coverageResult >= configArg.coverageTreshold) {
|
2017-03-04 20:49:10 +00:00
|
|
|
plugins.beautylog.ok(
|
2018-04-08 23:03:39 +00:00
|
|
|
`${coverageResult.toString()}% ` +
|
|
|
|
`coverage exceeds your treshold of ` +
|
|
|
|
`${configArg.coverageTreshold.toString()}%`
|
|
|
|
);
|
2017-03-04 20:49:10 +00:00
|
|
|
} else {
|
|
|
|
plugins.beautylog.warn(
|
2018-04-08 23:03:39 +00:00
|
|
|
`${coverageResult.toString()}% ` +
|
|
|
|
`coverage fails your treshold of ` +
|
|
|
|
`${configArg.coverageTreshold.toString()}%`
|
|
|
|
);
|
|
|
|
plugins.beautylog.error('exiting due to coverage failure');
|
|
|
|
if (!configArg.watch) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2017-03-04 20:49:10 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
return configArg;
|
|
|
|
};
|
2016-03-29 23:32:41 +00:00
|
|
|
|
2018-05-03 10:35:41 +00:00
|
|
|
/**
|
|
|
|
* run this module
|
|
|
|
* @param configArg some config for how to run this module
|
|
|
|
*/
|
2018-04-08 23:03:39 +00:00
|
|
|
export let run = function(configArg: INpmtsConfig) {
|
|
|
|
let done = q.defer<INpmtsConfig>();
|
|
|
|
let config = configArg;
|
2017-03-04 20:49:10 +00:00
|
|
|
if (config.test === true) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.ora.text('now starting tests');
|
|
|
|
plugins.beautylog.ora.end();
|
|
|
|
plugins.beautylog.log('ready for tapbuffer:');
|
2017-07-27 23:21:37 +00:00
|
|
|
if (configArg.testConfig.coverage) {
|
2017-06-16 14:08:36 +00:00
|
|
|
tap(config)
|
|
|
|
.then(handleCoverageData)
|
|
|
|
.then(() => {
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(config);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2017-06-16 14:08:36 +00:00
|
|
|
} else {
|
|
|
|
tap(config)
|
|
|
|
.then(() => {
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(config);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
2017-06-16 14:08:36 +00:00
|
|
|
}
|
2017-03-04 20:49:10 +00:00
|
|
|
} else {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.ora.end();
|
|
|
|
done.resolve(config);
|
2017-03-04 20:49:10 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
return done.promise;
|
|
|
|
};
|