当我们在测试规范文件中使用导入模块时,Mocha测试用例没有在Chrome浏览器上运行

gopyfrb3  于 2023-04-27  发布在  Go
关注(0)|答案(1)|浏览(95)

我们一直在使用mocha和chai为typescript项目编写单元测试用例。但是当我们在测试规范文件中使用import module语句并启动html在浏览器上运行测试用例时,mocha界面加载但没有测试用例在浏览器上运行。它继续显示通过:0次失败:0持续时间:Browser snapshot如果我们转到浏览器中的sources文件夹,那么测试规范文件chai.js和mocha.js将成功加载。官方步骤已经遵循以集成mocha和chai测试用例。
下面是示例代码结构:
index.ts:

export class SampleClass {
  sayHello() {
    return "Hello, world!";
  }
}

SampleSpec.ts

import { SampleClass } from "../src/index.js";
var expect = chai.expect;
var assert = chai.assert;
describe("Sample", () => {
  it("says hello", () => {
    console.log("Inside it block");
    const sample = new SampleClass();
    const result = "Hello";
    expect(result).to.equal(sample.sayHello);
  });
});

index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Test</title>
    <!-- <script src="../node_modules/chai/chai.js"></script>
    <script src="../node_modules/mocha/mocha.js"></script> -->
    <script src="https://cdn.jsdelivr.net/npm/chai@4.2.0/chai.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/mocha@10.2.0/mocha.js"></script>
  </head>
  <body>
    <div id="mocha"></div>
    <script type="module" src="../test/Castor.spec.js"></script>
    <script>
      mocha.setup("bdd");
      mocha.run();
    </script>
  </body>
</html>

tsconfig:

{
  "compilerOptions": {
    "target": "ES2020",                                  
    "module": "ES2020",                                
    "esModuleInterop": true,                            
    "forceConsistentCasingInFileNames": true,            

    "strict": true,                                      
    "skipLibCheck": true                                 
  }
}

注意:它在Safari上运行,并给出了正确的单元测试结果,但没有在Chrome浏览器上运行。
有没有人可以帮助在chrome上运行时这里有什么问题?

ubby3x7f

ubby3x7f1#

当我将一个three.js CLI mocha套件移动到浏览器中时,就发生了这种情况,我能够通过在工作的www.example.com()调用中添加type="module"来对其进行排序mocha.run。
我有更广泛的问题,为什么会是这样。

相关问题