简体   繁体   English

如何设置 mocha 以在 Gatsby 存储库中运行测试

[英]How to setup mocha to run tests in a Gatsby repo

I'm trying to get tests to run in a gatsby repo using Mocha, because we already have a lot of tests using mocha and chai, and we don't want to have 2 different assertion libraries, so we're not using Jest.我正在尝试使用 Mocha 让测试在 gatsby 存储库中运行,因为我们已经有很多使用 mocha 和 chai 的测试,而且我们不希望有 2 个不同的断言库,所以我们没有使用 Jest。

The first thing I did, is this:我做的第一件事是这样的:

npm i -D mocha chai @testing-library/react

Which installs mocha v8 and chai v4, Then I add a naive script in my package.json to see what happens:其中安装了 mocha v8 和 chai v4,然后我在 package.json 中添加了一个简单的脚本,看看会发生什么:

"scripts": {
  "test": "mocha --watch"
}

This gives me an error: unexpected token import for import { expect } from 'chai';这给了我一个错误: import { expect } from 'chai'; unexpected token import in my bare-bones test file.在我的基本测试文件中。 So next step, following Gatsby's conventions:所以下一步,遵循盖茨比的约定:

"scripts": {
  "test": "npx --node-arg '-r esm' mocha --watch"
}

Okay, we're live, but no tests are running, next iteration:好的,我们已经上线了,但没有运行测试,下一次迭代:

"scripts": {
  "test": "npx --node-arg '-r esm' mocha --watch 'src/**'"
}

Alright, now it crashes because of SyntaxError: Invalid or unexpected token for a <div> in a react component file.好的,现在它因为SyntaxError: Invalid or unexpected token for a <div> in react 组件文件而崩溃。

At this point, I wonder if I really have to install babel and all of its machinery just to run a simple test, especially since gatsby does not use babel at all?在这一点上,我想知道我是否真的必须安装 babel 及其所有机器来运行一个简单的测试,特别是因为 gatsby 根本不使用 babel?

Does someone know of a really clean, modern setup that makes writing tests with mocha in Gatsby simple?有人知道一个非常干净、现代的设置,它可以让在 Gatsby 中使用 mocha 编写测试变得简单吗? Can esm be taught to read JSX without a pile of hacks?可以教esm在没有一堆 hack 的情况下阅读 JSX 吗?

At this point, I wonder if I really have to install babel and all of its machinery just to run a simple test在这一点上,我想知道我是否真的必须安装 babel 及其所有机器来运行一个简单的测试

Unfortunately, yes.不幸的是,是的。

, especially since gatsby does not use babel at all? ,特别是因为 gatsby 根本不使用 babel?

Actually that's not true, it's babel all the way down.其实这不是真的,它一直是babel。


Thankfully, since Gatsby has already done all the tedious work of setting up babel, it's rather easy to set up your tests.值得庆幸的是,由于 Gatsby 已经完成了设置 babel 的所有繁琐工作,因此设置测试相当容易。

First, install the missing bits:首先,安装缺少的位:

npm i -D @babel/register jsdom jsdom-global

Then add a fresh .babelrc at the root of the project with the following config:然后使用以下配置在项目的根目录添加一个新的.babelrc

{
  "presets": ["babel-preset-gatsby"]
}

You don't need to install babel-preset-gatsby since it comes with all Gatsby setup.你不需要安装babel-preset-gatsby ,因为它带有所有 Gatsby 设置。

That should be it.应该是这样的。 Now replace esm with @babel/register & register jsdom-global .现在用@babel/register替换esm并注册jsdom-global Also make sure you set your NODE_ENV to test :还要确保将NODE_ENV设置为test

NODE_ENV=test npx mocha -r @babel/register -r jsdom-global/register ./src/**/*.test.js

And that should work.这应该有效。


When you add the test command to package.json , no needs to use npx:将测试命令添加到package.json时,无需使用 npx:

"scripts": {
  "test": "NODE_ENV=test mocha -r @babel/register -r jsdom-global/register ./src/**/*.test.js"
}

If you're testing Gatsby components, here's a few global variables you need to watch out for: __PATH_PREFIX__ , __BASE_PATH__ , and ___loader .如果你正在测试 Gatsby 组件,这里有一些你需要注意的全局变量: __PATH_PREFIX____BASE_PATH_____loader

Mock them however you'd like, but for a quick and dirty test:随心所欲地模拟它们,但要进行快速而肮脏的测试:

globalThis.__PATH_PREFIX__ = '/'
globalThis.__BASE_PATH__ = './'
globalThis.___loader = { enqueue: () => {} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM