Skip to content

Commit

Permalink
feat: add plugin-test
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Mar 12, 2020
1 parent 8e59b8d commit 6e4b969
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 122 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@testing-library/react-hooks": "^3.2.1",
"@types/jest": "^25.1.3",
"@types/node": "^13.7.7",
"@umijs/test": "^3.0.6",
"@umijs/test": "^3.0.7",
"create-test-server": "^3.0.1",
"father-build": "^1.17.2",
"lerna": "^3.20.2",
Expand All @@ -43,7 +43,7 @@
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-test-renderer": "^16.9.0",
"umi": "^3.0.6",
"umi": "^3.0.7",
"yorkie": "^2.0.0"
},
"license": "MIT"
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @umijs/plugin-test

> @umijs/plugin-test.
See our website [@umijs/plugin-test](https://umijs.org/plugins/plugin-test) for more information.

## Install

Using npm:

```bash
$ npm install --save-dev @umijs/plugin-test
```

or using yarn:

```bash
$ yarn add @umijs/plugin-test --dev
```
67 changes: 67 additions & 0 deletions packages/plugin-test/docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# @umijs/plugin-test

## 启用方式

默认开启。

## 介绍

基于 jest 提供 `umi test` 命令。

包含以下特性,

- 支持 lerna 包,可以针对子包测试和生成覆盖率
- 支持 TypeScript
- 内置以下补丁
- core-js/stable
- regenerator-runtime/runtime
- whatwg-fetch
- 支持通过 jest.config.json 和 package.json 中的 jest 属性进行配置,前者优先级更高
- jest.config.json 中的配置项支持函数的形式

## 使用

```bash
$ umi-test

# watch mode
$ umi-test -w
$ umi-test --watch

# collect coverage
$ umi-test --coverage

# print debug info
$ umi-test --debug

# test specified package for lerna package
$ umi-test --package name

# don't do e2e test
$ umi-test --no-e2e
```

## 配置

通过 jest.config.js 实现配置的目的,比如:

```js
module.exports = {
moduleNameMapper: {
'^umi$': require.resolve('umi'),
},
};
```

但有时你会希望保留 umi-test 内置的配置,只做扩展,我们也函数的形式,比如:

```js
module.exports = {
moduleNameMapper(memo) {
return {
...memo,
'^umi$': require.resolve('umi'),
};
},
};
```
33 changes: 33 additions & 0 deletions packages/plugin-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@umijs/plugin-test",
"version": "1.0.0-beta.1",
"description": "@umijs/plugin-test",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"src"
],
"repository": {
"type": "git",
"url": "https://github.com/umijs/plugins"
},
"keywords": [
"umi"
],
"authors": [
"chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
],
"license": "MIT",
"bugs": "http://github.com/umijs/plugins/issues",
"homepage": "https://github.com/umijs/plugins/tree/master/packages/plugin-test#readme",
"peerDependencies": {
"umi": "3.x"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@umijs/test": "^3.0.7"
}
}
46 changes: 46 additions & 0 deletions packages/plugin-test/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { IApi, utils } from 'umi';

export default (api: IApi) => {
api.registerCommand({
name: 'test',
description: 'test with jest',
details: `
$ umi-test
# watch mode
$ umi-test -w
$ umi-test --watch
# collect coverage
$ umi-test --coverage
# print debug info
$ umi-test --debug
# test specified package for lerna package
$ umi-test --package name
# don't do e2e test
$ umi-test --no-e2e
`,
async fn() {
const args = utils.yParser(process.argv.slice(3), {
alias: {
watch: ['w'],
version: ['v'],
},
boolean: ['coverage', 'watch', 'version', 'debug', 'e2e'],
default: {
e2e: true,
},
});

require('@umijs/test')
.default(args)
.catch((e: Error) => {
console.error(utils.chalk.red(e));
process.exit(1);
});
},
});
};
3 changes: 2 additions & 1 deletion packages/preset-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@umijs/plugin-layout": "0.5.1",
"@umijs/plugin-locale": "0.3.0",
"@umijs/plugin-model": "2.1.2",
"@umijs/plugin-request": "2.2.1"
"@umijs/plugin-request": "2.2.1",
"@umijs/plugin-test": "1.0.0-beta.1"
}
}
Loading

0 comments on commit 6e4b969

Please sign in to comment.