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: 新增build、create、info、inspect、update、init 命令用例和修改过期语法 #16356

Open
wants to merge 3 commits into
base: feat/test-optimize
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
46 changes: 43 additions & 3 deletions packages/taro-cli/src/__tests__/build-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'node:path'

import { emptyDirectory } from '@tarojs/helper'
import { chalk, emptyDirectory } from '@tarojs/helper'

import { run } from './utils'

Expand Down Expand Up @@ -39,6 +39,46 @@ describe('构建配置测试', () => {
emptyDirectoryMocked.mockReset()
})

it("should exit because there isn't a Taro project", async () => {
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
const logSpy = jest.spyOn(console, 'log')

exitSpy.mockImplementation(() => {
throw new Error()
})
logSpy.mockImplementation(() => {})

try {
await runBuild('')
} catch (error) {} // eslint-disable-line no-empty

expect(exitSpy).toHaveBeenCalledWith(1)
expect(logSpy).toHaveBeenCalledWith(chalk.red('找不到项目配置文件config/index,请确定当前目录是 Taro 项目根目录!'))

exitSpy.mockRestore()
logSpy.mockRestore()
})

it('should Please enter the correct compilation type', async () => {
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
const logSpy = jest.spyOn(console, 'log')

exitSpy.mockImplementation(() => {
throw new Error()
})
logSpy.mockImplementation(() => {})

try {
await runBuild(path.resolve(__dirname, 'fixtures/default'))
} catch (error) {} // eslint-disable-line no-empty

expect(exitSpy).toHaveBeenCalledWith(0)
expect(logSpy).toHaveBeenCalledWith(chalk.red('请传入正确的编译类型!'))

exitSpy.mockRestore()
logSpy.mockRestore()
})

describe('小程序', () => {
it(`项目 output.clean = clean: { keep: ['project.config.json'] } ==> 清空dist文件夹但保留指定文件`, async () => {
const exitSpy = jest.spyOn(process, 'exit') as jest.SpyInstance<void, any>
Expand All @@ -60,7 +100,7 @@ describe('构建配置测试', () => {
} catch (error) {
// no handler
}
expect(emptyDirectoryMocked).toBeCalledWith(OUTPUT_PATH, { excludes: ['project.config.json'] })
expect(emptyDirectoryMocked).toHaveBeenCalledWith(OUTPUT_PATH, { excludes: ['project.config.json'] })

exitSpy.mockRestore()
logSpy.mockRestore()
Expand Down Expand Up @@ -89,7 +129,7 @@ describe('构建配置测试', () => {
} catch (error) {
// no handler
}
expect(emptyDirectoryMocked).toBeCalledTimes(0)
expect(emptyDirectoryMocked).not.toHaveBeenCalled()

exitSpy.mockRestore()
logSpy.mockRestore()
Expand Down
203 changes: 203 additions & 0 deletions packages/taro-cli/src/__tests__/cli-build-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
import { Kernel } from '@tarojs/service'

import CLI from '../cli'

jest.mock('@tarojs/helper', () => ({
__esModule: true,
...jest.requireActual('@tarojs/helper'),
}))
jest.mock('@tarojs/service')
const MockedKernel = (Kernel as unknown) as jest.MockedClass<typeof Kernel>

function setProcessArgv (cmd: string) {
// @ts-ignore
process.argv = [null, ...cmd.split(' ')]
}

// Common expectation setup
const expectCommonInvocation = (options: Record<string, any>, additionalArgs: Record<string, any>, name?: string) => {
const ins = MockedKernel.mock.instances[0]
expect(ins.run).toHaveBeenCalledWith({
name: 'build',
opts: {
_: ['build', ...(name ? [name] : [])],
options: {
args: {
_: ['build', ...(name ? [name] : [])],
build: true,
check: true,
h: false,
help: false,
v: false,
version: false,
type: 'weapp',
'disable-global-config': false,
'inject-global-style': true,
...additionalArgs
},
assetsDest: undefined,
blended: false,
bundleOutput: undefined,
deviceType: undefined,
env: undefined,
isBuildNativeComp: false,
isWatch: false,
newBlended: false,
noCheck: false,
noInjectGlobalStyle: false,
platform: 'weapp',
plugin: undefined,
port: undefined,
publicPath: undefined,
qr: false,
resetCache: false,
sourceMapUrl: undefined,
sourcemapOutput: undefined,
sourcemapSourcesRoot: undefined,
withoutBuild: false,
...options
},
isHelp: false,
},
})
}

const taroBuildScenarios = [
{
cmd: 'taro build --type weapp',
options: {},
additionalArgs: {}
},
{
cmd: 'taro build --type weapp --watch',
options: { isWatch: true },
additionalArgs: { watch: true },
},
{
cmd: 'taro build --type weapp --env production',
options: { env: 'production' },
additionalArgs: { env: 'production' }
},
{
cmd: 'taro build --type weapp --blended',
options: { blended: true },
additionalArgs: { blended: true }
},
{
cmd: 'taro build --type weapp --no-build',
options: { withoutBuild: true },
additionalArgs: { build: false }
},
{
cmd: 'taro build --type weapp --new-blended',
options: { newBlended: true },
additionalArgs: { 'new-blended': true }
},
{
cmd: 'taro build --type weapp -p 80',
options: { port: 80 },
additionalArgs: { port: 80, p: 80 }
},
{
cmd: 'taro build --type weapp --no-check',
options: { noCheck: true },
additionalArgs: { check: false }
},
{
cmd: 'taro build --type weapp --mode prepare --env-prefix TARO_APP_',
options: {},
additionalArgs: { mode: 'prepare', envPrefix: 'TARO_APP_', 'env-prefix': 'TARO_APP_' },
},
{
cmd: 'taro build --plugin weapp',
options: { platform: 'plugin', plugin: 'weapp' },
additionalArgs: { type: undefined, plugin: 'weapp' },
},
{
cmd: 'taro build --plugin weapp --watch',
options: { platform: 'plugin', plugin: 'weapp', isWatch: true },
additionalArgs: { type: undefined, plugin: 'weapp', watch: true },
},
{
cmd: 'taro build native-components --type weapp',
options: { isBuildNativeComp: true },
additionalArgs: {},
_name: 'native-components'
},
{
cmd: 'taro build --type rn --platform ios',
options: { platform: 'rn', deviceType: 'ios' },
additionalArgs: { type: 'rn', platform: 'ios' },
},
{
cmd: 'taro build --type rn --reset-cache',
options: { platform: 'rn', resetCache: true },
additionalArgs: { type: 'rn', 'reset-cache': true, resetCache: true },
},
{
cmd: 'taro build --type rn --public-path',
options: { platform: 'rn', publicPath: true },
additionalArgs: { type: 'rn', 'public-path': true, publicPath: true },
},
{
cmd: 'taro build --type rn --bundle-output',
options: { platform: 'rn', bundleOutput: true },
additionalArgs: { type: 'rn', 'bundle-output': true, bundleOutput: true },
},
{
cmd: 'taro build --type rn --sourcemap-output',
options: { platform: 'rn', sourcemapOutput: true },
additionalArgs: { type: 'rn', 'sourcemap-output': true, sourcemapOutput: true },
},
{
cmd: 'taro build --type rn --sourcemap-use-absolute-path',
options: { platform: 'rn', sourceMapUrl: true },
additionalArgs: { type: 'rn', 'sourcemap-use-absolute-path': true, sourceMapUrl: true },
},
{
cmd: 'taro build --type rn --sourcemap-sources-root',
options: { platform: 'rn', sourcemapSourcesRoot: true },
additionalArgs: { type: 'rn', 'sourcemap-sources-root': true, sourcemapSourcesRoot: true },
},
{
cmd: 'taro build --type rn --assets-dest',
options: { platform: 'rn', assetsDest: true },
additionalArgs: { type: 'rn', 'assets-dest': true, assetsDest: true },
},
{
cmd: 'taro build --type rn --qr',
options: { platform: 'rn', qr: true },
additionalArgs: { type: 'rn', qr: true },
},
{
cmd: 'taro build --type h5 --no-inject-global-style',
options: { platform: 'h5', noInjectGlobalStyle: true },
additionalArgs: { type: 'h5', 'inject-global-style': false },
}
]

describe('create command', () => {
let cli
const appPath = '/'

beforeAll(() => {
cli = new CLI(appPath)
})

beforeEach(() => {
MockedKernel.mockClear()
process.argv = []
})

afterEach(() => {
MockedKernel.mockClear()
process.argv = []
})


it.each(taroBuildScenarios)(`should build the project for $cmd scenario`, async ({ cmd, options, additionalArgs, _name = undefined }) => {
setProcessArgv(cmd)
await cli.run()
expectCommonInvocation(options, additionalArgs, _name)
})
})
87 changes: 87 additions & 0 deletions packages/taro-cli/src/__tests__/cli-create-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Kernel } from '@tarojs/service'

import CLI from '../cli'

jest.mock('@tarojs/helper', () => ({
__esModule: true,
...jest.requireActual('@tarojs/helper'),
}))
jest.mock('@tarojs/service')
const MockedKernel = (Kernel as unknown) as jest.MockedClass<typeof Kernel>

function setProcessArgv(cmd: string) {
// @ts-ignore
process.argv = [null, ...cmd.split(' ')]
}

// Common expectation setup
const expectCommonInvocation = (options?: Record<string, any>, name?: string) => {
const ins = MockedKernel.mock.instances[0]
expect(ins.run).toHaveBeenCalledWith({
name: 'create',
opts: {
_: ['create', ...(name ? [name] : [])],
options: {
build: true,
check: true,
'inject-global-style': true,
type: undefined,
...options
},
isHelp: false,
},
})
}

describe('create command', () => {
let cli
const appPath = '/'

beforeAll(() => {
cli = new CLI(appPath)
})

beforeEach(() => {
MockedKernel.mockClear()
process.argv = []
})

afterEach(() => {
MockedKernel.mockClear()
process.argv = []
})

it('creates a default project', async () => {
setProcessArgv(`taro create page`)
await cli.run()
expectCommonInvocation({}, 'page')
})

it.each([
{
cmd: '--name page',
options: { name: 'page' }
},
{
cmd: '--name page --description=desc',
options: { name: 'page', description: 'desc' }
},
{
cmd: '--name page --type=plugin-command',
options: { name: 'page', type: 'plugin-command' }
},
{
cmd: '--name page --dir src',
options: { name: 'page', dir: 'src' }
},
{
cmd: '--name page --subpkg src',
options: { name: 'page', subpkg: 'src' }
},
])('creates a project with options: $cmd', async ({ cmd, options }) => {
setProcessArgv(`taro create ${cmd}`)

await cli.run()
expectCommonInvocation(options)
})
})
Loading