Skip to content

Commit

Permalink
Merge pull request #368 from yintyuan/release-3.6.8-app
Browse files Browse the repository at this point in the history
feat: taroize包下单元测试的优化,快照文件的上传,package.json中jest指令更换
  • Loading branch information
qican777 authored Oct 31, 2023
2 parents 221583a + 2814d65 commit a58883c
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/taro-cli-convertor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lint": "eslint src --ext .js --ext .jsx --ext .ts --ext .tsx",
"format": "prettier --write \"{src, __tests__}/**/*.{ts,tsx,js,jsx}\" && eslint src __tests__ --ext .js --ext .jsx --ext .ts --ext .tsx --fix",
"test": "jest",
"test:cov": "jest --coverage && npm run lint"
"test:cov": "jest --coverage"
},
"files": [
"bin",
Expand All @@ -36,7 +36,7 @@
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/taroize/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"testRegex": "(/taro-cli-convertor/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-transformer-wx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
],
"testPathIgnorePatterns": [
"node_modules",
"utils"
"/__tests__/utils.ts"
]
},
"dependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-with-weapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"dev": "rollup -c -w rollup.config.js",
"tsc": "tsc",
"lint": "eslint src --ext .js --ext .jsx --ext .ts --ext .tsx",
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\" && eslint src --ext .js --ext .jsx --ext .ts --ext .tsx --fix"
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\" && eslint src --ext .js --ext .jsx --ext .ts --ext .tsx --fix",
"test": "jest",
"test:cov": "jest --coverage"
},
"dependencies": {
"@babel/runtime": "^7.14.5",
Expand Down
82 changes: 82 additions & 0 deletions packages/taroize/__tests__/__snapshots__/script.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parseScript app.js 1`] = `
"import \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
const {
default: withWeapp
} = require(\\"@tarojs/with-weapp\\");
App({
onLaunch() {
console.log('onLaunch');
}
});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
render() {
return this.props.children;
}
}
export default App;"
`;

exports[`parseScript getAPP || getCurrentPages 1`] = `
"import \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
const {
default: withWeapp
} = require(\\"@tarojs/with-weapp\\");
App({
onLaunch() {
const appInstance = Taro.getApp();
console.log('页面栈信息:', Taro.getCurrentPages());
}
});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
render() {
return this.props.children;
}
}
export default App;"
`;

exports[`parseScript wxml expression 1`] = `
"import { Block, View } from \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
const {
default: withWeapp
} = require(\\"@tarojs/with-weapp\\");
App({
data: {
srt: 'Hello Word!'
}
});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
render() {
return this.props.children;
}
}
export default App;"
`;

exports[`parseScript wxml jsxText 1`] = `
"import { Block } from \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
const {
default: withWeapp
} = require(\\"@tarojs/with-weapp\\");
App({});
@withWeapp(cacheOptions.getOptionsFromCache(), true)
class App extends React.Component {
render() {
return this.props.children;
}
}
export default App;"
`;
33 changes: 33 additions & 0 deletions packages/taroize/__tests__/__snapshots__/template.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`template.ts import 正常情况 import src 为绝对路径 1`] = `"<TemplateDemoTmpl></TemplateDemoTmpl>"`;

exports[`template.ts import 正常情况 import src 为绝对路径 2`] = `
"import { Block, View } from \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
import withWeapp from \\"@tarojs/with-weapp\\";
@withWeapp({})
class TemplateDemoTmpl extends React.Component {
render() {
return <Block><View>模版DEMO</View></Block>;
}
}
export default TemplateDemoTmpl;"
`;

exports[`template.ts import 正常情况 import 引入template 1`] = `"<View><TemplateDemoTmpl></TemplateDemoTmpl></View>"`;

exports[`template.ts import 正常情况 import 引入template 2`] = `
"import { Block, View } from \\"@tarojs/components\\";
import React from \\"react\\";
import Taro from \\"@tarojs/taro\\";
import withWeapp from \\"@tarojs/with-weapp\\";
@withWeapp({})
class TemplateDemoTmpl extends React.Component {
render() {
return <Block><View>模版DEMO</View></Block>;
}
}
export default TemplateDemoTmpl;"
`;
35 changes: 35 additions & 0 deletions packages/taroize/__tests__/__snapshots__/wxml.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`wxml.ts测试 wx:key="index"会转换成key="index" 1`] = `
"data.map((item, index) => {
return <View key={index}><Text>{item.name}</Text></View>;
})"
`;

exports[`wxml.ts测试 wxml 中存在 wxs 1`] = `
Array [
Object {
"module": "wxs_demo",
"src": "./wxs__wxs_demo",
},
]
`;

exports[`wxml.ts测试 wxml 中存在 wxs 2`] = `
"module.exports = {
data: 'wxs demo'
};"
`;

exports[`wxml.ts测试 wxml中image的mode=""会转换成mode 1`] = `"<Image className=\\"img\\" src={imgSrc} mode=\\"scaleToFill\\"></Image>"`;
exports[`wxml.ts测试 wxs模块中的var regexp = getRegExp()转换为var regexp = new RegExp() 1`] = `
Array [
Object {
"module": "wxs_regexp",
"src": "./wxs__wxs_regexp",
},
]
`;
exports[`wxml.ts测试 wxs模块中的var regexp = getRegExp()转换为var regexp = new RegExp() 2`] = `"var regexp = new RegExp();"`;
13 changes: 9 additions & 4 deletions packages/taroize/__tests__/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from '@babel/types'

import { parseScript } from '../src/script'
import { parseWXML, WXS } from '../src/wxml'
import { generateMinimalEscapeCode } from './util'


interface Option {
Expand Down Expand Up @@ -36,8 +37,9 @@ describe('parseScript', () => {
`
option.wxml = { type: 'NullLiteral' }
const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true)
const code = generateMinimalEscapeCode(ast)
expect(ast).toBeTruthy()
expect(ast).toMatchSnapshot()
expect(code).toMatchSnapshot()
})

// 当存在 getAPP 或 getCurrentPages 方法
Expand All @@ -52,7 +54,8 @@ describe('parseScript', () => {
`
option.wxml = { type: 'NullLiteral' }
const ast = parseScript(option.script, option.scriptPath, option.wxml as t.Expression, option.wxses, option.refIds, true)
expect(ast).toMatchSnapshot()
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})

// 当wxml是纯文本 parseWXML的返回值 { wxml } 影响js转换
Expand All @@ -63,7 +66,8 @@ describe('parseScript', () => {
path = 'wxml_jsxText'
const { wxml } = parseWXML(path, wxmlStr)
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true)
expect(ast).toMatchSnapshot()
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})

// 当wxml存在变量 parseWXML的返回值 { wxml } 影响js转换
Expand All @@ -80,6 +84,7 @@ describe('parseScript', () => {
path = 'wxml_expression'
const { wxml } = parseWXML(path, wxmlStr)
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true)
expect(ast).toMatchSnapshot()
const code = generateMinimalEscapeCode(ast)
expect(code).toMatchSnapshot()
})
})
23 changes: 15 additions & 8 deletions packages/taroize/__tests__/template.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseWXML } from '../src/wxml'
import { generateMinimalEscapeCode } from './util'

jest.mock('fs')
const fs = require('fs')
Expand All @@ -12,7 +13,7 @@ describe('template.ts', () => {
})

test('import 引入template', () => {
const wxml = `
const wxmlStr = `
<import src="../template/template"/>
<view>
<template is="template_demo"></template>
Expand All @@ -24,14 +25,17 @@ describe('template.ts', () => {
</template>
`
// 将 template.ts 中的 fs.readFileSync 返回值模拟为常量 template
jest.spyOn(fs,'readFileSync').mockReturnValue(template)
jest.spyOn(fs, 'readFileSync').mockReturnValue(template)
const dirPath = 'import_normal'
const paresResult = parseWXML(dirPath, wxml)
expect(paresResult).toMatchSnapshot()
const { wxml, imports }: any = parseWXML(dirPath, wxmlStr)
const wxmlCode = generateMinimalEscapeCode(wxml)
const importsCode = generateMinimalEscapeCode(imports[0].ast)
expect(wxmlCode).toMatchSnapshot()
expect(importsCode).toMatchSnapshot()
})

test('import src 为绝对路径', () => {
const wxml = `
const wxmlStr = `
<import src="/pages/template/template"/>
<template is="template_demo"></template>
`
Expand All @@ -43,10 +47,13 @@ describe('template.ts', () => {
jest.spyOn(path, 'resolve').mockReturnValue('E:\\code\\taro_demo\\pages\\template\\template')
jest.spyOn(path, 'relative').mockReturnValue('../template/template')
jest.spyOn(fs, 'readFileSync').mockReturnValue(template)
jest.spyOn(fs,'existsSync').mockReturnValue(true)
jest.spyOn(fs, 'existsSync').mockReturnValue(true)
const dirPath = 'import_absoulte_path'
const paresResult = parseWXML(dirPath, wxml)
expect(paresResult).toMatchSnapshot()
const { wxml, imports }: any = parseWXML(dirPath, wxmlStr)
const wxmlCode = generateMinimalEscapeCode(wxml)
const importsCode = generateMinimalEscapeCode(imports[0].ast)
expect(wxmlCode).toMatchSnapshot()
expect(importsCode).toMatchSnapshot()
})
})

Expand Down
11 changes: 11 additions & 0 deletions packages/taroize/__tests__/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import generate from '@babel/generator'
import * as t from '@babel/types'

// 最低限度的转义: https://github.com/mathiasbynens/jsesc#minimal
export function generateMinimalEscapeCode (ast: t.File) {
return generate(ast as any, {
jsescOption: {
minimal: true,
},
}).code
}
17 changes: 11 additions & 6 deletions packages/taroize/__tests__/wxml.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from '@babel/types'

import { convertStyleUnit, parseContent, parseStyle, parseWXML } from '../src/wxml'
import { generateMinimalEscapeCode } from './util'

interface Option {
path: string
Expand Down Expand Up @@ -55,16 +56,18 @@ describe('wxml.ts测试', () => {
<view>{{wxs_demo.data}}</view>`
// parseWXML会先获取缓存,所有每个用例的path需要保持其唯一性
option.path = 'wxml_wxs'
const { wxses, imports } = parseWXML(option.path, option.wxml)
const { wxses, imports }:any = parseWXML(option.path, option.wxml)
const importsCode = generateMinimalEscapeCode(imports[0].ast)
expect(wxses).toMatchSnapshot()
expect(imports).toMatchSnapshot()
expect(importsCode).toMatchSnapshot()
})

test('wxml中image的mode=""会转换成mode', () => {
option.wxml = `<image class="img" src="{{imgSrc}}" mode=""></image>`
option.path = 'wxml_mode'
const { wxml }: any = parseWXML(option.path, option.wxml)
expect(wxml).toMatchSnapshot()
const { wxml }:any = parseWXML(option.path, option.wxml)
const wxmlCode = generateMinimalEscapeCode(wxml)
expect(wxmlCode).toMatchSnapshot()
})

test('wx:key="index"会转换成key="index"', () => {
Expand All @@ -73,7 +76,8 @@ describe('wxml.ts测试', () => {
</view>`
option.path = 'wxml_key'
const { wxml }: any = parseWXML(option.path, option.wxml)
expect(wxml).toMatchSnapshot()
const wxmlCode = generateMinimalEscapeCode(wxml)
expect(wxmlCode).toMatchSnapshot()
})

test('wxs模块中的var regexp = getRegExp()转换为var regexp = new RegExp()', () => {
Expand All @@ -82,8 +86,9 @@ describe('wxml.ts测试', () => {
</wxs>`
option.path = 'wxml_wxs_regexp'
const { wxses, imports }: any = parseWXML(option.path, option.wxml)
const importsCode = generateMinimalEscapeCode(imports[0].ast)
expect(wxses).toMatchSnapshot()
expect(imports).toMatchSnapshot()
expect(importsCode).toMatchSnapshot()
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/taroize/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"node"
],
"testPathIgnorePatterns": [
"node_modules"
"/node_modules/",
"/__tests__/util.ts"
]
},
"author": "yuche",
Expand Down

0 comments on commit a58883c

Please sign in to comment.