Karma无法以角度解析命名空间环境变量为什么?

时间:2018-04-25 04:56:03

标签: angular karma-jasmine angular-test

在我的应用程序中,我在 tsconfig.ts 应用程序中设置了环境的命名空间,但没有错误,但问题出在运行时 ng test 命令 karma无法解决这些变量。

我的tsconfig看起来像这样。

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ],
    "baseUrl": "src",
    "paths": {
      "@app/*": [
        "app/*"
      ],
      "@env/*": [
        "environments/*"
      ],
      "@service/*": [
        "services/*"
      ]
    }
  },
  "angularCompilerOptions": {
    "preserveWhitespaces": false
  }
}

在我的组件中,我正在访问这样的环境变量,以便为家庭组件阅读 templateUrl styleUrls

import { environment } from '@app/environments';

@Component({
  selector: 'app-home',
  templateUrl: environment.theme + '/home/home.component.html',
  styleUrls: [ environment.theme + '/home/home.component.scss']
})
export class HomeComponent {}

这是 karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
    config.set({
        basePath: '.',
        frameworks: ['jasmine', '@angular/cli', 'karma-typescript'],

        // file need to be loaded during testing.
        files: [
            // { pattern: 'src/environments/*', included: false, served: true, watched: true },
            // { pattern: 'src/services/*.{ts,js}', included: false, served: true, watched: true },
        ],


        karmaTypescriptConfig: {
            tsconfig: "./tsconfig.json",
            bundlerOptions: {
                transforms: [
                    require("karma-typescript-angular2-transform")
                ]
            }
        },
        preprocessors: {
            "**/*.ts": "karma-typescript"
        },

        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-junit-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular/cli/plugins/karma'),
            require('karma-typescript'),
        ],
        client: {
            clearContext: false, // leave Jasmine Spec Runner output visible in browser
            captureConsole: false
        },
        junitReporter: {
            outputDir: 'reports/junit/',
            outputFile: 'TESTS-xunit.xml',
            useBrowserName: false,
            suite: '' // Will become the package name attribute in xml testsuite element
        },
        coverageIstanbulReporter: {
            reports: ['html', 'lcovonly', 'text-summary'],
            dir: './reports/coverage',
            fixWebpackSourcePaths: false
        },
        angularCli: {
            environment: 'test'
        },
        reporters: ['progress', 'junit', 'karma-typescript'],
        port: 9876,
        colors: true,
        // Level of logging, can be: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadless'],
        singleRun: false
    });
};

我无法理解为什么我会收到此错误,有人可以解释为什么会发生这种情况吗?

Error details

我是否需要向 karma.conf.js 添加任何内容,以使其了解 tsconfig

中的路径选项

0 个答案:

没有答案