SyntaxError:意外令牌:在TypeScript中使用Aurelia运行开玩笑测试时

时间:2018-12-17 06:20:22

标签: javascript typescript unit-testing jestjs aurelia

仅使用aurelia-cli创建了一个新的TypeScript Aurelia项目。 使用导入功能在app.ts中安装了引导程序并包括了引导程序CSS。

handle-response

现在,当我运行测试时,出现如下所示的错误意外令牌

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!';
}

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

我是否出于开玩笑而缺少一些配置,以允许我在.ts组件中使用CSS导入?

这是我来自package.json的笑话

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)

1 个答案:

答案 0 :(得分:1)

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