发布于 2026-01-06 0 阅读
0

Mocha/Chai 与 TypeScript 简介 tsParticles - TypeScript 粒子系统 要求 测试 运行测试

使用 TypeScript 的 Mocha/Chai

介绍

tsParticles - TypeScript 粒子效果器

要求

测试

运行测试

介绍

我想为tsParticles创建一些测试(如果你喜欢的话,请点个赞🌟,它是免费的👀),但我对它们一无所知。

GitHub 标志 tsparticles / tsparticles

tsParticles - 轻松创建高度可定制的 JavaScript 粒子特效、彩带爆炸和烟花动画,并将其用作网站的动画背景。提供适用于 React.js、Vue.js(2.x 和 3.x)、Angular、Svelte、jQuery、Preact、Inferno、Solid、Riot 和 Web Components 的即用型组件。

我开始搜索,找到了一些关于MochaChai在JavascriptTypeScript中编写测试的信息,但我不得不同时阅读多篇文章,因为它们并不完整。

如果您需要对 TypeScript 代码进行单元测试,那么您来对地方了。

开始吧!

要求

在开始编写测试之前,我们需要向项目中添加一些库。

我会使用npm,但你也可以使用yarn(显然,谢谢)。



npm install chai mocha ts-node @types/chai @types/mocha --save-dev


Enter fullscreen mode Exit fullscreen mode

安装完成后,我们就差不多准备就绪了。

在你的package.json 文件中添加一个test,像这样 scripts



"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'"


Enter fullscreen mode Exit fullscreen mode

这将为模块设置正确的标志,如果您已经安装了 commonjs,则可以跳过所有步骤mocha,直到ts-node它仅支持commonjs模块,然后它会使用运行测试mocha

这篇文章有点过时了,请查看更新后的文章,安装过程更简单。

我更喜欢使用tests文件夹而不是.test.ts文件。这需要exclude在你的tsconfig 文件中添加一个键(显然,如果你有 tsconfig 文件的话)。

这是从我的配置中截取的一个示例。



"exclude": [
  "./tests/",
  "./node_modules/",
  "./dist/"
],


Enter fullscreen mode Exit fullscreen mode

一切准备就绪后,我们就可以开始编写测试了。

测试

用它编写测试chai非常容易,我几乎立刻就开始编写了。

让我来看个例子,它比我能写的所有字都快。



import { Options } from '../src/Classes/Options/Options'; // this will be your custom import
import { expect } from 'chai';

describe('Options tests', () => { // the tests container
    it('checking default options', () => { // the single test
        const options = new Options(); // this will be your class

                /* detect retina */
        expect(options.detectRetina).to.be.false; // Do I need to explain anything? It's like writing in English!

        /* fps limit */
        expect(options.fpsLimit).to.equal(30); // As I said 3 lines above

        expect(options.interactivity.modes.emitters).to.be.empty; // emitters property is an array and for this test must be empty, this syntax works with strings too
        expect(options.particles.color).to.be.an("object").to.have.property("value").to.equal("#fff"); // this is a little more complex, but still really clear
    });
});


Enter fullscreen mode Exit fullscreen mode

你可以看到它的语法非常简单,chai这是一个非常简单的测试编写库。

如果你想开始编写自己的测试,这就是你所需要的一切。超级简单。

如果你想了解所有可用的功能,可以在这里chai找到它们

运行测试

测试完成后,我们就可以用一开始准备好的命令来尝试了。



npm test


Enter fullscreen mode Exit fullscreen mode

如果你的操作全部正确,输出结果应该类似于这样。



> tsparticles@1.13.0-alpha.0 test /path/to/your/project/
> env TS_NODE_COMPILER_OPTIONS='{"module": "commonjs" }' mocha -r ts-node/register 'tests/**/*.ts'



  Options tests
    ✓ checking default options
    ✓ check default preset options


  2 passing (16ms)


Enter fullscreen mode Exit fullscreen mode

如果出现故障,您会收到类似这样的消息。



> tsparticles@1.13.0-alpha.0 test /path/to/your/project/
> env TS_NODE_COMPILER_OPTIONS='{"module": "commonjs" }' mocha -r ts-node/register 'tests/**/*.ts'



  Options tests
    1) checking default options
    ✓ check default preset options


  1 passing (48ms)
  1 failing

  1) Options tests
       checking default options:
     AssertionError: expected undefined not to be undefined
      at Context.<anonymous> (tests/Options.ts:19:45)
      at processImmediate (internal/timers.js:456:21)



npm ERR! Test failed.  See above for more details.



Enter fullscreen mode Exit fullscreen mode

错误会阻止任何后续命令,因此您可以在构建脚本之前运行测试,如下所示



"build": "npm test && tsc",


Enter fullscreen mode Exit fullscreen mode

现在可以使用以下命令进行测试和构建



npm run build


Enter fullscreen mode Exit fullscreen mode

如果测试通过,构建将按预期进行;否则,测试失败将停止构建过程。

如果你想查看我的配置,你可以在这里找到所有内容。

测试愉快😏

文章来源:https://dev.to/matteobruni/mocha-chai-with-typescript-37f