【问题标题】:How to require .html files in typescript and test using mocha如何在打字稿中要求 .html 文件并使用 mocha 进行测试
【发布时间】:2019-02-01 05:38:06
【问题描述】:

我正在使用 TypeScript 和 webpack 构建一个应用程序。 在我的 .ts 文件中,我有这样的代码:

let template = require('./example.component.html');
let style = require('./example.component.scss');

我的 Webpack 加载器是这样的:

{
    test: /\.scss$/,
    exclude: [path.resolve(__dirname, 'demo/index.scss')],
    use: ['to-string-loader', 'css-loader', 'postcss-loader', 'sass-loader']
},
{
    test: /\.html$/,
    exclude: /node_modules/,
    use: ['html-loader'],
}

我还有一个 ./test/mocha.opts 文件,内容如下:

--require ts-node/register
--require ./test/css-modules-compiler.js
--recursive
--module commonjs

然后我执行我的测试:

mocha src/**/*.spec.ts

使用 webpack 执行应用程序工作正常。

当我执行测试时,它不知道如何加载 .html 文件,我收到以下错误:

/path/to/project/node_modules/typescript/lib/typescript.js:117452
            throw new Error("Could not find file: '" + fileName + "'.");
                  ^
Error: Could not find file: '/path/to/project/src/example/example.component.html'.

如何正确加载 .html 内容文件并执行我的测试?

【问题讨论】:

    标签: typescript mocha.js


    【解决方案1】:

    回答我自己的问题。我决定使用 karma-runner 来运行我的测试并在使用 mocha 执行测试之前进行转译。

    效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 2018-01-21
      • 2020-01-20
      • 1970-01-01
      相关资源
      最近更新 更多