【问题标题】:Using es6 on mocha tests在 mocha 测试中使用 es6
【发布时间】:2016-11-08 06:14:08
【问题描述】:

我正在尝试将 es6 合并到我的服务器端代码中。在运行服务器时使用 babel-node 有效,但是在运行 mocha 测试时我无法将 es6 编译为 es5 代码。

这是我的文件夹结构

我有一个 server.js,它启动了一个 worker.js 文件(它有 express 服务器)

server.js 文件

import {SocketCluster} from 'socketcluster';

const socketCluster = new SocketCluster({
  workers:1,
  brokers:1,
  port: 3000,
  appName:null,
  workerController: __dirname + '/worker.js',
  brokerController: __dirname + '/broker.js',
  socketChannelLimit: 1000,
  crashWorkerOnError: true
})

worker.js 文件

export const run = (worker) => {
  console.log(' >> worker PID: ',process.pid);

  const app = express();

  const httpServer = worker.httpServer;
  const scServer = worker.scServer;

  app.use(cookieParser())

  httpServer.on('request', app);

  app.get('/',(req,res) => {
    console.log('recieved')
    res.send('Hello world')
  })

}

手动运行服务器时,它与以下脚本一起工作

"start": "nodemon server/server.js --exec babel-node"

但是,当我尝试使用 mocha 运行测试文件时,我得到一个“意外的令牌“导出”错误”

(function (exports, require, module, __filename, __dirname) { export const run = (broker) => {
                                                              ^^^^^^
SyntaxError: Unexpected token export
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:511:25)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at initBrokerServer (/home/avernus/Desktop/projects/node-sc-react/node_modules/sc-broker/server.js:178:25)
    at process.<anonymous> (/home/avernus/Desktop/projects/node-sc-react/node_modules/sc-broker/server.js:498:9)

这是启动 mocha 测试的脚本

"test": "mocha test/server/*.js --compilers js:babel-register"

我还错过了什么吗?

这是测试文件

import server from '../../server/server';
import http from 'http';
import assert from 'assert';
import {expect} from 'chai';

describe('Express server',() =>{
  it('should return "Hello World"',() => {
    http.get('http://127.0.0.1:3000',(res) => {
      expect(res).to.contain('wtf world')
    })
  })
})

【问题讨论】:

    标签: javascript node.js express ecmascript-6 mocha.js


    【解决方案1】:

    您需要使用Babel 将测试脚本从ES2015 转换为ES5,然后再将其传递给mocha 以运行测试。您可以按如下方式在package.json中添加/编辑测试脚本

    ...
    "scripts": {
      "test": "mocha --compilers js:babel-core/register --recursive"
    },
    ...
    

    更新:

    Mocha 已弃用 --compiler 标志。请查看this page 了解更多信息。新的 npm 脚本应如下所示

    ...
    "scripts": {
      "test": "mocha --require babel-register --recursive"
    },
    ...
    

    【讨论】:

    • 仍然收到错误:( .. 添加 --recursive 似乎不起作用
    • @Kunkka 我有点惊讶。我正在使用它,它只是工作。请检查this package.json
    • 因为 npm start 似乎可以使用相同的来源..您认为我还有什么可能错的?我认为 babel 没有正确编译进一步的导入
    • 问题可能是 server.js 需要 worker.js 文件的路径,而不是“需要”模块。所以也许 babel-register 不会跟踪它。但这意味着 npm start 也不应该正常工作?
    • @Kunkka 请添加 .babelrc 作为项目github.com/nmrony/hlsdownloader/blob/master/.babelrc 有它
    【解决方案2】:

    原来我需要在我的 server.js 文件中指定一个initController 以确保所有文件都由 babel 编译。这是我正在使用的 websocket 框架特有的问题。

    【讨论】:

      猜你喜欢
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 2015-08-24
      • 1970-01-01
      • 2016-05-18
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多