2017-09-11 67 views
2

我正在尝试为我的第一个Vue.js项目添加Foundation for Sites项目,该项目使用Vue CLI进行设置。噶单元测试Vue.js +基金会

网站运行然而噶+ Phantomjs单元测试套件,发射这样的错误:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR 
    SyntaxError: Invalid character: '`' 
    at webpack:///~/foundation-sites/js/foundation.util.core.js:24:0 <- index.js:10362 

我认为这是有关的WebPack /巴贝尔和基金会的ES模块加载。我不太清楚如何进一步诊断和解决问题。

这里是我所做的代码更改的概述...

Main.js

import jQuery from 'jquery' 
window.jQuery = jQuery 
window.$ = jQuery 

import Foundation from 'foundation-sites' 
window.Foundation = Foundation 

Hello.vue

<template> ... </template> 

<script> 
export default { 
    name: 'hello', 
    mounted() { 
    this.dropdownMenu = new Foundation.DropdownMenu($('#dropdown-menu'), { 
     // These options can be declarative using the data attributes 
     hoverDelay: 300 
    }) 
    }, 
    data() { 
    return { 
     msg: 'Welcome to Your Vue.js App' 
    } 
    }, 
    destroyed() { 
    this.dropdownMenu.destroy() 
    } 
} 
</script> 

测试/单元/index.js

import Vue from 'vue' 
import Foundation from 'foundation-sites' 
window.Foundation = Foundation 

// ... auto-generated unit tests 

测试/单位/ karma.conf.js

// This is a karma config file. For more details see 
// http://karma-runner.github.io/0.13/config/configuration-file.html 
// we are also using it with karma-webpack 
// https://github.com/webpack/karma-webpack 

var webpackConfig = require('../../build/webpack.test.conf') 

module.exports = function (config) { 
    config.set({ 
    // to run in additional browsers: 
    // 1. install corresponding karma launcher 
    // http://karma-runner.github.io/0.13/config/browsers.html 
    // 2. add it to the `browsers` array below. 
    browsers: ['PhantomJS'], 
    frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'], 
    reporters: ['spec', 'coverage'], 
    files: ['./index.js'], 
    preprocessors: { 
     './index.js': ['webpack', 'sourcemap'] 
    }, 
    webpack: webpackConfig, 
    webpackMiddleware: { 
     noInfo: true 
    }, 
    coverageReporter: { 
     dir: './coverage', 
     reporters: [ 
     { type: 'lcov', subdir: '.' }, 
     { type: 'text-summary' } 
     ] 
    } 
    }) 
} 

webpack.base.conf.js

var path = require('path') 
var utils = require('./utils') 
var config = require('../config') 
var vueLoaderConfig = require('./vue-loader.conf') 

function resolve (dir) { 
    return path.join(__dirname, '..', dir) 
} 

module.exports = { 
    entry: { 
    app: './src/main.js' 
    }, 
    output: { 
    path: config.build.assetsRoot, 
    filename: '[name].js', 
    publicPath: process.env.NODE_ENV === 'production' 
     ? config.build.assetsPublicPath 
     : config.dev.assetsPublicPath 
    }, 
    resolve: { 
    extensions: ['.js', '.vue', '.json'], 
    alias: { 
     'vue$': 'vue/dist/vue.esm.js', 
     '@': resolve('src'), 
    } 
    }, 
    module: { 
    rules: [ 
     { 
     test: /\.(js|vue)$/, 
     loader: 'eslint-loader', 
     enforce: 'pre', 
     include: [resolve('src'), resolve('test')], 
     options: { 
      formatter: require('eslint-friendly-formatter') 
     } 
     }, 
     { 
     test: /\.vue$/, 
     loader: 'vue-loader', 
     options: vueLoaderConfig 
     }, 
     { 
     test: /\.js$/, 
     loader: 'babel-loader', 
     include: [resolve('src'), resolve('test')] 
     }, 
     { 
     test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, 
     loader: 'url-loader', 
     options: { 
      limit: 10000, 
      name: utils.assetsPath('img/[name].[hash:7].[ext]') 
     } 
     }, 
     { 
     test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, 
     loader: 'url-loader', 
     options: { 
      limit: 10000, 
      name: utils.assetsPath('media/[name].[hash:7].[ext]') 
     } 
     }, 
     { 
     test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, 
     loader: 'url-loader', 
     options: { 
      limit: 10000, 
      name: utils.assetsPath('fonts/[name].[hash:7].[ext]') 
     } 
     } 
    ] 
    } 
} 

回答

0

我找到的解决方案是通过Babel加载Foundation。

webpack.base.conf.js

{ 
    test: /\.js$/, 
    loader: 'babel-loader', 
    include: [ 
    resolve('src'), 
    resolve('test'), 
    resolve('node_modules/foundation-sites') // Added this line. 
    ] 
}, 

造成这种情况的主要原因是在实际Foundation docs解释:

Our JavaScript uses some features of ECMAScript 2015. If you use Foundation with a build system, you'll need to compile our code to ES5. We use Babel in our templates to do this. Babel has plugins for every build system imaginable, so integrating it into an existing setup is easy.

基金会文档去推荐,包括和设置babel "presets": ["es2015"]但是Vue CLI已经设置稍有不同。

Vue CLI已经使用了babel-preset-envbabel-preset-stage2插件,我从那以后学习了ES2015> ES5转换。 Vue CLI使用多个加载程序设置webpack,因此似乎需要将Foundation添加到babel-loader配置中。

目前我正在导入基金会并将其添加到Vue的main.js入口点的全球window中。接下来,我可能会看看我是否可以使用Expose loader进行曝光。

0

我猜你是用巴贝尔transpiling和在你的webpack配置中定义这个。这种情况不会发生,因为在es5中不可用的内插字符串的反向错误上,这种情况不会发生。

假设您已经定义了karma.conf.js,您可能只需要将您的webpack配置添加到它。如果有的话,发布你的karma.conf.js也许很好。

我使用以下命令:

// karma.conf.js 
const webpackConfig = require('./build/webpack.base') 

delete webpackConfig.entry 

webpackConfig.resolve.alias.vue = 'vue/dist/vue.js' 

module.exports = function karmaConfig(config) { 
    config.set({ 
    browsers: ['PhantomJS'], 
    frameworks: ['mocha', 'chai'], 
    // this is the entry file for all our tests. 
    files: ['ui-tests/index.js'], 
    // we will pass the entry file to webpack for bundling. 
    preprocessors: { 
     'ui-tests/index.js': ['webpack'], 
    }, 
    // use the webpack config 
    webpack: webpackConfig, 
    // avoid walls of useless text 
    webpackMiddleware: { 
     noInfo: true, 
    }, 
    singleRun: true, 
    junitReporter: { 
     outputDir: '../../build/logs/', 
     outputFile: 'junit-js-ui-test-report.xml', 
     suite: 'review', 
     useBrowserName: false, 
     nameFormatter: undefined, 
     classNameFormatter: undefined, 
     properties: {}, 
    }, 
    }) 
} 

和:

// ui-tests/index.js 
const tests = require.context('..', true, /ui-tests\/.*\.spec$/) 
tests.keys().forEach(tests) 

,并在我的包脚本运行如下:

./node_modules/.bin/karma start karma.conf.js 

*******更新*****

不确定ho如果你正在处理你的babel预设,或许你正在使用babelrc,并且这没有找到(或者某种方式不在你的基本webpack配置中),但你可以尝试添加类似的东西,但是符合你的要求:

babel: { 
    babelrc: false, 
    presets: [ 
    ['es2015' {modules: false}], 
    'stage-1' 
] 
}, 

在构建/ webpack.test.conf您的WebPack配置...很难说,虽然我不知道你有你的test.conf

+0

Vue Cli设置了相当好的模块化业务和webpack配置。我刚刚添加了karma测试配置,它与您发布的示例看起来几乎完全相同。 我注意到webpack配置为Vue'esm'源提供了别名。我不确定我是否应该为基金会做类似的工作。 在问题中添加了karma测试配置和webpack-base配置。 –

+1

我刚刚添加了一个vue应用程序的基础,我有业力设置,并得到与你一样的错误。所以它不是特定于您的设置的东西。我有其他ES6进口工作正常 –

0

我有一个类似的问题了不同的软件包,我发现覆盖代码负责它。

在测试/单元开放index.js和评论这2行:

const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/) 
srcContext.keys().forEach(srcContext) 

这应该解决的问题。

+0

感谢您的建议。不幸的是,我仍然看到与这些注释相同的错误。 –