developit / preact-jsx-chai

:white_check_mark: Add JSX assertions to Chai, with support for Preact Components.
http://npm.im/preact-jsx-chai
MIT License
56 stars 7 forks source link

Setting up tests with preact-cli project #52

Open 01binary opened 7 years ago

01binary commented 7 years ago

Is there an article on setting this up with preact-cli created project? I have no doubt you are very busy so I will keep hacking, but if you know what's happening please let me know.

TL;DR: yarn && yarn test on my branch

$ mocha --recursive --require babel-polyfill --compilers js:babel-register

Gets me:

SyntaxError: d:/Desktop/ultravest-assistant/test/state/Component.tests.js: Unexpected token (12:9)
  10 |  it('any test that uses JSX', () => {
  11 |          // following taken from preact-jsx-chai readme
> 12 |          expect(<div id="1">a</div>).to.eql(<div id="1">a</div>);
     |                 ^
  13 |  });
  14 | });

I have some other tests that don't use JSX and work. I ended up putting babel "env" preset into my package.json because I go to extreme lengths to keep my package root clean, but doing the same with .babelrc didn't make any difference. My package is customized from what preact-cli did, in an attempt to get tests to work:

{
  "name": "ultravest-assistant",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "lint": "eslint src",
    "test": "mocha --recursive --require babel-polyfill --compilers js:babel-register",
    "start": "if-env NODE_ENV=production && npm run -s serve || npm run -s dev",
    "build": "lint && preact build",
    "serve": "preact build && preact serve",
    "dev": "preact watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "eslintConfig": {
    "extends": "eslint-config-synacor"
  },
  "babel": {
    "presets": [ "env" ]
  },
  "devDependencies": {
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-register": "^6.26.0",
    "chai": "^4.1.2",
    "eslint": "^4.6.1",
    "eslint-config-synacor": "^1.1.1",
    "if-env": "^1.0.0",
    "mocha": "^3.5.0",
    "preact-cli": "^1.4.1",
    "sass-lint": "^1.11.1"
  },
  "dependencies": {
    "node-sass": "^4.5.3",
    "preact": "^8.2.5",
    "preact-compat": "^3.17.0",
    "preact-jsx-chai": "^2.2.1",
    "react": "npm:preact-compat",
    "recompose": "^0.25.0",
    "sass-loader": "^6.0.6"
  }
}

I am confused because error message indicates it's getting transpiled through babylon but it's still unhappy.

01binary commented 7 years ago

I found the Nectarine project and used as example 🥇

"babel": {
    "presets": [ "env" ],
    "plugins": [
      [ "transform-react-jsx", { "pragma": "h" } ],
      "transform-decorators-legacy"
    ]
  },

Thanks!

developit commented 6 years ago

^ awesome! We're actually looking at shipping a Jest-based testing setup with preact-cli, and it would support both preact-jsx-chai and preact-render-spy.

01binary commented 6 years ago

I may have a better config now, I will post a link to sample project and my config in this thread

01binary commented 6 years ago

Here's my project, it's pre-CSS so the page doesn't look good yet, but it has Jest tests and preact-render-spy. It also uses Recompose, so it's got a fixture that helps test HOCs generated by that library.

https://github.com/01binary/ultravest-assistant

I think I found a good balance of testing state & mutators separately from state transforms and separately from UI: