我对jest vs angularfire2有问题。我设置了jest然后我收到了错误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
> angular-ngrx-diary@0.0.0 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! angular-ngrx-diary@0.0.0 jest: `jest "--debug"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the angular-ngrx-diary@0.0.0 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(因为配置是这样的)。为什么我明白了?
同样奇怪的是即使我将transformIgnorePatterns
设置为空数组,这意味着忽略任何内容我会得到同样的错误。
角: 4.3.5
火力地堡: 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
设置和复制的步骤
<rootDir>/src/setupJest.ts
import 'jest-preset-angular';
"jest": { "preset": "jest-preset-angular", "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts", "transformIgnorePatterns": [ "<rootDir>/node_modules/(?!@ngrx|angularfire2)" ] },
但实际上你可以:
答案 0 :(得分:2)
核心问题是ts-jest(jest-preset-angular基于ts-jest)不会转换JS文件。
所以我需要做点什么工作。 Jest提供开箱即用的babel-jest
转换器。所以我需要配置jest,这样他就可以转换js和ts文件。
这是我最后的开玩笑configuraiton(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
}
},