首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Jest在Nx Workspace项目中对Svelte应用程序进行单元测试时出错

使用Jest在Nx Workspace项目中对Svelte应用程序进行单元测试时出错
EN

Stack Overflow用户
提问于 2021-03-04 03:29:38
回答 1查看 221关注 0票数 1

我创建了一个空的NxWorkspace,然后按照步骤here添加了一个svelte项目。我使用命令nx g @nxext/svelte:c my-comp添加了元件及其等级库文件。在此之前,测试运行没有任何问题,但是在向MyComp.svelte文件添加一些TypeScript代码(如下所示)后,测试停止。

代码语言:javascript
复制
<script lang="ts">
  let temp: string;
</script>

<h1>Hello component!</h1>

<h1>
</h1>

测试文件my-comp.spec.ts

代码语言:javascript
复制
import MyComp from './MyComp.svelte';
import { render } from '@testing-library/svelte';

it('it works', async () => {
  const { getByText } = render(MyComp);

  expect(getByText('Hello component!'));
});

我收到的错误是:

代码语言:javascript
复制
 FAIL   my-app  apps/my-app/src/components/my-comp/my-comp.spec.ts
  ● Test suite failed to run

    ParseError: Unexpected token

      at error (../../node_modules/svelte/src/compiler/utils/error.ts:25:16)
      at Parser$1.error (../../node_modules/svelte/src/compiler/parse/index.ts:100:3)
      at Parser$1.acorn_error (../../node_modules/svelte/src/compiler/parse/index.ts:93:8)
      at Object.read_script [as read] (../../node_modules/svelte/src/compiler/parse/read/script.ts:51:10)
      at tag (../../node_modules/svelte/src/compiler/parse/state/tag.ts:205:27)
      at new Parser$1 (../../node_modules/svelte/src/compiler/parse/index.ts:52:12)
      at parse (../../node_modules/svelte/src/compiler/parse/index.ts:216:17)
      at Object.compile (../../node_modules/svelte/src/compiler/compile/index.ts:91:14)
      at Object.process (../../node_modules/svelte-jester/src/transformer.js:21:25)
      at ScriptTransformer.transformSource (../../node_modules/@jest/transform/build/ScriptTransformer.js:464:35)

生成的jest.config.js

代码语言:javascript
复制
module.exports = {
  displayName: 'my-app',
  preset: '../../jest.preset.js',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+\\.svelte$': 'svelte-jester',
    '^.+\\.[tj]s$': 'ts-jest',
  },
  moduleFileExtensions: ['ts', 'js', 'html', 'svelte'],
  coverageDirectory: '../../coverage/apps/my-app',
};

tsconfig.json文件:

代码语言:javascript
复制
{
  "extends": "../../tsconfig.base.json",

  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es2017",
    /**
      Svelte Preprocess cannot figure out whether you have a value or a type, so tell TypeScript
      to enforce using `import type` instead of `import` for Types.
     */
    "importsNotUsedAsValues": "error",
    "isolatedModules": true,
    /**
      To have warnings/errors of the Svelte compiler at the correct position,
      enable source maps by default.
     */
    "sourceMap": true,
    /** Requests the runtime types from the svelte modules by default. Needed for TS files or else you get errors. */
    "types": ["svelte"],

    "strict": false,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "files": [],
  "include": [],
  "references": [
    {
      "path": "./tsconfig.app.json"
    },
    {
      "path": "./tsconfig.spec.json"
    }
  ]
}

the tsconfig.spec.json

代码语言:javascript
复制
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "module": "commonjs",
    "types": ["jest", "node", "@types/jest"]
  },
  "include": [
    "**/*.spec.ts",
    "**/*.spec.tsx",
    "**/*.spec.js",
    "**/*.spec.jsx",
    "**/*.d.ts"
  ]
}

NX工作区根目录下的tsconfig.base.json

代码语言:javascript
复制
{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": ["es2017", "dom"],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {}
  },
  "exclude": ["node_modules", "tmp"]
}

我在跳,有一些简单的修复在这些文件中,我没有看到。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2021-03-06 07:34:23

对于那些发现相同问题的人,可以在GitHub存储库中的以下issue中找到此问题的答案。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66463653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档