简体   繁体   English

WebdriverIO 和 Angular,在导入类的打字稿中编写 e2e 测试(不能在模块外使用 import 语句)

[英]WebdriverIO and Angular, writing e2e tests in typescript that import classes (Cannot use import statement outside a module)

I am trying to get a wdio set of e2e tests working.我正在尝试让一组 wdio 的 e2e 测试工作。 Some of the tests use some utility classes written in typescript.一些测试使用一些用打字稿编写的实用程序类。

When the test is being compiled it is falling over this error:编译测试时,它会遇到此错误:

Spec file(s): D:\TEMP\xx\angular-wdio6-builder-demo\e2e\test\specs\basic.spec.ts                                                                               
 Error:  D:\TEMP\xx\angular-wdio6-builder-demo\e2e\test\specs\basic.spec.ts:1                                                                                  
import {Util} from '../util/util.spec';                                                                                                                        
^^^^^^                                                                                                                                                         
                                                                                                                                                               
SyntaxError: Cannot use import statement outside a module                                                                                                      
    at wrapSafe (internal/modules/cjs/loader.js:1054:16)                                                                                                       
    at Module._compile (internal/modules/cjs/loader.js:1102:27)                                                                                                
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)                                                                                  
    at Module.load (internal/modules/cjs/loader.js:986:32)                                                                                                     
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)                                                                                           
    at Module.require (internal/modules/cjs/loader.js:1026:19)                                                                                                 
    at require (internal/modules/cjs/helpers.js:72:18)                                                                                                         
    at D:\TEMP\xx\angular-wdio6-builder-demo\node_modules\@wdio\jasmine-framework\node_modules\jasmine\lib\jasmine.js:89:5                                     
    at Array.forEach (<anonymous>)                                                                                                                             
    at Jasmine.loadSpecs (D:\TEMP\xx\angular-wdio6-builder-demo\node_modules\@wdio\jasmine-framework\node_modules\jasmine\lib\jasmine.js:88:18)                
[0-0] RUNNING in chrome - D:\TEMP\xx\angular-wdio6-builder-demo\e2e\test\specs\basic.spec.ts      

The above output is from a clone of one of the WebdriverIO Boilerplate Projects .以上输出来自WebdriverIO Boilerplate Projects之一的克隆。 The only change I made (apart from the chromedriver update) was to change the test in this sample to typescript and use a utility class.我所做的唯一更改(除了 chromedriver 更新)是将本示例中的测试更改为打字稿并使用实用程序类。

I have tried all the options I could find, but none of them fixed the issue, of just running this one simple test.我已经尝试了我能找到的所有选项,但没有一个能解决这个问题,只是运行这个简单的测试。 Especially, there seemed no way that any babel configuration was picked up.尤其是,似乎没有办法选择任何 babel 配置。

The source is located at https://github.com/rgansevles/angular-wdio6-builder-demo (clone from https://github.com/migalons/angular-wdio6-builder-demo )源位于https://github.com/rgansevles/angular-wdio6-builder-demo (从https://github.com/migalons/angular-wdio6-builder-demo克隆)

To reproduce, clone my repo and run:要重现,请克隆我的 repo 并运行:

npm install
npm run e2e

Has anyone an idea how to get this sample working with the import statement?有没有人知道如何让这个示例与 import 语句一起工作?

Thanks in advance,提前致谢,

Rob

Btw, this is the test file it fails on e2e/test/specs/basic.spec.ts :顺便说一句,这是它在 e2e/test/specs/basic.spec.ts 上失败的测试文件:

import {Util} from '../util/util.spec';

const util = new Util();

describe('webdriver.io page', () => {
  it('should have the right title', () => {
    browser.url('');
    const title = browser.getTitle();
    expect(title).toEqual(util.browserTitle);
  });

  it('should say app is running', () => {
    browser.url('');
    const message = $('body > app-root > div.content > div.card.highlight-card.card-small > span').getText();
    expect(message).toEqual('angular-wdio6-builder-demo app is running!');
  });
});

I've got it.我懂了。 Several things are to do:有几件事要做:

  • Create tsconfig.json within e2e-folder.在 e2e 文件夹中创建tsconfig.json This file have to extend the basic tsconfig.json and must overwrite:这个文件必须扩展基本的 tsconfig.json 并且必须覆盖:
    • "module": "commonjs"
    • "types": ["node", "@wdio/sync", "@wdio/jasmine-framework"]
  • Modify wdio.conf.js to get typescript compiled修改wdio.conf.js编译打字稿
    • Use ts instead of js in spec pattern在规范模式中使用ts而不是js
    • Add requires: ['ts-node/register'] to jasmineNodeOpts添加requires: ['ts-node/register']jasmineNodeOpts
  • Within package.json you have to add an environment to script e2epackage.json您必须向脚本e2e添加环境
    • "e2e": "TS_NODE_PROJECT=e2e/tsconfig.json ng e2e"

I`ve forked the same repo and did the changes here: https://github.com/Michel73/angular-wdio6-builder-demo我已经分叉了相同的 repo 并在此处进行了更改: https : //github.com/Michel73/angular-wdio6-builder-demo

One thing left: In my VSCode the basic.spec.ts shows compile errors, cause it seems that VSCode always takes the basic tsconfig.js .剩下的一件事:在我的 VSCode 中, basic.spec.ts显示编译错误,因为 VSCode 似乎总是采用基本的tsconfig.js Therefore I installed the TypeScript Auto Compiler -extension for VSCode.因此,我为 VSCode 安装了TypeScript Auto Compiler扩展。

Could you try adding babel setup to your test framework in wdio js file, For example:您可以尝试在 wdio js 文件中将 babel 设置添加到您的测试框架中,例如:

Mocha:摩卡:

mochaOpts: {
         ui: 'bdd',
        require: ['@babel/register', './test/helpers/common.js'],
        // ...
    },

Jasmine:茉莉花:

jasmineNodeOpts: {
        // Jasmine default timeout
        helpers: [require.resolve('@babel/register')],
        // ...
    },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM