使用Open WC Karma-ESM插件测试Typescript文件时出错

问题描述 投票:0回答:1

我以sudo的身份运行测试,即sudo npm run test

....而且有效。去搞清楚!有人在乎为什么会这样吗?

我正在使用开放式Web组件karma-esm插件在我的monorepo中运行测试。但是我发现无头chrome抛出一个错误,即出现意外的标记“

这是我的根package.json测试脚本:

"test": "npx karma start --coverage"

这是我的karma.conf.js文件(位于存储库的根目录上,因此未定义基本路径):

const defaultConfig = require('@open-wc/testing-karma/esm-config');
const merge = require('deepmerge');

module.exports = (config) => {
   config.set(
      merge(defaultConfig(config), {

        // define where your test files are, make sure to set type to module
        files: [
            { pattern: 'packages/*.tests.ts', type: 'module' },
        ],

        plugins: [
            // load plugin
            require.resolve('@open-wc/karma-esm'),

            // fallback: resolve any karma- plugins
            'karma-*',
        ],

        frameworks: ['esm'],

        esm: {
            babel: false,
            nodeResolve: true,
            fileExtensions: ['.ts', '.scss'],
            customBabelConfig: {
                plugins: [
                    ['@babel/plugin-proposal-decorators', { legacy: true }],
                    ['@babel/plugin-proposal-class-properties', { loose: true }],
                    ['css-modules-transform', { extensions: ['.css', '.scss', '.less'] }],
                ],
                presets: [
                    '@babel/preset-typescript',
                ],
            },
        },

        coverageIstanbulReporter: {
            thresholds: {
                global: {
                    statements: 90,
                    lines: 90,
                    branches: 90,
                    functions: 90,
                },
            },
        },
        captureTimeout: 60000,
        browserDisconnectTolerance: 3, 
        browserDisconnectTimeout : 60000,
        browserNoActivityTimeout : 60000,

    }),
);

return config;
};

除了一些覆盖率阈值以外,它们与建议的配置非常接近。这是命令的输出:

START:
28 10 2019 16:40:50.262:WARN [filelist]: Pattern 
"/Users/ppepperSandbox/Documents/workspace/test-monorepo/__snapshots__/**/*.md" does not 
match any file.
28 10 2019 16:40:50.283:INFO [karma-server]: Karma v4.1.0 server started at 
http://0.0.0.0:9876/
28 10 2019 16:40:50.283:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with 
concurrency unlimited
28 10 2019 16:40:50.286:INFO [launcher]: Starting browser ChromeHeadless
28 10 2019 16:40:50.723:INFO [HeadlessChrome 78.0.3904 (Mac OS X 10.14.6)]: Connected on 
socket fmkN7XNEGqz3pMNNAAAA with id 92870419
HeadlessChrome 78.0.3904 (Mac OS X 10.14.6) ERROR
Uncaught SyntaxError: Unexpected token '<'
at node_modules/source-map-support/browser-source-map-support.js:1:1

SyntaxError: Unexpected token '<'HeadlessChrome 78.0.3904 (Mac OS X 10.14.6) ERROR
Uncaught SyntaxError: Unexpected token '<'
at node_modules/source-map-support/browser-source-map-support.js:1:1

SyntaxError: Unexpected token '<'
Finished in 0.245 secs / 0 secs @ 16:40:50 GMT-0400 (Eastern Daylight Time)

我知道,如果tsconfig.json文件将module设置为ES格式,则可能会发生语法错误。但是我的module设置为CommonJS,我认为这样可以工作。我不能正确克服这个问题吗?有人知道吗?我的tsconfig.json文件如下:

{
    "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "removeComments": true,
    "strict": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "baseUrl": "."
  },
    "include": [
    "packages/**/src/**/*.ts",
    "declarations.d.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

谢谢!

javascript angular karma-runner web-component monorepo
1个回答
0
投票

我以sudo的身份运行测试,即sudo npm run test

....而且有效。去搞清楚!有人在乎为什么会这样吗?我最好的猜测是,从某个时候开始,我开始在全球范围内安装模块……这可能不是一个好主意。

© www.soinside.com 2019 - 2024. All rights reserved.