Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 单测 #70

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
npm i @mpxjs/cli -g
```

> 如果插件版本是2.x以上,需要前往https://github.com/mpx-ecology/mpx-cli/tree/next 查看文档

## 使用

```sh
Expand Down
98 changes: 98 additions & 0 deletions packages/mpx-cli/__tests__/__snapshots__/preset.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`test-ali 1`] = `
Object {
"staticAliDir": Array [
"/Users/didi/Desktop/work/mpx-cli/packages/test/test-ali/static/ali",
],
"staticWxDir": Array [],
}
`;

exports[`test-plugin-combine 1`] = `
Object {
"dependencies": Object {
"@mpxjs/api-proxy": "^2.8.0",
"@mpxjs/core": "^2.8.0",
"@mpxjs/fetch": "^2.8.0",
"@mpxjs/pinia": "^2.8.0",
"@mpxjs/store": "^2.8.0",
"@mpxjs/utils": "^2.8.0",
"pinia": "^2.0.14",
"vue": "^2.6.14",
"vue-demi": "^0.13.11",
"vue-i18n": "^8.27.2",
"vue-i18n-bridge": "^9.2.2",
"vue-router": "^3.1.3",
},
"devDependencies": Object {
"@babel/core": "^7.10.4",
"@babel/plugin-transform-runtime": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/runtime-corejs3": "^7.10.4",
"@mpxjs/babel-plugin-inject-page-events": "^2.8.0",
"@mpxjs/eslint-config": "^1.0.5",
"@mpxjs/miniprogram-simulate": "^1.4.17",
"@mpxjs/mpx-cli-service": "^2.0.0",
"@mpxjs/mpx-jest": "^0.0.24",
"@mpxjs/size-report": "^2.8.0",
"@mpxjs/vue-cli-plugin-mpx": "^2.0.0",
"@mpxjs/vue-cli-plugin-mpx-e2e-test": "^2.0.0",
"@mpxjs/vue-cli-plugin-mpx-eslint": "^2.0.0",
"@mpxjs/vue-cli-plugin-mpx-typescript": "^2.0.0",
"@mpxjs/vue-cli-plugin-mpx-unit-test": "^2.0.0",
"@mpxjs/webpack-plugin": "^2.8.0",
"@vue/cli-service": "latest",
"autoprefixer": "^10.2.4",
"babel-jest": "^27.4.5",
"eslint": "^7.0.0",
"jest": "^27.4.5",
"postcss": "^8.2.6",
"stylus": "^0.55.0",
"stylus-loader": "^6.1.0",
"typescript": "^4.1.3",
"vue-template-compiler": "^2.6.14",
"webpack": "^5.43.0",
},
"name": "test-plugin-combine",
"private": true,
"scripts": Object {
"build": "mpx-cli-service build",
"build:e2e": "npm run build && npm run test:e2e",
"lint": "eslint --ext .js,.ts,.mpx src/",
"serve": "mpx-cli-service serve",
"test": "jest test/components",
"test:e2e": "npx e2e-runner j---config=./jest-e2e.config.js",
},
"version": "0.1.0",
}
`;

exports[`test-wx 1`] = `
Object {
"staticAliDir": true,
"staticDdDir": true,
"staticQqDir": false,
"staticSwanDir": true,
"staticTtDir": true,
"staticWxDir": true,
}
`;

exports[`test-wx 2`] = `
Object {
"appid": "test-plugin-have-appid",
"condition": Object {},
"description": "test-plugin-have-description",
"projectname": "test-wx",
"setting": Object {
"autoAudits": false,
"checkSiteMap": false,
"es6": false,
"minified": false,
"newFeature": true,
"postcss": true,
"urlCheck": true,
},
}
`;
163 changes: 161 additions & 2 deletions packages/mpx-cli/__tests__/preset.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
jest.mock('inquirer')

const glob = require('glob')
const path = require('path')
const fs = require('fs')
const create = require('@mpxjs/cli/lib/create')
Expand All @@ -23,7 +23,6 @@ test('normal', async () => {
useConfigFiles: true
}
)

const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx')
})
Expand Down Expand Up @@ -228,3 +227,163 @@ test('test-e2e', async () => {
const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx-e2e-test')
})

// 封装一个方法,用来生成项目,然后将配置项作为参数传入
const createProject = async (name, options) => {
const cwd = path.resolve(__dirname, '../../test')
await create(
name,
{
force: true,
git: false,
cwd
},
options
// srcMode: 'wx' 表示小程序的类型是微信小程序
// appid: 'test-yao-1.0.0' 表示小程序的appid
// description: 'test' 表示小程序的描述
// cross: true 表示是否跨平台
// needE2ETest: true 表示是否需要e2e测试
// plugins: {} 表示插件
// useConfigFiles: true 表示是否使用配置文件
)
}

// wx模式
test('test-wx', async () => {
const cwd = path.resolve(__dirname, '../../test')
const name = 'test-wx'
await createProject(name, {
srcMode: 'wx',
appid: 'test-plugin-have-appid',
description: 'test-plugin-have-description',
cross: true,
needE2ETest: true,
plugins: {},
useConfigFiles: true
})
// 生成的目录下是否包含了wx目录,ali目录,swan目录,tt目录,dd目录
const staticWxDir = fs.existsSync(path.resolve(cwd, name, 'static/wx'))
const staticAliDir = fs.existsSync(path.resolve(cwd, name, 'static/ali'))
const staticSwanDir = fs.existsSync(path.resolve(cwd, name, 'static/swan'))
const staticQqDir = fs.existsSync(path.resolve(cwd, name, 'static/qq'))
const staticDdDir = fs.existsSync(path.resolve(cwd, name, 'static/dd'))
const staticTtDir = fs.existsSync(path.resolve(cwd, name, 'static/tt'))
expect(staticWxDir).toBe(true)
expect(staticAliDir).toBe(true)
expect(staticSwanDir).toBe(true)
expect(staticQqDir).toBe(false)
expect(staticDdDir).toBe(true)
expect(staticTtDir).toBeTruthy()

// 生成的appid是否是test-plugin-have-appid
const projectConfig = require(path.resolve(cwd, name, 'static/wx/project.config.json'))
expect(projectConfig.appid).toBe('test-plugin-have-appid')

// 生成的description是否是test-plugin-have-description
expect(projectConfig.description).toBe('test-plugin-have-description')

// 保存快照
// 检查生成的目录是否和预期的目录一致
expect({
staticWxDir,
staticAliDir,
staticSwanDir,
staticQqDir,
staticDdDir,
staticTtDir
}).toMatchSnapshot()

// 检查生成的目录下的project.config.json文件是否和预期的一致
expect(projectConfig).toMatchSnapshot()
})

// 测试插件是否存在,wx模式
test('test-plugin-have', async () => {
const cwd = path.resolve(__dirname, '../../test')
const name = 'test-plugin-have'
await createProject(name, {
srcMode: 'wx',
appid: 'test-plugin-have-appid',
description: 'test-plugin-have-description',
cross: true,
needE2ETest: true,
plugins: {},
useConfigFiles: true
})

// 有没有 @mpxjs/babel-plugin-inject-page-events 这个依赖
const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@mpxjs/babel-plugin-inject-page-events')
})

// 将srcMode改成ali,生成的static/ali目录是否存在,还是否存在wx目录
test('test-ali', async () => {
const cwd = path.resolve(__dirname, '../../test')
const name = 'test-ali'
await createProject(name, {
srcMode: 'ali',
appid: 'test-ali-appid',
description: 'test-ali-description',
cross: true,
needE2ETest: true,
plugins: {},
useConfigFiles: true
})

const staticWxDir = glob.sync(path.resolve(cwd, name, 'static/wx'))
const staticAliDir = glob.sync(path.resolve(cwd, name, 'static/ali'))
expect(staticWxDir.length).toBe(0) // 确保 static/wx 目录不存在
expect(staticAliDir.length).toBeGreaterThan(0) // 确保 static/ali 目录存在

// 保存快照
// 检查生成的目录是否和预期的目录一致
expect({
staticWxDir,
staticAliDir
}).toMatchSnapshot()
})

// 插件组合测试
test('test-plugin-combine', async () => {
const cwd = path.resolve(__dirname, '../../test')
const name = 'test-plugin-combine'
await createProject(name, {
srcMode: 'wx',
appid: 'test-plugin-combine-appid',
description: 'test-plugin-combine-description',
cross: true,
needE2ETest: true,
plugins: {
'@mpxjs/vue-cli-plugin-mpx': {
version: '^2.0.0'
},
'@mpxjs/vue-cli-plugin-mpx-typescript': {
version: '^2.0.0'
},
'@mpxjs/vue-cli-plugin-mpx-unit-test': {
version: '^2.0.0'
},
'@mpxjs/vue-cli-plugin-mpx-e2e-test': {
version: '^2.0.0'
}
},
useConfigFiles: true
})

const pkg = require(path.resolve(cwd, name, 'package.json'))
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx')
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx-typescript')
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx-unit-test')
expect(pkg.devDependencies).toHaveProperty('@mpxjs/vue-cli-plugin-mpx-e2e-test')

// 查看上面五个插件的版本是否正确
expect(pkg.devDependencies['@mpxjs/vue-cli-plugin-mpx']).toBe('^2.0.0')
expect(pkg.devDependencies['@mpxjs/vue-cli-plugin-mpx-typescript']).toBe('^2.0.0')
expect(pkg.devDependencies['@mpxjs/vue-cli-plugin-mpx-unit-test']).toBe('^2.0.0')
expect(pkg.devDependencies['@mpxjs/vue-cli-plugin-mpx-e2e-test']).toBe('^2.0.0')

// 保存快照
// 检查生成的文件是否和预期的一致
expect(pkg).toMatchSnapshot()
})
Loading