mardi 7 juillet 2020

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.

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:

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

This gives me an error: unexpected token import for import { expect } from 'chai'; 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.

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?

Does someone know of a really clean, modern setup that makes writing tests with mocha in Gatsby simple? Can esm be taught to read JSX without a pile of hacks?

Aucun commentaire:

Enregistrer un commentaire