- use dependencies in
package.json
- use new babel
babel.config.js
(for babel 7+) - use jest config
jest.config.js
- use a setup test file e.g.
setupTest.js
for basic jest configuration likejest-dom
andpretty-format
- use
babel-jest
for transform es6 into commonjs. seetransform
config - exclude node modules that should not be transformed. see
transformIgnorePatterns
config. basic example is thecan
package which is written in es6. submodules likecan-component
are written in commonjs. - use
jest-trailing-slash-resolver
for resolving stealjs'ish module naming with trailing slash../foo/bar/
->./foo/bar/bar
. seeresolver
- use a setup test file e.g.
- use
jest-dom
- name tests like
xxx.test.js
orxxx.spec.js
. phpStorm will jump from source to test withCRTL+SHIFT+T
- use
npx
for calling localjest
. e.g.npx jest --clearCache
- snapshots should be committed to git
- create a Jest phpStorm Configuration to debug tests. add a
--watch
to the config to automatically run tests. see https://blog.jetbrains.com/webstorm/2018/10/testing-with-jest-in-webstorm/ for more info. - add javascript libraries (types) to phpStorm get autocompletion.
Languages & Framworks -> JavaScript -> Libraries
.- @types/jquery
- @types/jest
- jest-dom (add jest-dom.ts file from node_modules)
- import like
import {Component} from "can";
can slow down the test because jest have to transform the whole can package into commonjs. better:
- import the es6 code
import Component from "can/es/can-component";
- even better, import the module directly, since it is written in commonjs.
import Component from "can-component";