使用Spectron时,如何在电子应用程序的main.js之前应用babel配置?

时间:2020-03-13 20:42:16

标签: babeljs spectron

我正在开玩笑地运行一个测试脚本,该脚本使用Spectron启动了Electron应用程序。加载babel配置的正确方法是什么?通常,我是这样启动应用程序的:npx electron -r @babel/register .(npm在package.json中启动)

我使用Spectron的测试脚本试图像这样初始化它:

import { Application } from 'spectron';
const app = new Application({
  path: 'node_modules/electron/dist/electron',
  args: ["-r @babel/register", "src/main.js"],
  chromeDriverArgs: ['--disable-dev-shm-usage', '--headless'],
  chromeDriverLogPath: 'chromeDriver.log'
})

但是main.js在第一个import语句上因SyntaxError崩溃。显然-r @babel/register未加载。

如果我在终端上手动从同一pathargs构造一条命令:

node_modules/electron/dist/electron -r @babel/register src/main.js

应用程序启动正常。但是查看chromeDriver日志后,我发现此命令可能与实际调用的命令不同。从日志中:

"chromeOptions": {
         "args": [ "spectron-path=node_modules/electron/dist/electron", "spectron-arg0=-r @babel/register", "spectron-arg1=src/main.js", "--disable-dev-shm-usage", "--headless" ]

我目前的变通方法是使用以下内容制作一个specmain.js脚本:

"use strict";
require("@babel/register");
require("./main")

然后我的测试脚本现在使用以下命令成功启动应用程序:

const app = new Application({
  path: 'node_modules/electron/dist/electron',
  args: ["src/specmain.js"],
  chromeDriverArgs: ['--disable-dev-shm-usage', '--headless'],
  chromeDriverLogPath: 'chromeDriver.log'
})

在Spectron的背景下,在@babel/register之前要求main.js的好方法是什么?

0 个答案:

没有答案