由于 .tns,NativeScript 在浏览器中的单元测试失败。文件

Unit testing in browser for NativeScript fails because of .tns. files

我有一个 Angular 风格的 Nativescript 项目,必须在浏览器(而不是移动设备)中使用 "vanila" Jasmine 进行测试 ng test

默认情况下,通过 "naked" 测试,它可以工作。但问题是,如果我尝试 test/import 任何具有“.tns”替代项的内容,在某些情况下它会加载它,并且构建失败。

我的问题与类似,但那里没有描述好的解决方案。

例如: 我有两个文件:

app.component.tns.ts
app.component.ts

我尝试将其导入 app.component.spec.ts 进行测试:

import {AppComponent} from "@src/app/app.component";

它加载 .tns. 文件,但构建失败,因为它无法加载特定于移动设备的库。

     ERROR in ./src/app/app.component.tns.ts
Module not found: Error: Can't resolve 'nativescript-ui-sidedrawer' in '/home/..../src/app'
resolve 'nativescript-ui-sidedrawer' in '/home/...../src/app'
  Parsed request is a module
  using description file: /home/...../src/package.json (relative path: ./app)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module

    ...
     @ ./src/app/app.component.tns.ts 25:35-72
     @ ./src/app/app.module.spec.ts
     @ ./src sync \.spec\.ts$
     @ ./src/test.ts

是否有 "remove" .tns 的解决方案。文件,就好像我是 运行 一个简单的 ng serve?

更新:我的tsconfig.spec.json应该排除这些文件,但它也不起作用...

  "exclude": [
    "**/*.tns.ts",
    "**/*.android.ts",
    "**/*.ios.ts"
  ]
}

问题似乎出在 tsconfig.json 上。具体这部分:

"compilerOptions": {
  ...
      "paths": {
          "@src/*": [
              "src/*.android.ts",
              "src/*.ios.ts",
              "src/*.tns.ts",
              "src/*.web.ts",
              "src/*.ts"
          ]
      },

因为 tsconfig.spec.json 对其进行了扩展。

我将tsconfig.spec.json修改为:

{
  "compilerOptions": {
    "target": "es5",
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom",
      "es6",
      "es2015.iterable"
    ],
    "baseUrl": ".",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "paths": {
      "@src/*": [
        "src/*.ts"
      ]
    },
    "outDir": "../out-tsc/spec",
    "types": [
      "jasmine",
      "node"
    ]
  },
  "files": [
    "src/test.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ],
  "exclude": [
    "**/*.tns.ts",
    "**/*.android.ts",
    "**/*.ios.ts"
  ]
}

现在是测试 运行,并且导入了正确的组件。