gpt4 book ai didi

javascript - 语法错误 : Unexpected token : when running jest test with Aurelia in TypeScript

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:44 25 4
gpt4 key购买 nike

刚刚使用 aurelia-cli 创建了一个新的 TypeScript Aurelia 项目。安装 Bootstrap 并使用导入在 app.ts 中包含 Bootstrap css。

import 'bootstrap/dist/css/bootstrap.min.css';
import '../../static/assets/css/app.scss';
import { routes } from './routes';

interface IApp {
message: string;
}

export class App implements IApp{
message = 'Hello World!';
}

现在当我运行测试时,我得到如下错误意外标记

yarn test

# and the output contains

yarn run v1.12.3
$ nps test
nps is executing `test` : nps test.jest
nps is executing `test.jest` : node node_modules/rimraf/bin.js test/coverage-jest && jest
ts-jest[config] (WARN) TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information.
FAIL test/unit/app.spec.ts
● 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 hav`enter code here`e 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.

SyntaxError: Unexpected token :



at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/home/app.ts:163:1)

我评论了 app.ts 中的 import bootstrap.css 行,一切运行正常。

我是否缺少一些允许我在 .ts 组件中使用 css 导入的 Jest 配置?

这是我在 package.json 中的 Jest 部分

"jest": {
"verbose": true,
"roots": [
"<rootDir>/test"
],
"modulePaths": [
"<rootDir>/src",
"<rootDir>/node_modules"
],
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"transform": {
"^.+\\.ts$": "ts-jest"
},
"testRegex": "\\.spec\\.(ts|js)$",
"testPathIgnorePatterns": [
"build",
"dist",
"sample"
],
"setupFiles": [
"<rootDir>/test/jest-pretest.ts"
],
"testEnvironment": "node",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.{js,ts}",
"!**/*.spec.{js,ts}",
"!**/node_modules/**",
"!**/test/**"
],
"coverageDirectory": "<rootDir>/test/coverage-jest",
"coverageReporters": [
"json",
"lcov",
"text",
"html",
"clover"
]

},

最佳答案

我遇到了同样的错误。我通过将 tsconfig.json 中的模块 compilerOption 从“esnext”更改为“commonjs”来解决我的问题。 Why am I getting “Unexpected token import” on one webpack project but not the other?

关于javascript - 语法错误 : Unexpected token : when running jest test with Aurelia in TypeScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53809850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com