tsParticles - TypeScript 粒子效果器
一个轻量级的 TypeScript 粒子系统库。它无依赖 (*),支持浏览器,并兼容 React.js、Vue.js(2.x 和 3.x)、Angular、Svelte、jQuery、Preact、Inferno、Riot.js、Solid.js 和 Web Components。
我想为tsParticles创建一些测试(如果你喜欢的话,请点个赞🌟,它是免费的👀),但我对它们一无所知。
一个轻量级的 TypeScript 粒子系统库。它无依赖 (*),支持浏览器,并兼容 React.js、Vue.js(2.x 和 3.x)、Angular、Svelte、jQuery、Preact、Inferno、Riot.js、Solid.js 和 Web Components。
我开始搜索,找到了一些关于Mocha和Chai在Javascript和TypeScript中编写测试的信息,但我不得不同时阅读多篇文章,因为它们并不完整。
如果您需要对 TypeScript 代码进行单元测试,那么您来对地方了。
开始吧!
在开始编写测试之前,我们需要向项目中添加一些库。
我会使用npm,但你也可以使用yarn(显然,谢谢)。
npm install chai mocha ts-node @types/chai @types/mocha --save-dev
安装完成后,我们就差不多准备就绪了。
在你的package.json 文件中添加一个test键,像这样 scripts
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'"
这将为模块设置正确的标志,如果您已经安装了 commonjs,则可以跳过所有步骤mocha,直到ts-node它仅支持commonjs模块,然后它会使用运行测试mocha。
这篇文章有点过时了,请查看更新后的文章,安装过程更简单。
我更喜欢使用tests文件夹而不是.test.ts文件。这需要exclude在你的tsconfig 文件中添加一个键(显然,如果你有 tsconfig 文件的话)。
这是从我的配置中截取的一个示例。
"exclude": [
"./tests/",
"./node_modules/",
"./dist/"
],
一切准备就绪后,我们就可以开始编写测试了。
用它编写测试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
});
});
你可以看到它的语法非常简单,chai这是一个非常简单的测试编写库。
如果你想开始编写自己的测试,这就是你所需要的一切。超级简单。
如果你想了解所有可用的功能,可以在这里chai找到它们。
测试完成后,我们就可以用一开始准备好的命令来尝试了。
npm test
如果你的操作全部正确,输出结果应该类似于这样。
> 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)
如果出现故障,您会收到类似这样的消息。
> 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.
错误会阻止任何后续命令,因此您可以在构建脚本之前运行测试,如下所示
"build": "npm test && tsc",
现在可以使用以下命令进行测试和构建
npm run build
如果测试通过,构建将按预期进行;否则,测试失败将停止构建过程。
如果你想查看我的配置,你可以在这里找到所有内容。
测试愉快😏
文章来源:https://dev.to/matteobruni/mocha-chai-with-typescript-37f