Javascript 如何使用babel配置jest

Javascript 如何使用babel配置jest,javascript,jestjs,babel-jest,Javascript,Jestjs,Babel Jest,我正在配置使用jest和babel进行测试的简单项目。我如何让他们一起工作 我尝试过这里提到的说明:但无法完成 abc.test.js mathconsts.js package.json babel.config.js jest.config.js 发现错误: ● 测试套件无法运行 Jest遇到了意外的标记 这通常意味着您正试图导入Jest无法解析的文件,例如,它不是纯JavaScript。 默认情况下,如果Jest看到一个Babel配置,它将使用它来转换您的文件,忽略“node_module

我正在配置使用jest和babel进行测试的简单项目。我如何让他们一起工作

我尝试过这里提到的说明:但无法完成

abc.test.js

mathconsts.js

package.json

babel.config.js

jest.config.js

发现错误:

● 测试套件无法运行
Jest遇到了意外的标记
这通常意味着您正试图导入Jest无法解析的文件,例如,它不是纯JavaScript。
默认情况下,如果Jest看到一个Babel配置,它将使用它来转换您的文件,忽略“node_modules”。
以下是您可以做的:
•要转换某些“节点模块”文件,可以在配置中指定自定义“transformIgnorePatterns”。
•如果需要自定义转换,请在配置中指定“转换”选项。
•如果您只是想模拟您的非JS模块(例如二进制资产),您可以使用“moduleNameMapper”配置选项将其剔除。
您将在文档中找到这些配置选项的更多详细信息和示例:
https://jestjs.io/docs/en/configuration.html
细节:
/Users/tests/abc.test.js:1
({“Object.”:函数(模块,导出,require,uu dirname,uu filename,global,jest){import{PI}from./mathconts”;
^
SyntaxError:意外标记{
在ScriptTransformer._transformAndBuildScript(节点_modules/@jest/transform/build/ScriptTransformer.js:537:17)
在ScriptTransformer.transform(node_modules/@jest/transform/build/ScriptTransformer.js:579:25)

如何使其运行?

我无法通过复制和粘贴您上面提供的代码来重现您的问题。您提到的所有文件是否都位于同一目录中?您使用什么命令来运行单元测试?
abc.test.js
mathconts.js
位于名为tests的文件夹中。我正在使用
npm test
>我使用的是节点版本
v10.16.3
您是否复制了位于的配置@DanOsandbox。下载该配置作为zip,解压缩,
npm安装
npm测试
import {PI} from "./mathconsts";

describe("tests",()=>{
    test('assert pi', () => {
        expect(PI).toBe(3.14);
    });
});
export const PI = 3.14;
{
  "name": "simple-testing",
  "version": "1.0.0",
  "description": "Project to write simple tests",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "axios": "0.19.0",
    "dotenv": "8.1.0"
  },
  "devDependencies": {
    "@babel/core": "^7.6.4",
    "@babel/preset-env": "^7.6.3",
    "babel-jest": "^24.9.0",
    "jest": "24.9.0",
    "prettier": "1.18.2"
  }
}
module.exports = {
    presets: [
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current',
                },
            },
        ],
    ],
};
module.exports = {
  transformIgnorePatterns: [
    "/node_modules/"
  ],
}
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/tests/abc.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { PI } from "./mathconsts";
                                                                                                    ^

    SyntaxError: Unexpected token {

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)