The following questions will help Jest to create a suitable configuration for your project
✔ Would you like to use Jest when running "test" script in "package.json"? … yes ✔ Choose the test environment that will be used for testing › node ✔ Do you want Jest to add coverage reports? … yes ✔ Automatically clear mock calls and instances between every test? … yes
chris$ npx jest FAIL 2020-03-12/sum.test.js ● Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do: • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config. • If you need a custom transformation specify a "transform" option in your config. • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs: https://jestjs.io/docs/en/configuration.html
Details:
/Users/chris/code/UnitTestByJest/2020-03-12/sum.test.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import sum from "./sum"; ^^^^^^
SyntaxError: Unexpected token import
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1059:14)
Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.846s, estimated 2s Ran all test suites.
在此,確定需要 Babel ,就可以安裝
chris$ npm install -D babel-jest @babel/core @babel/preset-env + babel-jest@25.1.0 + @babel/core@7.8.7 + @babel/preset-env@7.8.7 added 90 packages from 16 contributors, updated 2 packages and audited 1208611 packages in 19.43s
24 packages are looking for funding run `npm fund` for details