Vue.js可以在e2e测试中导入模块吗?导入时出现语法错误

时间:2017-10-06 07:31:54

标签: vue.js nightwatch.js e2e-testing

我正在使用NightWatch框架运行我的e2e测试

在测试中,我需要检查浏览器显示的值以重置为定义的初始值,设置为从config.js文件导出的常量

但是我收到SYNTAX错误,是不是被接受了?

Starting selenium server... started - PID:  49634
There was an error while starting the test runner:
../test/e2e/specs/test.js:4
import { WORKING_TIME } from '@/config';
^^^^^^
SyntaxError: Unexpected token import    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at loader (../node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/../node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)

config.js

let coeff = process.env.NODE_ENV === 'testing' ? 0.5 : 1
export const WORKING_TIME = coeff * 60 

E2E / test.js

import { WORKING_TIME } from '@/config'

module.exports = { 
    'check STOP event': (browser) => {
    const devServer = browser.globals.devServerURL
    browser.url(devServer).waitForElementVisible('#app', 5000)
    browser.click('[title=start]').waitForElementVisible('i.toggle-volume', 5000)

    // CLICK on STOP button
    browser.click('[title=stop]').pause(1000)
    ...
    // Check timer is reset
    const leftPad = (str, length = 10) => {
      return `${' '.repeat(length)}${str}`
    }
    const min = Math.floor(WORKING_TIME / 60)
    const sec = WORKING_TIME % 60
    const initialTimer = leftPad(min, 2) + ':' + leftPad(sec, 2)
    browser.expect.element('.my-timer').text.to.equal(initialTimer)
    browser.end()
  }
}

1 个答案:

答案 0 :(得分:0)

正如@Omran所说,我遇到了ES6问题......

1 - 需要在导入之前插入(并且还要写一个正确的路径......)

<强> E2E / test.js

require('babel-core/register')
import { WORKING_TIME } from '../../../config'

2 - e2e test runner.js正在设置:

process.env.NODE_ENV = 'testing'

我应该把它添加到.babelrc

.babelrc

{
  "presets": [
    ["env", {
      "modules": false,
      "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"]
      }
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    },
    "testing": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }
}