SyntaxError : 예기치 않은 토큰 내보내기로 인해 jest 프레임 워크를 통해 단위 테스트를 실행할 수 없습니다.

샤리 코프 블라디슬라프

jest 대 angularfire2에 문제가 있습니다. 농담을 설정 한 다음 오류가 발생했습니다 SyntaxError: Unexpected token export. 전체 출력 :

 PASS  src/app/diary/components/diary.spec.ts
 FAIL  src/app/diary/containers/my-dairy-page.spec.ts
  ● Test suite failed to run

    /Users/svlad/dev/ng2-diary-book/node_modules/angularfire2/auth.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './auth/auth';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
      at Object.<anonymous> (src/app/core/containers/app.ts:18:14)
      at Object.<anonymous> (src/app/core/core.module.ts:15:13)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.467s

디버그 출력

디버그 모드에서 출력하여 전체 jest 구성을 볼 수 있습니다.

sharikovvlad:ng2-diary-book svlad$ npm run jest -- --debug

> [email protected] jest /Users/svlad/dev/ng2-diary-book
> jest "--debug"

{
  "config": {
    "automock": false,
    "browser": false,
    "cache": true,
    "cacheDirectory": "/var/folders/pf/510z0sc56zq3hvfb7lyr0jlr0000gn/T/jest_dx",
    "clearMocks": false,
    "coveragePathIgnorePatterns": [
      "/node_modules/"
    ],
    "globals": {
      "ts-jest": {
        "tsConfigFile": "src/tsconfig.spec.json"
      },
      "__TRANSFORM_HTML__": true
    },
    "haste": {
      "providesModuleNodeModules": []
    },
    "moduleDirectories": [
      "node_modules"
    ],
    "moduleFileExtensions": [
      "ts",
      "js",
      "html",
      "json"
    ],
    "moduleNameMapper": [
      [
        "(.*)",
        "/Users/svlad/dev/ng2-diary-book/src/$1"
      ]
    ],
    "modulePathIgnorePatterns": [],
    "name": "520410dc90eed72792e9fd01593c2a6d",
    "resetMocks": false,
    "resetModules": false,
    "rootDir": "/Users/svlad/dev/ng2-diary-book",
    "roots": [
      "/Users/svlad/dev/ng2-diary-book"
    ],
    "setupFiles": [],
    "setupTestFrameworkScriptFile": "/Users/svlad/dev/ng2-diary-book/src/setupJest.ts",
    "snapshotSerializers": [],
    "testEnvironment": "jest-environment-jsdom",
    "testMatch": [],
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|js)$",
    "testRunner": "/Users/svlad/dev/ng2-diary-book/node_modules/jest-jasmine2/build/index.js",
    "testURL": "about:blank",
    "timers": "real",
    "transform": [
      [
        "^.+\\.(ts|js|html)$",
        "/Users/svlad/dev/ng2-diary-book/node_modules/jest-preset-angular/preprocessor.js"
      ]
    ],
    "transformIgnorePatterns": [
      "/Users/svlad/dev/ng2-diary-book/node_modules/(?!@ngrx|angularfire2)"
    ]
  },
  "framework": "jasmine2",
  "globalConfig": {
    "bail": false,
    "coverageReporters": [
      "json",
      "text",
      "lcov",
      "clover"
    ],
    "expand": false,
    "mapCoverage": true,
    "noStackTrace": false,
    "notify": false,
    "projects": [
      "/Users/svlad/dev/ng2-diary-book"
    ],
    "rootDir": "/Users/svlad/dev/ng2-diary-book",
    "testPathPattern": "",
    "testResultsProcessor": null,
    "updateSnapshot": "new",
    "useStderr": false,
    "verbose": null,
    "watch": false,
    "watchman": true
  },
  "version": "20.0.4"
}
 PASS  src/app/diary/components/diary.spec.ts
 FAIL  src/app/diary/containers/my-dairy-page.spec.ts
  ● Test suite failed to run

    /Users/svlad/dev/ng2-diary-book/node_modules/angularfire2/auth.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './auth/auth';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/ScriptTransformer.js:289:17)
      at Object.<anonymous> (src/app/core/containers/app.ts:18:14)
      at Object.<anonymous> (src/app/core/core.module.ts:15:13)

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.709s
Ran all test suites.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] jest: `jest "--debug"`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] jest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/svlad/.npm/_logs/2017-08-20T21_09_30_035Z-debug.log
sharikovvlad:ng2-diary-book svlad$ 

이 코드는 ES5로 트랜스 파일되어야하므로 구문 오류가 발생하지 않아야합니다 (config에서 그렇게 말 했으므로). 나는 그것을 왜 얻습니까?

또한 이상한 것은 내가 아무것도 무시하지 않는다는transformIgnorePatterns 것을 의미하는 빈 배열로 설정하더라도 동일한 오류가 발생 한다는 입니다.

버전 정보

각도 : 4.3.5

Firebase : 4.2.0

AngularFire : 4.0.0-rc0

기타 (예 : Ionic / Cordova, Node, 브라우저, 운영 체제) :

sharikovvlad:~ svlad$ node -v
v8.3.0
sharikovvlad:~ svlad$ yarn --version
0.27.5
sharikovvlad:~ svlad$ macosVersion
10.12.6

이러한 조건을 재현하는 방법

설정 및 재현 단계

  1. 설치와 함께 : yarn add --dev is-preset-angular @ types / is
  2. <rootDir>/src/setupJest.ts내용이있는 파일 추가 :import 'jest-preset-angular';
  3. package.json에 jest 블록을 추가합니다.
 "jest": {
    "preset": "jest-preset-angular",
    "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!@ngrx|angularfire2)"
    ]
  },

그러나 실제로 다음과 같이 할 수 있습니다.

  1. 내 저장소 https://github.com/sharikovvladislav/ng2-diary-book/tree/feature/try-add-jest 및 체크 아웃 분기 try-add-jest 복제
  2. 원사 설치 실행
  3. 실행 해발은 실행이다
  4. 여기에 문제가 있습니다.
샤리 코프 블라디슬라프

핵심 문제는 ts-jest (jest-preset-angular는 ts-jest를 기반으로 함)가 JS 파일을 트랜스 파일하지 않는다는 것입니다.

그래서 나는 그 일을 할 무언가가 필요합니다. Jest는 babel-jest즉시 트랜스 파일러를 제공합니다 . 그래서 jest를 구성하여 js와 ts 파일을 모두 트랜스 파일 할 것입니다.

다음은 마지막 jest 구성 (package.json)입니다.

"jest": {
  "preset": "jest-preset-angular",
  "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts",
  "transform": {
    "^.+\\.(ts|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js",
    "^.+\\.js$": "babel-jest"
  },
  "transformIgnorePatterns": [
    "node_modules/(?!(@ngrx|angularfire2))"
  ],
  "globals": {
    "ts-jest": {
      "tsConfigFile": "src/tsconfig.spec.json",
      "useBabelrc": true
    },
    "__TRANSFORM_HTML__": true
  }
},

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관