Jest: Jestκ°€ React μ•±μ—μ„œ 예기치 μ•Šμ€ 토큰을 λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2018λ…„ 08μ›” 31일  Β·  42μ½”λ©˜νŠΈ  Β·  좜처: facebook/jest

μ‚¬λž‘ 제슀트? 우리 집단 지원을 κ³ λ €ν•˜μ‹­μ‹œμ˜€: πŸ‘‰ https://opencollective.com/jest/donate

πŸ› 버그 μ‹ κ³ 

React μ•±μ—μ„œ Jest둜 ν…ŒμŠ€νŠΈλ₯Ό μ‹€ν–‰ν•˜λ €κ³  ν•˜λ©΄ Jest encountered an unexpected token 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. Link ꡬ성 μš”μ†Œλ₯Ό λ³΅μ‚¬ν•˜κ³  here μ—μ„œ 직접 ν…ŒμŠ€νŠΈ

FAIL  src/components/Link.test.js
  ● 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:

    SyntaxError: C:\workspace\react\testapp\src\components\Link.test.js: Unexpected token (8:4)

       6 | test('Link changes the class when hovered', () => {
       7 |   const component = renderer.create(
    >  8 |     <Link page="http://www.facebook.com">Facebook</Link>,
         |     ^
       9 |   );
      10 |   let tree = component.toJSON();
      11 |   expect(tree).toMatchSnapshot();

      at Parser.raise (node_modules/@babel/parser/lib/index.js:3938:15)
      at Parser.unexpected (node_modules/@babel/parser/lib/index.js:5247:16)
      at Parser.parseExprAtom (node_modules/@babel/parser/lib/index.js:6327:20)
      at Parser.parseExprSubscripts (node_modules/@babel/parser/lib/index.js:5923:21)
      at Parser.parseMaybeUnary (node_modules/@babel/parser/lib/index.js:5902:21)
      at Parser.parseExprOps (node_modules/@babel/parser/lib/index.js:5811:21)
      at Parser.parseMaybeConditional (node_modules/@babel/parser/lib/index.js:5783:21)
      at Parser.parseMaybeAssign (node_modules/@babel/parser/lib/index.js:5730:21)
      at Parser.parseExprListItem (node_modules/@babel/parser/lib/index.js:6994:18)
      at Parser.parseCallExpressionArguments (node_modules/@babel/parser/lib/index.js:6123:22)

μ—¬κΈ° λ‚΄ package.json

{
  "name": "testapp",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "webpack-dev-server --config ./config/webpack.config.development.js",
    "build": "webpack -p --config ./config/webpack.config.production.js",
    "test": "jest"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "moment": "^2.22.2",
    "polished": "^2.0.3",
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-delay-render": "^0.1.2",
    "react-dom": "^16.4.2",
    "react-imported-component": "^4.6.2",
    "react-router-dom": "^4.3.1",
    "simple-grid": "^1.0.1",
    "styled-components": "^3.4.5",
    "uuid": "^3.3.2",
    "validator": "^10.7.0"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-core": "7.0.0-bridge.0",
    "babel-jest": "^23.4.2",
    "babel-loader": "^8.0.0",
    "babel-plugin-styled-components": "^1.6.0",
    "css-loader": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "jest": "^23.5.0",
    "mini-css-extract-plugin": "^0.4.2",
    "react-test-renderer": "^16.4.2",
    "regenerator-runtime": "^0.12.1",
    "style-loader": "^0.23.0",
    "webpack": "^4.17.1",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.7"
  }
}

μ—¬κΈ° λ‚΄ .babelrc 파일이 μžˆμŠ΅λ‹ˆλ‹€(루트 디렉토리에 있음).

{
  "plugins": [
    "@babel/plugin-proposal-class-properties",
    "babel-plugin-styled-components",
    "@babel/plugin-syntax-dynamic-import"
  ],
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "env": {
    "development": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        ["babel-plugin-styled-components", { "displayName": true }],
        "@babel/plugin-syntax-dynamic-import"
      ],
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    },
    "test": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        ["babel-plugin-styled-components", { "displayName": true }],
        "@babel/plugin-syntax-dynamic-import"
      ],
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    },
    "production": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        "babel-plugin-styled-components",
        "@babel/plugin-syntax-dynamic-import"
      ],
      "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
  }
}

μž¬ν˜„ν•˜κΈ° μœ„ν•΄

λ™μž‘μ„ μž¬ν˜„ν•˜λŠ” 단계:

  • yarn add --dev babel-jest babel-core@^7.0.0-0 @babel/core Jest μ„€μΉ˜
  • ν…ŒμŠ€νŠΈ λ§Œλ“€κΈ°
  • yarn test

μ˜ˆμƒλ˜λŠ” 행동

  • 농담이 μž‘λ™ν•΄μ•Όν•©λ‹ˆλ‹€

npx envinfo --preset jest

여기에 κ²°κ³Όλ₯Ό λΆ™μ—¬λ„£μŠ΅λ‹ˆλ‹€.

OS: Windows 10
    CPU: x64 Intel(R) Xeon(R) CPU E3-1505M v6 @ 3.00GHz
  Binaries:
    Yarn: 1.5.1 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 5.6.0 - C:\Program Files\nodejs\npm.CMD

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

3일 후에 이 λ¬Έμ œμ— λŒ€ν•œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.
이λ₯Ό μœ„ν•œ 2가지 μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€.

μ†”λ£¨μ…˜ 1:

파일 이름을 .babelrc μ—μ„œ babel.config.js λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.

## μ†”λ£¨μ…˜ 2:
이 방법을 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
λ‹€μŒκ³Ό 같은 λ³€ν™˜ νŒŒμΌμ„ λ§Œλ“­λ‹ˆλ‹€(λ‚΄ νŒŒμΌμ€ jest-transforme.js μž„).

const config = {
  babelrc: false,
  presets: [
    [
      "@babel/env",
      {
        modules: false
      }
    ],
    "@babel/react"
  ],
  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { loose: true }],
    "transform-es2015-modules-commonjs"
  ]
};
module.exports = require("babel-jest").createTransformer(config);

λ‚΄ jest.config.js λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

module.exports = {
  collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"],
  testMatch: ["<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}", "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"],
  transform: {
    "^.+\\.(js|jsx|mjs)$": "<rootDir>/config/jest/jest-transformer.js"
  },
  transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"]
};

그리고 아직 λ‚΄ .babelrc
```
const ν™˜κ²½ = require("./env-config.js");

module.exports = {
사전 μ„€μ •: [
[
"λ‹€μŒ/바벨",
{
"사전 μ„€μ • ν™˜κ²½": {
useBuiltIns: "ν•­λͺ©"
}
}
]
],
ν”ŒλŸ¬κ·ΈμΈ: [
["μŠ€νƒ€μΌ ꡬ성 μš”μ†Œ", { ssr: true, displayName: true, μ „μ²˜λ¦¬: false }],
[
"λͺ¨λ“ˆ 해결사",
{
루트: ["./"]
}
]
],
ν™˜κ²½: {
개발자: {
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }],
"λ³€ν˜•-λ°μ½”λ ˆμ΄ν„°-λ ˆκ±°μ‹œ",
"λ³€ν™˜ 클래슀 속성"
]
},
짓닀: {
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }],
"λ³€ν˜•-λ°μ½”λ ˆμ΄ν„°-λ ˆκ±°μ‹œ",
"λ³€ν™˜ 클래슀 속성"
]
},
생산: {
사전 μ„€μ •: [
[
"minify", // μ™œ 여기에 minifyλ₯Ό μΆ”κ°€ν•©λ‹ˆκΉŒ? 전체 μ½”λ“œ λ²ˆλ“€μ— λŒ€ν•΄ uglifyλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
{
λ§ΉκΈ€: 거짓,
평가: 거짓
}
]
],
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["@babel/plugin-proposal-decorators", { λ ˆκ±°μ‹œ: true }],
["@babel/plugin-proposal-class-properties", { λŠμŠ¨ν•œ: true }],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }]
],
λŒ“κΈ€: 거짓,
컴팩트: 사싀,
μΆ•μ†Œ: 사싀
}
}
};
````
이것이 μ—¬λŸ¬λΆ„μ΄ 이 문제λ₯Ό 톡과할 수 있기λ₯Ό λ°”λžλ‹ˆλ‹€. ν™˜ν˜Έ.

λͺ¨λ“  42 λŒ“κΈ€

λ‚˜λ„ 같은 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

λ™μΌν•œ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

λ™μΌν•œ λ¬Έμ œκ°€ 여기에 μžˆμŠ΅λ‹ˆλ‹€. monorepo둜 μž‘μ—…ν•  λ•Œ μ•½κ°„ 더 λ³΅μž‘ν•©λ‹ˆλ‹€.

@fabioSalimbeni @hoaiduyit @mikedloss λ‚΄ React 및 React Native ν…ŒμŠ€νŠΈ( Jest 및 enzyme )λ₯Ό Babel 7을 μ‚¬μš©ν•˜μ—¬ Lerna λͺ¨λ…Έλ ˆν¬μ—μ„œ ν†΅κ³Όμ‹œμΌ°μŠ΅λ‹ˆλ‹€.

λͺ¨λ…Έλ ˆν¬μ—μ„œ μž‘μ—…ν•˜λŠ” 경우 babel.config.js 와 ν•¨κ»˜ μƒˆ Babel ꡬ성을 μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€: https://babeljs.io/docs/en/v7-migration.

이것은 monorepo의 λ£¨νŠΈμ— λŒ€ν•œ λ‚΄ babel.config.js μž…λ‹ˆλ‹€.

module.exports = {
    overrides: [
        {
            test: 'platforms/webApp1',
            extends: 'platforms/webApp1/babel.config.js'
        },
        {
            test: 'platforms/webApp2',
            extends: 'platforms/webApp2/babel.config.js'
        }
    ]
};

이것이 webApp1 λŒ€ν•œ λ‚΄ babel.config.js λͺ¨μŠ΅μž…λ‹ˆλ‹€. monorepo μ•± 쀑 ν•˜λ‚˜:

module.exports = {
    env: {
        test: {
            presets: [
                [
                    '@babel/preset-env',
                    {
                        modules: 'commonjs',
                        debug: false
                    }
                ],
                '@babel/preset-flow',
                '@babel/preset-react'
            ],
            plugins: [
                '@babel/plugin-syntax-dynamic-import',
                '@babel/plugin-proposal-class-properties'
            ]
        },
        production: {
            presets: [
                ['@babel/preset-env', { modules: false }],
                '@babel/preset-flow',
                '@babel/preset-react'
            ],
            plugins: [
                '@babel/plugin-syntax-dynamic-import',
                '@babel/plugin-proposal-class-properties'
            ]
        },
        development: {
            presets: [
                ['@babel/preset-env', { modules: false }],
                '@babel/preset-flow',
                '@babel/preset-react'
            ],
            plugins: [
                '@babel/plugin-syntax-dynamic-import',
                '@babel/plugin-proposal-class-properties'
            ]
        }
    }
};

그리고 이것은 Jest κ΅¬μ„±μž…λ‹ˆλ‹€.

    "jest": {
        "verbose": true,
        "clearMocks": true,
        "collectCoverage": true,
        "setupTestFrameworkScriptFile": "<rootDir>/config/setupTest.js",
        "transform": {
            "^.+\\.js$": "babel-jest"
        },
        "moduleNameMapper": {
            "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
            "\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js"
        }
    }

setupTest.js λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');

Enzyme.configure({ adapter: new Adapter() });

fileMocks.js λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

module.exports = 'i-am-a-stubbed-file';

styleMocks.js λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

module.exports = {};

λ˜ν•œ 일뢀 Babel ν”ŒλŸ¬κ·ΈμΈκ³Ό babel-core 버전을 각 μ•±μ˜ package.json 에 devDependencies 둜 μΆ”κ°€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

        ...
        "@babel/cli": "^7.0.0",
        "@babel/core": "^7.0.0",
        "@babel/plugin-proposal-class-properties": "^7.0.0",
        "@babel/plugin-syntax-dynamic-import": "^7.0.0",
        "@babel/preset-env": "^7.0.0",
        "@babel/preset-flow": "^7.0.0",
        "@babel/preset-react": "^7.0.0",
        "babel-core": "7.0.0-bridge.0",
        "babel-eslint": "^9.0.0",
        "babel-jest": "^23.4.2",
        "babel-loader": "^8.0.2",
        ...
        "jest": "^23.5.0",
        "jest-cli": "^23.5.0",
        ...

3일 후에 이 λ¬Έμ œμ— λŒ€ν•œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.
이λ₯Ό μœ„ν•œ 2가지 μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€.

μ†”λ£¨μ…˜ 1:

파일 이름을 .babelrc μ—μ„œ babel.config.js λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.

## μ†”λ£¨μ…˜ 2:
이 방법을 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
λ‹€μŒκ³Ό 같은 λ³€ν™˜ νŒŒμΌμ„ λ§Œλ“­λ‹ˆλ‹€(λ‚΄ νŒŒμΌμ€ jest-transforme.js μž„).

const config = {
  babelrc: false,
  presets: [
    [
      "@babel/env",
      {
        modules: false
      }
    ],
    "@babel/react"
  ],
  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { loose: true }],
    "transform-es2015-modules-commonjs"
  ]
};
module.exports = require("babel-jest").createTransformer(config);

λ‚΄ jest.config.js λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

module.exports = {
  collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"],
  testMatch: ["<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}", "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"],
  transform: {
    "^.+\\.(js|jsx|mjs)$": "<rootDir>/config/jest/jest-transformer.js"
  },
  transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"]
};

그리고 아직 λ‚΄ .babelrc
```
const ν™˜κ²½ = require("./env-config.js");

module.exports = {
사전 μ„€μ •: [
[
"λ‹€μŒ/바벨",
{
"사전 μ„€μ • ν™˜κ²½": {
useBuiltIns: "ν•­λͺ©"
}
}
]
],
ν”ŒλŸ¬κ·ΈμΈ: [
["μŠ€νƒ€μΌ ꡬ성 μš”μ†Œ", { ssr: true, displayName: true, μ „μ²˜λ¦¬: false }],
[
"λͺ¨λ“ˆ 해결사",
{
루트: ["./"]
}
]
],
ν™˜κ²½: {
개발자: {
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }],
"λ³€ν˜•-λ°μ½”λ ˆμ΄ν„°-λ ˆκ±°μ‹œ",
"λ³€ν™˜ 클래슀 속성"
]
},
짓닀: {
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }],
"λ³€ν˜•-λ°μ½”λ ˆμ΄ν„°-λ ˆκ±°μ‹œ",
"λ³€ν™˜ 클래슀 속성"
]
},
생산: {
사전 μ„€μ •: [
[
"minify", // μ™œ 여기에 minifyλ₯Ό μΆ”κ°€ν•©λ‹ˆκΉŒ? 전체 μ½”λ“œ λ²ˆλ“€μ— λŒ€ν•΄ uglifyλ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
{
λ§ΉκΈ€: 거짓,
평가: 거짓
}
]
],
ν”ŒλŸ¬κ·ΈμΈ: [
["λ³€ν™˜ μ •μ˜", ν™˜κ²½],
["@babel/plugin-proposal-decorators", { λ ˆκ±°μ‹œ: true }],
["@babel/plugin-proposal-class-properties", { λŠμŠ¨ν•œ: true }],
["λͺ¨λ“ˆ 해석기", { 루트: ["./"] }]
],
λŒ“κΈ€: 거짓,
컴팩트: 사싀,
μΆ•μ†Œ: 사싀
}
}
};
````
이것이 μ—¬λŸ¬λΆ„μ΄ 이 문제λ₯Ό 톡과할 수 있기λ₯Ό λ°”λžλ‹ˆλ‹€. ν™˜ν˜Έ.

@GeorgianSorinMaxim λ¬Έμ œλŠ” λ‚΄ 농담 μ‘μš© ν”„λ‘œκ·Έλž¨ λ¬΄μ‹œμž…λ‹ˆλ‹€ .babelrc νŒŒμΌμ„, 그리고둜 λ³€κ²½ babel.config.js 문제λ₯Ό ν•΄κ²°ν•˜μ§€λ§Œ, μ–΄μ¨Œλ“  그것을 ν•΄κ²°, λ‚΄ λŒ€λ‹΅μ΄ λŒ“κΈ€μ˜ μ΄μƒμž…λ‹ˆλ‹€.

@GeorgianSorinMaxim @hoaiduyit 덕뢄에 κ²°κ΅­ babel-

Babel 7이 μ œλŒ€λ‘œ μž‘λ™ν•˜λ €λ©΄ babel.config.js κ°€ ν•„μš”ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€. babelrc λŒ€μ‹  μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

babel-jest μ—¬μ „νžˆ Babel 6의 babel-core μ˜μ‘΄ν•©λ‹ˆλ‹€. Babel 7 을 μ‚¬μš©ν•˜λ €λ©΄ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
이 λ³€μ••κΈ° babel7-jest

babel-jest λŠ” babel 7μ—μ„œ 잘 μž‘λ™ν•˜λ©° μ‚¬μš©μž μ •μ˜ λ³€ν™˜κΈ°κ°€ ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@SimenB λ‚΄ .babelrc 을 babel.config.js ν–ˆμ§€λ§Œ μ—¬μ „νžˆ 예기치 μ•Šμ€ 토큰과 ν•¨κ»˜ ν•΄λ‹Ή 였λ₯˜κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€. ν•˜μ§€λ§Œ babel7-pre.42와 ν•¨κ»˜ μž‘λ™ν•©λ‹ˆλ‹€. λ‹€λ₯Έ 일반적으둜 λ†“μΉ˜λŠ” 것듀이 μžˆμŠ΅λ‹ˆκΉŒ? :) 감사 ν•΄μš”.

@alexindigo babel.config.js μ“°λŠ” 방법을 ν™•μΈν•˜μ„Έμš”

https://babeljs.io/docs/en/configuration

예, yarn upgrade --latest μ‹€ν–‰ν•˜λ©΄ λ¬Έμ œκ°€ ν•΄κ²°λ˜μ—ˆμŠ΅λ‹ˆλ‹€. :)

@alexindigo @hoaiduyit babel.config.js λŒ€ν•΄ μ–΄λ–»κ²Œ μ•Œμ•„ λƒˆ λ‚˜μš”? 같은 λ¬Έμ œμ™€ μ”¨λ¦„ν•˜κ³  λ‹Ήμ‹ μ˜ 해결책을 μ°ΎλŠ” λ™μ•ˆ, λ‚˜λŠ” μ†μœΌλ‘œ μƒκ°ν•˜κ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€... _"흠, 이것이 μž‘λ™ν•  방법은 μ—†μ§€λ§Œ μ–΄μ¨Œλ“  μ‹œλ„ν•  κ²ƒμž…λ‹ˆλ‹€."_ ν•˜μ§€λ§Œ μΆ©λΆ„νžˆ lulzν–ˆμŠ΅λ‹ˆλ‹€. 감사 ν•΄μš”

Jest 및 νš¨μ†Œ ꡬ성 : package.json에 λ‹€μŒ μ½”λ“œ μΆ”κ°€

"jest": {
    "testEnvironment": "jsdom",
    "moduleDirectories": [
      "src",
      "node_modules"
    ],
    "moduleNameMapper": {
      "\\.(css|scss)$": "<rootDir>/__mocks__/styleMock.js",
      "\\.(jpg|gif|ttf|eot|svg)$": "<rootDir>/__mocks__/fileMock.js"
    },
    "transform": {
      "^.+\\.(js|jsx)$": "babel-jest",
      ".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform"
    },
    "setupTestFrameworkScriptFile": "<rootDir>/setupTests.js",
    "setupFiles": [
      "<rootDir>setup.js"
    ],
    "moduleFileExtensions": [
      "css",
      "scss",
      "js",
      "json",
      "jsx"
    ],
    "testRegex": "\/test\/spec\/.*\\.js$",
    "transformIgnorePatterns": [
      "/node_modules/(?!test-component).+\\.js$"
    ]
  }

Enzyme => setup.js μ„€μ •μš©

import { configure } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
configure({ adapter: new Adapter() })

setupTestFrameworkScriptFile의 경우: setupTests.js global.fetch = require('jest-fetch-mock')

const { JSDOM } = require('jsdom')
const jsdom = new JSDOM('<!doctype html><html><body></body></html>')
const { window } = jsdom

const exposedProperties = ['window', 'navigator', 'document']
const { document } = new JSDOM('').window
global.document = document
global.window = document.defaultView
global.HTMLElement = window.HTMLElement
global.HTMLAnchorElement = window.HTMLAnchorElement

Object.keys(document.defaultView).forEach(property => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property)
    global[property] = document.defaultView[property]
  }
})

global.navigator = {
  userAgent: 'node.js',
}

.tsx 파일만 λ™μΌν•œ λ¬Έμ œμ— μ§λ©΄ν•©λ‹ˆκΉŒ?

방금 λ‰΄μŠ€λ₯Ό νΌλœ¨λ¦¬λ©΄μ„œ Jest v24λŠ” λ‚΄λΆ€μ μœΌλ‘œ Babel 7을 μ‚¬μš©ν•˜λ„λ‘ μ—…λ°μ΄νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€. babel bridge λ˜λŠ” babel7-jest ν•˜λ©΄ 더 이상 ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
https://twitter.com/i/web/status/1088904207653105664

.babelrc 을 babel.config.js 둜 λ³€κ²½ν•˜κ³  Babel7둜 μ—…κ·Έλ ˆμ΄λ“œν–ˆλŠ”λ° μ—¬μ „νžˆ 이전과 λ™μΌν•œ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. λ‚΄ babel.config.js 파일:

module.exports = function (api) {
  api.cache(true);

  const presets = ["@babel/preset-env"];

  return {
    presets
  };
}

@babel/preset-react ν•˜κ³  babel.config.js νŒŒμΌμ— μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. 였λ₯˜λ₯Ό μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€.

λ‚΄ babel.config.js νŒŒμΌμ€ __test__ 디렉토리에 λ„£μ—ˆκΈ° λ•Œλ¬Έμ— babel-jest μ˜ν•΄ κ°μ§€λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. package.json κ°€ μžˆλŠ” μ΅œμƒμœ„ λ””λ ‰ν† λ¦¬λ‘œ μ΄λ™ν–ˆκ³  λͺ¨λ“  것이 μž‘λ™ν–ˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒμ„ μˆ˜ν–‰ν•˜μ—¬ λΉ„μŠ·ν•œ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€.

1- νš¨μ†Œ μ„€μ • νŒŒμΌμ„ μΆ”κ°€ν•˜κ³  λ‹€μŒμ„ μž‘μ„±ν•˜μ‹­μ‹œμ˜€.

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

2- λ‹€μŒκ³Ό 같이 package.json에 jest ꡬ성을 μΆ”κ°€ν•©λ‹ˆλ‹€.

"jest": {
 "setupFilesAfterEnv": [
  "./path to your setup file/setupEnzyme.js"
 ]
}

3- 루트 κ²½λ‘œμ— .babelrc νŒŒμΌμ„ μΆ”κ°€ν•˜κ³  λ‹€μŒμ„ μž‘μ„±ν•˜μ‹­μ‹œμ˜€.

{
    "env": {
        "test": {
            "presets": [
                "@babel/preset-env",
                "@babel/preset-react"
            ]
        }
    }
}

4 - 당신이 였λ₯˜κ°€ λ°”λ‘œ μ‹€ν–‰ ν…ŒμŠ€νŠΈμ—μ„œ ν‚€μ›Œλ“œλ₯Ό "κΈ°λŒ€"κ°€μ§€κ³ μžˆλŠ” 경우 npm install -D chai 와 같은 ν…ŒμŠ€νŠΈ μ½”λ“œμ—μ„œμ΄ κΈ°λŠ₯을 κΈ°λŒ€ μˆ˜μž… import { expect } from 'chai';

μ—¬μ „νžˆ 였λ₯˜κ°€ λ°œμƒν•˜λ©΄ npm install -D @babel/core @babel/preset-env @babel/preset-react 와 같은 babel 쒅속성을 μ„€μΉ˜ν•˜μ‹­μ‹œμ˜€.

도움이 λ˜μ—ˆκΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.

3일 후에 이 λ¬Έμ œμ— λŒ€ν•œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.
이λ₯Ό μœ„ν•œ 2가지 μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€.

μ†”λ£¨μ…˜ 1:

파일 이름을 .babelrc μ—μ„œ babel.config.js λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.

λ‚΄ μ†”λ£¨μ…˜:

μ €λŠ” .babelrc 파일이 μ—†μ—ˆκΈ° λ•Œλ¬Έμ— κ·Έ λ°˜λŒ€λ‘œ ν•˜λŠ” 것도 νš¨κ³Όκ°€ μžˆλ‹€κ³  μƒκ°ν•˜κ³  λŒ€μ‹  babel.config.js의 사전 μ„€μ • 정보λ₯Ό μ‚¬μš©ν•˜μ—¬ .babelrc νŒŒμΌμ„ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. (μΆ”κ°€ νŒŒμΌμ„ λ§Œλ“€μ—ˆκ³  원본 이름을 바꾸지 μ•Šμ•˜μŠ΅λ‹ˆλ‹€)
ν…ŒμŠ€νŠΈλ₯Ό λ‹€μ‹œ μ‹€ν–‰ν•˜λ©΄ Requires Babel "^7.0.0-0", but was loaded with "6.26.3" λΌλŠ” μƒˆλ‘œμš΄ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.
λ‚˜λŠ” κ·Έ 였λ₯˜λ₯Ό κ΅¬κΈ€λ§ν–ˆκ³  그것은 λ‚˜λ₯Ό μ—¬κΈ°λ‘œ μ΄λŒμ—ˆλ‹€. 지침( npm install [email protected] )을 λ”°λžκ³  이제 ν…ŒμŠ€νŠΈκ°€ μ‹€ν–‰λ˜κ³  ν†΅κ³Όν•©λ‹ˆλ‹€.

방금 λ‰΄μŠ€λ₯Ό νΌλœ¨λ¦¬λ©΄μ„œ Jest v24λŠ” λ‚΄λΆ€μ μœΌλ‘œ Babel 7을 μ‚¬μš©ν•˜λ„λ‘ μ—…λ°μ΄νŠΈλ˜μ—ˆμŠ΅λ‹ˆλ‹€. babel bridge λ˜λŠ” babel7-jest ν•˜λ©΄ 더 이상 ν•„μš”ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
https://twitter.com/i/web/status/1088904207653105664

Jest v24둜 μ—…κ·Έλ ˆμ΄λ“œν•˜λ©΄ 이 λ¬Έμ œκ°€ ν•΄κ²°λ˜μ—ˆμŠ΅λ‹ˆλ‹€. :)

unexpected token 였λ₯˜ λ°œμƒ

> 10 | const browser; (끝의 μ„Έλ―Έμ½œλ‘ )

λ‚΄ νŒ¨ν‚€μ§€.json

 "dependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.3.4",
    "@babel/register": "^7.0.0",
    "assert": "^1.4.1",
    "chai": "^4.2.0",
    "jest-puppeteer": "^4.0.0",
    "screen-info": "^1.0.1",
    "screenres": "^2.0.1"
  },
  "devDependencies": {
    "@babel/plugin-transform-modules-commonjs": "^7.2.0",
    "@babel/preset-env": "^7.3.4",
    "babel-jest": "^24.5.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-jest": "^24.3.0",
    "jest": "^24.5.0",
    "jest-cli": "^24.5.0",
    "puppeteer": "^1.13.0"
  },
  "jest": {
    "transformIgnorePatterns": [
      "/node_modules/(?!@babel).+\\.js$"
    ],
    "transform": {
      "^.+\\.js?$": "babel-jest"
    }
  },

babel.config.js

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        },
      },
      'jest'
    ]
  ],
  env: {
    test: {
      plugins: ['@babel/plugin-transform-modules-commonjs']
    }
  }
}

ν…ŒμŠ€νŠΈλœ hoaiduyit μ†”λ£¨μ…˜. μž‘λ™ν•©λ‹ˆλ‹€. babel.config.jsλ₯Ό μΆ”κ°€ν•˜κ³  jest.config.js에 babel-jest transform/transpile jestλ₯Ό μ„€μΉ˜ν•˜λ©΄ λ©λ‹ˆλ‹€.

3일 후에 이 λ¬Έμ œμ— λŒ€ν•œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.
이λ₯Ό μœ„ν•œ 2가지 μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€.

μ†”λ£¨μ…˜ 1:

파일 이름을 .babelrc μ—μ„œ babel.config.js λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ λ©λ‹ˆλ‹€.

μ†”λ£¨μ…˜ 2:

이 방법을 μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
λ‹€μŒκ³Ό 같은 λ³€ν™˜ νŒŒμΌμ„ λ§Œλ“­λ‹ˆλ‹€(λ‚΄ νŒŒμΌμ€ jest-transforme.js μž„).

const config = {
  babelrc: false,
  presets: [
    [
      "@babel/env",
      {
        modules: false
      }
    ],
    "@babel/react"
  ],
  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-proposal-class-properties", { loose: true }],
    "transform-es2015-modules-commonjs"
  ]
};
module.exports = require("babel-jest").createTransformer(config);

λ‚΄ jest.config.js λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

module.exports = {
  collectCoverageFrom: ["src/**/*.{js,jsx,mjs}"],
  testMatch: ["<rootDir>/src/**/__tests__/**/*.{js,jsx,mjs}", "<rootDir>/src/**/?(*.)(spec|test).{js,jsx,mjs}"],
  transform: {
    "^.+\\.(js|jsx|mjs)$": "<rootDir>/config/jest/jest-transformer.js"
  },
  transformIgnorePatterns: ["[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs)$"]
};

그리고 아직 λ‚΄ .babelrc

const enviroments = require("./env-config.js");

module.exports = {
  presets: [
    [
      "next/babel",
      {
        "preset-env": {
          useBuiltIns: "entry"
        }
      }
    ]
  ],
  plugins: [
    ["styled-components", { ssr: true, displayName: true, preprocess: false }],
    [
      "module-resolver",
      {
        root: ["./"]
      }
    ]
  ],
  env: {
    dev: {
      plugins: [
        ["transform-define", enviroments],
        ["module-resolver", { root: ["./"] }],
        "transform-decorators-legacy",
        "transform-class-properties"
      ]
    },
    build: {
      plugins: [
        ["transform-define", enviroments],
        ["module-resolver", { root: ["./"] }],
        "transform-decorators-legacy",
        "transform-class-properties"
      ]
    },
    production: {
      presets: [
        [
          "minify", // why add minify here, we run uglify over the whole code bundles
          {
            mangle: false,
            evaluate: false
          }
        ]
      ],
      plugins: [
        ["transform-define", enviroments],
        ["@babel/plugin-proposal-decorators", { legacy: true }],
        ["@babel/plugin-proposal-class-properties", { loose: true }],
        ["module-resolver", { root: ["./"] }]
      ],
      comments: false,
      compact: true,
      minified: true
    }
  }
};

이것이 μ—¬λŸ¬λΆ„μ΄ 이 문제λ₯Ό 톡과할 수 있기λ₯Ό λ°”λžλ‹ˆλ‹€. ν™˜ν˜Έ.

κ·€ν•˜μ˜ 방법에 λŒ€ν•œ tks ~이 방법은 λ‚΄ 문제λ₯Ό μ˜¬λ°”λ₯΄κ²Œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆλ‹€!

μ•ˆλ…•ν•˜μ„Έμš” - 이 μŠ€λ ˆλ“œμ—μ„œ μ—¬λŸ¬ 가지λ₯Ό μ‹œλ„ν–ˆμ§€λ§Œ 아무 것도 μž‘λ™ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ£Όμš” 였λ₯˜λŠ” μ΄μƒν•©λ‹ˆλ‹€. b/cλŠ” μ˜λ―Έκ°€ μ—†λŠ” 쀄 번호λ₯Ό κ°€λ¦¬ν‚€λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 마치 μ†ŒμŠ€ μ½”λ“œμ™€ μ‹€ν–‰ν•˜λ €λŠ” μ‹€μ œ μ½”λ“œκ°€ μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” 것과 κ°™μŠ΅λ‹ˆλ‹€.

Jestκ°€ 예기치 μ•Šμ€ 토큰을 λ°œκ²¬ν–ˆλ‹€λŠ” 이 였λ₯˜κ°€ 남아 μžˆμŠ΅λ‹ˆλ‹€. μ €λŠ” Jest > 24, no type script에 μžˆμŠ΅λ‹ˆλ‹€. μ΅œκ·Όμ—(6κ°œμ›” μ „~) 제거된 react-create-appμž…λ‹ˆλ‹€. μΊμ‹œλ₯Ό μ§€μš°κ³  node_modulesλ₯Ό μ œκ±°ν•΄λ„ 도움이 λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. μ•„λ§ˆλ„ λˆ„κ΅°κ°€ jestκ°€ μ˜¬λ°”λ₯΄κ²Œ λ³€ν™˜ν•˜κ³  μ‹€ν–‰ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ ꡬ성과 μ •ν™•ν•œ 였λ₯˜κ°€ μ œκ³΅ν•˜λŠ” 힌트λ₯Ό μ •ν™•νžˆ μ„€λͺ…ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

FAIL  __e2e__/chat/sms.spec.js
  ● 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/ben/dev/app/webui/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:17
    export default function _asyncToGenerator(fn) {
    ^^^^^^

    SyntaxError: Unexpected token export

       5 |   afterEach(async () => {
       6 |     await logout();
    >  7 | 
         | ^
       8 |     await page.evaluate(() => {
       9 |       localStorage.clear();
      10 |     });

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:471:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:513:25)
      at Object.<anonymous> (__e2e__/chat/sms.spec.js:7:49)

λ‹€λ“€ 감사 ν•΄μš”,

이 문제둜 인해 Jenkinsκ°€ μ‹€νŒ¨ν•©λ‹ˆλ‹€. μž‘λ™ν•˜λŠ” μ†”λ£¨μ…˜μ΄ μžˆμŠ΅λ‹ˆκΉŒ?

Create-React-App을 μ‚¬μš©ν•˜λŠ” μ‚¬λžŒμ˜ 경우 CRAλ₯Ό μ‚¬μš©ν•  λ•Œ package.jsonμ—μ„œ νŠΉμ • jest κ΅¬μ„±λ§Œ λ³€κ²½ν•  수 μžˆμœΌλ―€λ‘œ package.json에 transformignorepatterns λ₯Ό 좔가해도 λ¬Έμ œκ°€ ν•΄κ²°λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Jestκ°€ λ‚΄λΆ€ 라이브러리λ₯Ό μ„ νƒν•˜λŠ” 데 λ¬Έμ œκ°€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€. JestλŠ” 이 λΌμ΄λΈŒλŸ¬λ¦¬μ—μ„œ κ°€μ Έμ˜€κΈ°λ₯Ό ν•  λ•Œλ§ˆλ‹€ '예기치 μ•Šμ€ 토큰' 였λ₯˜λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€.

이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ ν…ŒμŠ€νŠΈ 슀크립트λ₯Ό μ•„λž˜μ™€ 같이 λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
"test": "react-scripts test --transformIgnorePatterns 'node_modules/(?!(<your-package-goes-here>)/)'",

μ•ˆλ…•ν•˜μ„Έμš”, μ €λŠ” JetSet이라고 ν•˜κ³  React Native둜 μž‘μ„±λœ μž‘μ€ ν”„λ‘œμ νŠΈλ‘œ λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό μˆ˜ν–‰ν•˜λŠ” 데 어렀움을 κ²ͺκ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€. 이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ 2일 λ™μ•ˆ κ²€μƒ‰ν–ˆκ³  babel.config.js와 같은 것을 νŽΈμ§‘ν•˜κ³  ν•΄λ‹Ή νŒŒμΌμ„ μ‚­μ œν•œ λ‹€μŒ .babel νŒŒμΌμ„ 생성(그리고 μ•½κ°„μ˜ μΆ”κ°€)ν•˜λ €κ³  ν–ˆμŠ΅λ‹ˆλ‹€.


_버그 μ‹ κ³ :_
D:\jetset\JetSetApp>jest SaveData.test.js
μ‹€νŒ¨ __test__/SaveData.test.js
● ν…ŒμŠ€νŠΈ μŠ€μœ„νŠΈ μ‹€ν–‰ μ‹€νŒ¨

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:

D:\jetset\JetSetApp\node_modules\expo-file-system\build\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as FileSystem from './FileSystem';
                                                                                                ^

SyntaxError: Unexpected token *

  1 | import React, { Component } from 'react'
  2 | import { View, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native'
> 3 | import { documentDirectory, getInfoAsync, readDirectoryAsync, readAsStringAsync, writeAsStringAsync, deleteAsync } from 'expo-file-system';
    | ^
  4 |
  5 | /**
  6 |  * This class primarily serves to handle files within the phone system so that results can be stored

  at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
  at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
  at Object.<anonymous> (app/resources/SaveData/SaveData.js:3:1)

ν…ŒμŠ€νŠΈ μŠ€μœ„νŠΈ: 1개 μ‹€νŒ¨, 총 1개
ν…ŒμŠ€νŠΈ: 총 0개
μŠ€λƒ…μƒ·: 총 0개
μ‹œκ°„: 3.509초
/SaveData.test.js/i와 μΌμΉ˜ν•˜λŠ” λͺ¨λ“  ν…ŒμŠ€νŠΈ μŠ€μœ„νŠΈλ₯Ό μ‹€ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.


_ babel.config.js _
module.exports = ν•¨μˆ˜(api) {
api.cache(μ°Έ);
λ°˜ν’ˆ {
사전 μ„€μ •: ['babel-preset-expo'],
};
};


_ νŒ¨ν‚€μ§€.json _
{
"메인": "node_modules/expo/AppEntry.js",
"슀크립트": {
"μ‹œμž‘": "μ—‘μŠ€ν¬ μ‹œμž‘",
"μ•ˆλ“œλ‘œμ΄λ“œ": "μ—‘μŠ€ν¬ μ‹œμž‘ --android",
"ios": "μ—‘μŠ€ν¬ μ‹œμž‘ --ios",
"μ›Ή": "μ—‘μŠ€ν¬ μ‹œμž‘ --μ›Ή",
"κΊΌλ‚΄κΈ°": "μ—‘μŠ€ν¬ κΊΌλ‚΄κΈ°",
"ν…ŒμŠ€νŠΈ": "농담"
},
"농담": {
"사전 μ„€μ •": "λ°˜μ‘ λ„€μ΄ν‹°λΈŒ"
},
"쒅속성": {
"μ—‘μŠ€ν¬": "^34.0.1",
"μ—‘μŠ€ν¬ 파일 μ‹œμŠ€ν…œ": "^6.0.2",
"expo-mail-composer": "^6.0.0",
"μˆœκ°„": "^2.24.0",
"λ°˜μ‘": "16.8.3",
"react-dom": "^16.8.6",
"λ°˜μ‘ λ„€μ΄ν‹°λΈŒ": " https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz ",
"λ°˜μ‘ λ„€μ΄ν‹°λΈŒ 제슀처 처리기": "^1.3.0",
"λ°˜μ‘ λ„€μ΄ν‹°λΈŒ 선택 κ·Έλ£Ή": "^1.1.2",
"λ°˜μ‘ λ„€μ΄ν‹°λΈŒ μ›Ή": "^0.11.4"
},
"devDependencies": {
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"바벨 농담": "^24.9.0",
"babel-preset-expo": "^6.0.0",
"농담": "^24.9.0",
"μ†Œν’ˆ μœ ν˜•": "^15.7.2",
"react-native-dismiss-keyboard": "^1.0.0",
"react-native-modal-datetime-picker": "^7.5.0",
"λ°˜μ‘ 탐색": "^3.12.1"
},
"λΉ„κ³΅κ°œ": 사싀
}

참고둜 μ €λŠ” NODE_ENV="test"κ°€ μ„€μ •λ˜μ§€ μ•Šμ€ 경우 전체 λΉŒλ“œ μ‹œμŠ€ν…œμ΄ μ œλŒ€λ‘œ μž‘λ™ν•˜μ§€ μ•ŠλŠ” κ²ƒμ²˜λŸΌ 이것에 λŒ€ν•œ 정말 어리석은 해결책을 찾은 것 κ°™μŠ΅λ‹ˆλ‹€.

μ΅œμ‹  λ²„μ „μ˜ React JestλŠ” package.json에 λ‚΄μž₯λ˜μ–΄ μžˆμœΌλ―€λ‘œ ν…ŒμŠ€νŠΈ μŠ€ν¬λ¦½νŠΈλŠ” λ‹€μŒκ³Ό κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€.
"test": "react-scripts test"
λŒ€μ‹ μ—
"test": "jest"

μ΅œμ‹  λ²„μ „μ˜ React JestλŠ” package.json에 λ‚΄μž₯λ˜μ–΄ μžˆμœΌλ―€λ‘œ ν…ŒμŠ€νŠΈ μŠ€ν¬λ¦½νŠΈλŠ” λ‹€μŒκ³Ό κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€.
"test": "react-scripts test"
λŒ€μ‹ μ—
"test": "jest"

덕뢄에 λ¬Έμ œκ°€ ν•΄κ²°λ˜μ—ˆμŠ΅λ‹ˆλ‹€!

많이 μ‹œλ„ν•œ ν›„ λ¬Έμ œλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
.babelrc νŒŒμΌμ— "transform-es2015-modules-commonjs", λˆ„λ½

{
  "presets": ["next/babel"],
  "plugins": [
    ["styled-components", { "ssr": true, "displayName": true, "preprocess": false } ],
    "transform-flow-strip-types",
    "jsx-control-statements",
    "import-glob",
    "transform-es2015-modules-commonjs",
    [
      "import",
      {
        "libraryName": "antd",
        "style": "css"
      }
    ]
  ]
}

"농담": "^24.9.0",
"바벨 농담": "23.6.0",

농담과 babel-jestλ₯Ό μ—…λ°μ΄νŠΈν•˜λ €κ³  μ‹œλ„ν•˜μ‹­μ‹œμ˜€.

.babelrc 을 babel.config.js 둜 λ³€κ²½ν•˜κ³  Babel7둜 μ—…κ·Έλ ˆμ΄λ“œν–ˆλŠ”λ° μ—¬μ „νžˆ 이전과 λ™μΌν•œ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. λ‚΄ babel.config.js 파일:

module.exports = function (api) {
  api.cache(true);

  const presets = ["@babel/preset-env"];

  return {
    presets
  };
}

@babel/preset-react ν•˜κ³  babel.config.js νŒŒμΌμ— μΆ”κ°€ν–ˆμŠ΅λ‹ˆλ‹€. 였λ₯˜λ₯Ό μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€.

이것은 λ‚΄ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. @babel/preset-envλ₯Ό μ„€μΉ˜ν•˜κ³  babel.config.js에 μΆ”κ°€ν•˜κ³  μˆ˜μ •ν–ˆμŠ΅λ‹ˆλ‹€.

@jmayergit 감사

babel.rc νŒŒμΌμ„ babel.config.js 파일둜 λ³€κ²½ν–ˆμ§€λ§Œ μž‘λ™ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒμ΄ 닡변을 λ³΄μ•˜μŠ΅λ‹ˆλ‹€. https://stackoverflow.com/questions/58470062/test-jest-with-babel-plugin-import
κ°€μ Έμ˜€κΈ° ν”ŒλŸ¬κ·ΈμΈμ΄ μžˆλŠ” 경우 ν…ŒμŠ€νŠΈ, 개발 및 ν”„λ‘œλ•μ…˜ ν™˜κ²½μš© ν”ŒλŸ¬κ·ΈμΈμ„ λΆ„λ¦¬ν•©λ‹ˆλ‹€.

λ‚΄ babel.config.js νŒŒμΌμ€ 이제 λ‹€μŒκ³Ό 같이 μž‘λ™ν•©λ‹ˆλ‹€.

module.exports = {
  presets: [
    [
      'next/babel',
    ],
  ],
  env: {
    test: {

    },
    dev: {
      plugins: [
        ['import', { libraryName: 'antd', style: 'css' }],
      ],
    },
    production: {
      plugins: [
        ['import', { libraryName: 'antd', style: 'css' }],
      ],
    },
  },
};

λ‚˜λŠ” λ˜ν•œ 5 일뢀터이 였λ₯˜λ‘œ 어렀움을 κ²ͺκ³  μžˆμŠ΅λ‹ˆλ‹€.

    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:

    SyntaxError: C:\Users\SK-PC\Desktop\React-Course\expansify\src\test\components\MyNavbar.test.js: Unexpected token (5:28)

      3 | import { MyNavbar } from '../../components/MyNavbar'
      4 | test("Should return MyNavbar component", () => {
    > 5 |     const wrapper = shallow(<MyNavbar />)
        |                             ^
      6 |     expect(wrapper.find('.navbar-brand').find('kbd').text()).toBe("Expensify")
      7 |     // const renderer = new ReactShallowRenderer()
      8 |     // renderer.render(<MyNavbar />)

      at Parser._raise (node_modules/@babel/parser/src/parser/location.js:241:45)
      at Parser.raiseWithData (node_modules/@babel/parser/src/parser/location.js:236:17)
      at Parser.raise (node_modules/@babel/parser/src/parser/location.js:220:17)
      at Parser.unexpected (node_modules/@babel/parser/src/parser/util.js:149:16)
      at Parser.parseExprAtom (node_modules/@babel/parser/src/parser/expression.js:1144:20)
      at Parser.parseExprSubscripts (node_modules/@babel/parser/src/parser/expression.js:539:23)
      at Parser.parseMaybeUnary (node_modules/@babel/parser/src/parser/expression.js:519:21)
      at Parser.parseExprOps (node_modules/@babel/parser/src/parser/expression.js:311:23)
      at Parser.parseMaybeConditional (node_modules/@babel/parser/src/parser/expression.js:263:23)
      at Parser.parseMaybeAssign (node_modules/@babel/parser/src/parser/expression.js:211:21)

 PASS  src/test/reducers/filters.test.js

Test Suites: 1 failed, 5 passed, 6 total
Tests:       29 passed, 29 total
Snapshots:   0 total
Time:        12.144s
Ran all test suites.

@fullstacksk

μ΅œμ‹  λ²„μ „μ˜ React JestλŠ” package.json에 λ‚΄μž₯λ˜μ–΄ μžˆμœΌλ―€λ‘œ ν…ŒμŠ€νŠΈ μŠ€ν¬λ¦½νŠΈλŠ” λ‹€μŒκ³Ό κ°™μ•„μ•Ό ν•©λ‹ˆλ‹€.
"test": "react-scripts test"
λŒ€μ‹ μ—
"test": "jest"

λ‹€μŒ μœ„μΉ˜μ—μ„œ λ¬Έμ„œλ₯Ό 읽을 수 μžˆμŠ΅λ‹ˆλ‹€.
https://jestjs.io/docs/en/tutorial-react
https://create-react-app.dev/docs/running-tests/#docsNav

@catmans1
create-react-app λ˜λŠ” react-scripts와 같은 쒅속성을 μ„€μΉ˜ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
μž‘λ™ν• κΉŒμš”?
"test": "react-scripts test"
λŒ€μ‹ μ—
"test": "jest"

이제 env 및 react둜 μ˜΅μ…˜μ„ ν…ŒμŠ€νŠΈν•˜κΈ° μœ„ν•΄ babel-preset-jestλ₯Ό μ‚¬μš©ν•  λ•Œ μž‘λ™ν•©λ‹ˆλ‹€. λ‚˜λŠ” babel-jeastλ₯Ό μ œκ±°ν–ˆμŠ΅λ‹ˆλ‹€.

μ €λ₯Ό μœ„ν•΄ μΌν–ˆμŠ΅λ‹ˆλ‹€: babel.config.js "@babel/preset-react"λ₯Ό 포함

https://medium.com/@audreyhal/how -to-solve-jest-error-with-create-react-app-part-1-80f33aa1661a

어리석은 μ‹€μˆ˜λ₯Ό ν•˜κ³  .tsx ν™•μž₯μžκ°€ μ•„λ‹Œ .ts 첫 번째 ν…ŒμŠ€νŠΈ νŒŒμΌμ„ μž‘μ„±ν•œ ν›„ 이 였λ₯˜λ₯Ό λ³΄μ•˜μŠ΅λ‹ˆλ‹€. μ›λž˜ λ¬Έμ œμ™€λŠ” 거리가 멀지 만 λˆ„κ΅°κ°€μ—κ²Œ 도움이 될 수 μžˆμŠ΅λ‹ˆλ‹€ πŸ˜„

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰