多级导入/导出

时间:2018-09-08 13:26:53

标签: javascript typescript

我有一个用javascript完成的自动化测试套件。我正在将项目从javascript转换为打字稿。在原始项目中,结构如下:

  

助手

     
    

实用程序

         
      

销售力量

             
        

navigation.js

      
    
  
     

索引

     
    

utilityIndex.js

  
     

测试

     
    

myExampleTest.js

  

navigation.js

var files = require('../../../indices/fileIndex.js');
var utility = require('../../../indices/utilityIndex.js);
var pageObjectsSF = require('../../../indices/pageObjectSFIndex.js');

module.exports = {
  "NavigateToAccountFromContact": function (browser) {
    browser
      .click(pageObjectsSF.contact.details.detailsTab.selector, pageObjectsSF.contact.details.detailsTab.type, `Click Details Tab`)
      .click(pageObjectsSF.contact.details.accountName.selector, pageObjectsSF.contact.details.accountName.type, `Click Account Name`);
      utilities.generic.logging();
  }
}

已被转换为:navigation.ts

    import * as files from '../../../indices/fileIndex';
    import * as utilities from '../../../indices/utilityIndex';
    import * as pageObjectsSF from '../../../indices/pageObjectSFIndex';

    export = {
      "NavigateToAccountFromContact": function (browser) {
        browser
          .click(pageObjectsSF.contact.details.detailsTab.selector, pageObjectsSF.contact.details.detailsTab.type, `Click Details Tab`)
          .click(pageObjectsSF.contact.details.accountName.selector, pageObjectsSF.contact.details.accountName.type, `Click Account Name`);
           utilities.generic.logging();
      }
    }

utilityIndex.js

exports.navigation = require('../helpers/utilities/salesforce/navigation.ts');
exports.dataValidation = require('../helpers/utilities/salesforce/dataValidation');
exports.search = require('../helpers/utilities/salesforce/search');
exports.generic = require('../helpers/utilities/salesforce/generic');

myExampleTest.ts

//Declare File Dependencies
var files = require('../../indices/fileIndex.js');
var utilities = require('../../indices/utilityIndex.js');
var query = require('../../indices/queryIndex.js');

module.exports = {
  '@tags': ['Tags'],
  'TestName': function (browser) { 
    //Test Logic Here
  }
};

已转换为:myExampleTest.ts

//Declare File Dependencies
import * as files from '../../indices/fileIndex';
import * as utilities from '../../indices/utilityIndex';
import * as query from '../../indices/queryIndex';

export = {

  '@tags': ['Tags'],
  'TestName': function (browser) { 
    //Test Logic Here
  }
};

我尚未将utilityIndex转换为打字稿文件,但是当我尝试运行文件时,出现以下错误:

C:\GitRepos\RegressionTesting\helpers\utilities\salesforce\navigation.ts:1
(function (exports, require, module, __filename, __dirname) { import * as files from '../../../indices/fileIndex';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:152:10)
    at Module._compile (module.js:605:28)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (C:\GitRepos\RegressionTesting\indices\utilityIndex.js:1:84)

最初在UtilityIndex.js中,而不是:exports.navigation = require('../helpers/utilities/salesforce/navigation.ts');

我有exports.navigation = require('../helpers/utilities/salesforce/navigation');

但得到Error: Cannot find module 'indices/utilityIndex'

2 个答案:

答案 0 :(得分:0)

您不能直接使用Node运行TypeScript文件。使用tsc将其编译为JavaScript或使用ts-node之类的运行时。

答案 1 :(得分:0)

最终导致与'../helpers/utilities/salesforce/navigation.ts'关联的js文件没有正确的路径。一旦我解决问题就解决了。谢谢