Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用VSCode调试器进行Jest测试会抛出错误“;SyntaxError:无法在模块外部使用导入语句;_Javascript_Ecmascript 6_Jestjs_Es6 Modules_Babel Jest - Fatal编程技术网

Javascript 使用VSCode调试器进行Jest测试会抛出错误“;SyntaxError:无法在模块外部使用导入语句;

Javascript 使用VSCode调试器进行Jest测试会抛出错误“;SyntaxError:无法在模块外部使用导入语句;,javascript,ecmascript-6,jestjs,es6-modules,babel-jest,Javascript,Ecmascript 6,Jestjs,Es6 Modules,Babel Jest,我已经尝试了很多事情,在这个阶段我可能会让事情变得更糟。有很多相关的问题,但没有解决我的问题。我显然做错了什么 我想在Jest测试中使用VSCode调试器。当我使用require&module.export时,我就可以使用它了。。使用导入导出切换到ES6模块时出现错误“SyntaxError:无法在模块外部使用导入语句” 我已经试着让它与babel Configuration SM一起工作,然后现在读到ES6模块受支持,我们不需要babel。我真的不介意我如何让它工作 典型的出口是 export

我已经尝试了很多事情,在这个阶段我可能会让事情变得更糟。有很多相关的问题,但没有解决我的问题。我显然做错了什么

我想在Jest测试中使用VSCode调试器。当我使用require&module.export时,我就可以使用它了。。使用导入导出切换到ES6模块时出现错误“SyntaxError:无法在模块外部使用导入语句”

我已经试着让它与babel Configuration SM一起工作,然后现在读到ES6模块受支持,我们不需要babel。我真的不介意我如何让它工作

典型的出口是

export default function buildIdentifiables(disposals, acquisitions) { 
典型导入(尝试使用和不使用.js)

My package.json

  "name": "cgc-node",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "jest"
  },
  "_moduleAliases": {
    "@": "src"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "babel-jest": "^26.1.0",
    "jest": "^26.1.0",
    "jest-environment-node": "^26.1.0"
  },
  "jest": {
    "testEnvironment": "node",
    "verbose": true,
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    }
  },
  "babel": {
    "presets": [
      [
        "@babel/env",
        {
          "targets": {
            "node": true
          }
        }
      ]
    ]
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "currency.js": "^2.0.0",
    "express": "^4.17.1",
    "lodash": "^4.17.19",
    "module-alias": "^2.2.2",
    "moment": "^2.27.0",
    "mysql": "github:mysqljs/mysql",
    "mysql2": "^2.1.0",
    "uuid": "^8.2.0"
  }
}
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/CGTC/cgc-node/main.js"
        },

        {
          "name": "Jest",
          "type": "node",
          "request": "launch",
          "env": { "NODE_ENV": "test" },
          "cwd": "${workspaceRoot}",
          "program": "${workspaceRoot}/CGTC/cgc-node/node_modules/.bin/jest",
          "stopOnEntry": false,
          "args": ["--config=${workspaceRoot}/CGTC/cgc-node/package.json"],
          "runtimeArgs": ["--nolazy"],
          "console": "internalConsole",
          "sourceMaps": false,
          "internalConsoleOptions": "openOnSessionStart",
          // "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node"
        }
    ]
}
Launch.json

  "name": "cgc-node",
  "type": "module",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "jest"
  },
  "_moduleAliases": {
    "@": "src"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "babel-jest": "^26.1.0",
    "jest": "^26.1.0",
    "jest-environment-node": "^26.1.0"
  },
  "jest": {
    "testEnvironment": "node",
    "verbose": true,
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    }
  },
  "babel": {
    "presets": [
      [
        "@babel/env",
        {
          "targets": {
            "node": true
          }
        }
      ]
    ]
  },
  "dependencies": {
    "body-parser": "^1.19.0",
    "currency.js": "^2.0.0",
    "express": "^4.17.1",
    "lodash": "^4.17.19",
    "module-alias": "^2.2.2",
    "moment": "^2.27.0",
    "mysql": "github:mysqljs/mysql",
    "mysql2": "^2.1.0",
    "uuid": "^8.2.0"
  }
}
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/CGTC/cgc-node/main.js"
        },

        {
          "name": "Jest",
          "type": "node",
          "request": "launch",
          "env": { "NODE_ENV": "test" },
          "cwd": "${workspaceRoot}",
          "program": "${workspaceRoot}/CGTC/cgc-node/node_modules/.bin/jest",
          "stopOnEntry": false,
          "args": ["--config=${workspaceRoot}/CGTC/cgc-node/package.json"],
          "runtimeArgs": ["--nolazy"],
          "console": "internalConsole",
          "sourceMaps": false,
          "internalConsoleOptions": "openOnSessionStart",
          // "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node"
        }
    ]
}
和babel.cofig.js

{
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true
        }
      }
    ]
  ]
}
让我知道,如果有什么额外的我可以张贴帮助


谢谢

最后,我无法完全“修复”我的问题(即使是在一个非常有知识的人的帮助下),我不得不重新开始一次干净的普通回购,只是粘贴我的文件

然而,我认为我的问题很大一部分是如何打开vscode。我打开了我的主“编码”文件夹(这样所有项目都可以一次看到),在所有这些项目之外有一个launch.json文件。由于launch.json文件指向一个特定的目录,我不确定这个“全局”启动文件是否可以工作

也许还有一种方法可以打开包含所有项目的文件夹,每个项目都有自己的launch.json文件,但我还没有让它工作

相反,当我导航到项目的根目录并在项目根目录下使用自己的launch.json文件
code.
时,它会工作

"version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Jest Tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/.bin/jest",
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "port": 9229
    }
  ]
}
package.json

    {
  "name": "node-jest-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "yarn jest"
  },

  "devDependencies": {
    "@babel/preset-env": "^7.11.0"
  },

  "jest": {
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    }
  },

  "dependencies": {
    "jest": "^26.2.2",

  }
}
babel.config.js

module.exports = (api) => {
  api.cache(false);

  const presets = [
    [
      "@babel/preset-env",
      {
        targets: {
          esmodules: true,
          node: "current",
        },
      },
    ],
  ];

  return {
    presets,
  };
};

最后,我无法完全“修复”我的问题(即使是在一个非常有知识的人的帮助下),我不得不从一个干净的普通回购开始,只是粘贴我的文件

然而,我认为我的问题很大一部分是如何打开vscode。我打开了我的主“编码”文件夹(这样所有项目都可以一次看到),在所有这些项目之外有一个launch.json文件。由于launch.json文件指向一个特定的目录,我不确定这个“全局”启动文件是否可以工作

也许还有一种方法可以打开包含所有项目的文件夹,每个项目都有自己的launch.json文件,但我还没有让它工作

相反,当我导航到项目的根目录并在项目根目录下使用自己的launch.json文件
code.
时,它会工作

"version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Jest Tests",
      "type": "node",
      "request": "launch",
      "runtimeArgs": [
        "--inspect-brk",
        "${workspaceRoot}/node_modules/.bin/jest",
        "--runInBand"
      ],
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "port": 9229
    }
  ]
}
package.json

    {
  "name": "node-jest-example",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "yarn jest"
  },

  "devDependencies": {
    "@babel/preset-env": "^7.11.0"
  },

  "jest": {
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    }
  },

  "dependencies": {
    "jest": "^26.2.2",

  }
}
babel.config.js

module.exports = (api) => {
  api.cache(false);

  const presets = [
    [
      "@babel/preset-env",
      {
        targets: {
          esmodules: true,
          node: "current",
        },
      },
    ],
  ];

  return {
    presets,
  };
};

我在尝试调试带有多个子包的monorepo中的Typescript Jest测试时遇到了同样的问题。我的错误是在根文件夹中启动VSCode。如果我只打开我想使用的子包文件夹,那么问题就消失了,我可以调试了。我在尝试调试包含多个子包的monorepo中的Typescript Jest测试时遇到了同样的问题。我的错误是在根文件夹中启动VSCode。如果我只打开我想在其中工作的子包文件夹,那么问题就消失了,我可以进行调试。