Home Manual Reference Source Test

test/integration/tasks/init.spec.js

  1. import expect from 'unexpected';
  2. import { spy } from 'sinon';
  3. import proxyquire from 'proxyquire';
  4. import { obj as createStream } from 'through2';
  5. import { src } from 'gulp';
  6. import handlebars from 'gulp-compile-handlebars';
  7. import { transform as babelTransform } from '@babel/core';
  8. import { transpileModule as tsTransform } from 'typescript';
  9. import evaluate from 'eval';
  10. import pkg from '../../../package.json';
  11. import Atviseproject from '../../../src/lib/config/Atviseproject';
  12.  
  13. const destSpy = spy((c, e, cb) => cb(null));
  14. const srcSpy = spy((c, e, cb) => cb(null, c));
  15.  
  16. const gulpStub = {
  17. src: (...args) => src(...args).pipe(createStream(srcSpy)),
  18. dest: () => createStream(destSpy),
  19. };
  20.  
  21. const handlebarsSpy = spy(handlebars);
  22.  
  23. const InitTask = proxyquire('../../../src/init/InitTask', {
  24. gulp: gulpStub,
  25. handlebars: {
  26. default: handlebarsSpy,
  27. },
  28. }).default;
  29.  
  30. /** @test {InitTask} */
  31. describe('InitTask', function () {
  32. const baseConfig = {
  33. name: 'unit-testing',
  34. atviseHost: 'localhost',
  35. atvisePortOpc: 4840,
  36. atvisePortHttp: 80,
  37. modulePackage: pkg,
  38. };
  39.  
  40. function optionsForLang(lang) {
  41. return Object.assign({ configLang: lang }, baseConfig);
  42. }
  43.  
  44. beforeEach(() => {
  45. srcSpy.resetHistory();
  46. destSpy.resetHistory();
  47. handlebarsSpy.resetHistory();
  48. });
  49.  
  50. function expectValidConfig(lang, transform) {
  51. it(`should create valid ${lang} config`, function () {
  52. return InitTask.run(optionsForLang(lang)).then(() => {
  53. const createdFiles = destSpy.args.map((args) => args[0]);
  54.  
  55. // Expect config file is created
  56. const configs = createdFiles.filter((file) => file.relative.match(/Atviseproject/));
  57. expect(configs, 'to have length', 1);
  58.  
  59. // Expect code can be transpiled
  60. const configCode = configs[0].contents.toString();
  61. let resultingCode;
  62. expect(
  63. () =>
  64. (resultingCode = transform(configCode, createdFiles).replace(
  65. /require\(['|"]atscm['|"]\)/,
  66. "require('../../../')"
  67. )),
  68. 'not to throw'
  69. );
  70.  
  71. // Expect transpiled code to be runnable
  72. let config;
  73. expect(() => (config = evaluate(resultingCode, true).default), 'not to throw');
  74.  
  75. // Expect config to extend Atviseproject
  76. expect(config, 'to have properties', Object.getOwnPropertyNames(Atviseproject));
  77. expect(config.name, 'to equal', 'UnitTesting');
  78. expect(config.host, 'to equal', baseConfig.atviseHost);
  79. expect(config.port.opc, 'to equal', baseConfig.atvisePortOpc);
  80. expect(config.port.http, 'to equal', baseConfig.atvisePortHttp);
  81. });
  82. });
  83. }
  84.  
  85. expectValidConfig('es2015', (code, createdFiles) => {
  86. const rcs = createdFiles.filter((file) => file.relative.match(/.babelrc/));
  87.  
  88. expect(rcs, 'to have length', 1);
  89.  
  90. const rc = JSON.parse(rcs[0].contents.toString());
  91.  
  92. return babelTransform(code, rc).code;
  93. });
  94.  
  95. expectValidConfig('es5', (code) => code);
  96.  
  97. expectValidConfig('ts', (code, createdFiles) => {
  98. const rcs = createdFiles.filter((file) => file.relative.match(/tsconfig.json/));
  99.  
  100. expect(rcs, 'to have length', 1);
  101.  
  102. const rc = JSON.parse(rcs[0].contents.toString());
  103.  
  104. return tsTransform(code, rc).outputText;
  105. });
  106. });