feat: 加入jest

This commit is contained in:
tackchen 2021-01-14 18:07:28 +08:00
parent 41112daecb
commit 5bd8f76f5a
8 changed files with 3809 additions and 5 deletions

40
.circleci/config.yml Normal file
View File

@ -0,0 +1,40 @@
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
build:
branches:
only:
- master
- develop
docker:
# specify the version you desire here
- image: circleci/node:latest
# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# - image: circleci/mongo:3.4.4
working_directory: ~/repo
parallelism: 1
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- run: yarn install --no-lockfile
- run: yarn add -W codecov
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules
# run tests!
- run: yarn test --runInBand
- run: ./node_modules/.bin/codecov

View File

@ -2,12 +2,14 @@ module.exports = {
"parser": '@typescript-eslint/parser',
"plugins": [
'@typescript-eslint',
'jest'
],
"env": {
"browser": true,
"es6": true,
"node": true,
"commonjs": true
"commonjs": true,
"jest/globals": true
},
// 接入vue失败 暂且eslint 忽略vue文件
// "extends": [
@ -80,7 +82,7 @@ module.exports = {
"prefer-const": ["error", {
"destructuring": "any",
"ignoreReadBeforeAssign": false
}]
}],
// "vue/script-indent": ["warn", 4, {
// "baseIndent": 1,
// "switchCase": 1

27
jest.config.js Normal file
View File

@ -0,0 +1,27 @@
module.exports = {
preset: 'ts-jest/presets/js-with-babel',
testEnvironment: 'jsdom',
// roots: ['./src/'],
// testEnvironment: 'jest-environment-jsdom-global',
testEnvironmentOptions: {
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
},
collectCoverage: true,
coverageDirectory: './coverage/',
// collectCoverageFrom: [
// 'packages/*/src/**/*.ts'],
verbose: false,
// testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/packages/simulator/'],
// coveragePathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/packages/simulator/'],
globals: {
__TEST__: true,
__VERSION__: require('./package.json').version,
ontouchstart: null
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'json'],
moduleNameMapper: {
'^cnchar$': '<rootDir>/src/cnchar/main',
'^cnchar-types$': '<rootDir>/src/cnchar-types',
'^@cnchar-plugin/(.*?)$': '<rootDir>/src/cnchar/plugin/$1',
},
};

3637
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,7 @@
"publish": "node ./helper/publish.js",
"lint": "eslint src --ext js",
"test": "node test npm",
"jest": "jest",
"dev:docs": "vuepress dev vuepress",
"build:docs": "vuepress build vuepress && node ./helper/build-docs.js"
},
@ -33,6 +34,7 @@
"@babel/preset-env": "^7.5.5",
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@types/jest": "^24.0.11",
"@typescript-eslint/eslint-plugin": "^2.14.0",
"@typescript-eslint/parser": "^2.14.0",
"@vuepress/plugin-back-to-top": "^1.0.0-rc.1",
@ -44,6 +46,7 @@
"eslint": "^6.7.2",
"eslint-loader": "^3.0.3",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-vue": "^7.2.0",
"gulp": "^4.0.2",
"gulp-babel": "^8.0.0",
@ -51,8 +54,10 @@
"gulp-rename": "^1.4.0",
"highlight.js": "^9.15.8",
"husky": "^3.1.0",
"jest": "^24.7.1",
"less": "^3.9.0",
"less-loader": "^5.0.0",
"ts-jest": "^24.0.2",
"ts-loader": "^6.2.1",
"typescript": "^3.7.4",
"vue": "^2.5.2",

View File

@ -0,0 +1,58 @@
import EventEmitter from '.';
test('off指定事件', () => {
const eventEmitter = new EventEmitter();
const mockCallback = jest.fn();
eventEmitter.on('abc', mockCallback);
eventEmitter.off('abc', mockCallback);
eventEmitter.emit('abc');
expect(mockCallback.mock.calls.length).toBe(0);
});
test('off所有对应的事件绑定', () => {
const eventEmitter = new EventEmitter();
const mockCallback = jest.fn();
eventEmitter.on('abc', mockCallback);
eventEmitter.off('abc');
eventEmitter.emit('abc');
expect(mockCallback.mock.calls.length).toBe(0);
});
// test('once是否正确?', () => {
// const eventEmitter = new EventEmitter();
// const mockCallback = jest.fn();
// eventEmitter.once('abc', mockCallback);
// eventEmitter.emit('abc');
// eventEmitter.emit('abc');
// expect(mockCallback.mock.calls.length).toBe(1);
// });
// test('off不指定listener运行是否正确?', () => {
// const mockCallback = jest.fn();
// const eventEmitter = new EventEmitter();
// eventEmitter.on('abc', mockCallback);
// eventEmitter.off('abc');
// eventEmitter.emit('abc');
// expect(mockCallback.mock.calls.length).toBe(0);
// });
// test('连续绑定相同事件, 事件触发次数是否正确?', ()=>{
// const mockCallback = jest.fn();
// const eventEmitter = new EventEmitter();
// eventEmitter.on('abc', mockCallback);
// eventEmitter.on('abc', mockCallback);
// eventEmitter.emit('abc');
// expect(mockCallback.mock.calls.length).toBe(2);
// });
test('destory是否生效?', () => {
const mockCallback = jest.fn();
const eventEmitter = new EventEmitter();
eventEmitter.on('abc', mockCallback);
eventEmitter.destroy();
eventEmitter.emit('abc');
expect(mockCallback.mock.calls.length).toBe(0);
});

30
testUtils/index.ts Normal file
View File

@ -0,0 +1,30 @@
import AnyTouch from '@any-touch/core';
import {GestureSimulator, sleep} from '@any-touch/simulator';
export function create () {
AnyTouch.removeUse();
const el = document.createElement('div');
const at = new AnyTouch(el);
const gs = new GestureSimulator(el);
const mouse = new GestureSimulator(el, {device: 'mouse'});
const mockCB = jest.fn();
const {mock} = mockCB;
const mockCalls = mock.calls;
const {dispatchTouchStart, dispatchTouchCancel, dispatchTouchEnd, dispatchTouchMove} = gs;
return {
GestureSimulator,
dispatchTouchStart,
dispatchTouchCancel,
dispatchTouchEnd,
dispatchTouchMove,
gs,
mouse,
touch: gs,
at,
el,
mockCB,
mock,
AnyTouch,
sleep,
mockCalls
};
}

View File

@ -17,13 +17,20 @@
"removeComments": false,
"jsx": "preserve",
"lib": ["esnext", "dom"],
// "types": ["jest", "puppeteer", "node"],
// "types": ["jest", "node"],
"rootDir": ".",
// "paths": {
// "@vue/*": ["packages/*/src"]
// }
// },
"paths": {
"cnchar": ["src/cnchar/main"],
"cnchar-types": ["src/cnchar-types"],
"@cnchar-plugin/*": ["src/cnchar/plugin/*"],
"@testUtils":["testUtils"]
}
},
"include": [
"testUtils",
"./src/**/*"
],
"exclude": [