无法使Jasmine使用Babel 7,错误导入功能

问题描述 投票:0回答:1

我已经安装了Jasmine 3.5和Babel 7。节点版本为v12.14.0这是我的package.json:

{
  "name": "demo project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "babel index.js",
    "test": "jasmine --config=./spec/support/jasmine.json"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.10.1",
    "@babel/core": "^7.10.2",
    "@babel/plugin-transform-runtime": "^7.10.1",
    "@babel/preset-env": "^7.10.2",
    "@babel/register": "^7.10.1",
    "babel-eslint": "^10.1.0",
    "eslint-config-prettier": "^6.11.0",
    "eslint-plugin-jasmine": "^4.1.1",
    "eslint-plugin-prettier": "^3.1.3",
    "jasmine": "^3.5.0",
    "mongodb-memory-server": "^6.6.1",
    "prettier": "2.0.5",
    "prettier-standard": "^16.3.0"
  },
  "dependencies": {
    "@babel/runtime": "^7.10.2",
    "babel-preset-env": "^1.7.0",
    "mongodb": "^3.5.9",
    "web3": "^1.2.9"
  }
}

我的jasmine.json:

{
  "spec_dir": "src",
  "spec_files": ["**/?(*.)+(spec|test).[jt]s?(x)"],
  "helpers": ["helpers/**/*.js", "../node_modules/@babel/register/lib/node.js"],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

我的.babelrc:

{
  "presets": ["@babel/preset-env"],
  "plugins": [
      ["@babel/transform-runtime"]
  ]
}

我的createWallet.spec.js:

import createWallets from "./createWallet";
console.log({ createWallets })
describe("Tests", function () {
  it("Creates 1 wallet", async function () {
    createWallets(1);
    expect(true).toBe(true);
  });
});

createWallet.js:

"use strict";
import { web3 } from "./nodeConnection";

export function createWallet(amount) {
  const wallets = web3.eth.accounts.wallet.create(amount);
  console.log({ wallets });
}

当我运行“ npm测试”时,出现此错误:

> jasmine --config=./spec/support/jasmine.json

{ createWallets: undefined }
Randomized with seed 22172
Started
(node:57859) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
F

Failures:
1) Tests Creates 100 wallets and inserts them on db.
  Message:
    TypeError: (0 , _createWallet.default) is not a function
  Stack:
        at <Jasmine>
        at _callee2$ (/home/vallo/Proyectos/decrypto-erc20-collector/src/eth/createWallet.spec.js:19:21)
        at tryCatch (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
        at Generator.invoke [as _invoke] (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:274:22)
        at Generator.prototype.<computed> [as next] (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
        at asyncGeneratorStep (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
        at _next (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
        at /home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
        at <Jasmine>
        at UserContext.<anonymous> (/home/vallo/Proyectos/decrypto-erc20-collector/node_modules/@babel/runtime/helpers/asyncToGenerator.js:21:12)
        at <Jasmine>
        at processTicksAndRejections (internal/process/task_queues.js:93:5)

1 spec, 1 failure

我不明白为什么不导出createWallet或为什么它未定义。

可能还会在package.json上包含不必要的包。

javascript jasmine babel
1个回答
0
投票

将createWallet.js更改为

function createWallet(amount) {
  return web3.eth.accounts.wallet.create(amount);
}

export default createWallet;

添加到我的.babelrc文件:

["@babel/plugin-transform-modules-commonjs", {
  "allowTopLevelThis": true
}]

并安装了@ babel / plugin-transform-modules-commonjs`

© www.soinside.com 2019 - 2024. All rights reserved.