Предпосылка
* babel-jest
* @babel/preset-env
* @babel/plugin-proposal-class-properties
npm install jest babel-jest @babel/preset-env @babel/plugin-proposal-class-properties
babel.config.json
{ "presets": ["@babel/preset-env"], "plugins": ["@babel/plugin-proposal-class-properties"] }
package.json
{ "name": "jest-hello-world", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "jest" }, "author": "", "license": "ISC", "dependencies": { "@babel/plugin-proposal-class-properties": "^7.10.4", "@babel/preset-env": "^7.10.4", "babel-jest": "^26.1.0", "jest": "^26.1.0" } }
источник/hello.js
export class Hello { name; returnName() { return this.name; } }
источник/hello.test.js
import {Hello} from "./hello"; test('test returnName if the result is as same as name', ()=>{ const hello = new Hello(); hello.name = "testing"; expect(hello.returnName()).toBe("testing"); })
Выполнить
npm run test
Результат
``` PASS src/hello.test.js √ test returnName if the result is as same as name (1 ms) Test Suites: 1 passed, 1 total Tests: 1 passed, 1 total Snapshots: 0 total Time: 5.564 s, estimated 11 s Ran all test suites. ```