m***@126.com
m***@126.com
  • 发布:2022-06-20 15:19
  • 更新:2022-06-29 13:05
  • 阅读:2269

uniapp vue3 cli项目自动化测试报错TypeError: Cannot read properties of undefined (reading 'platform')

分类:uni-app

uniapp vue3 cli项目集成自动化测试,h5、ios和android的自动化测试出错,信息如下:

$ cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=ios jest -i  
 FAIL  src/pages/index/index.test.js  
  ● Test suite failed to run  

    TypeError: Cannot read properties of undefined (reading 'platform')  

      at module.exports.setup (node_modules/@dcloudio/uni-automator/dist/environment.js:1:476)  
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:317:13)  
      at runJest (node_modules/@jest/core/build/runJest.js:407:19)  
      at _run10000 (node_modules/@jest/core/build/cli/index.js:339:7)  
      at runCLI (node_modules/@jest/core/build/cli/index.js:190:3)

请问,上述问题如何解决?uniapp vue3项目怎样才能正确集成自动化测试?

附上项目创建流程。

  1. 参考官网说明(https://uniapp.dcloud.io/quickstart-cli.html#%E5%88%9B%E5%BB%BAuni-app),
    使用npx degit dcloudio/uni-preset-vue#vite-ts my-vue3-project创建项目
  2. 参考自动化测试uni-app cli项目说明(https://uniapp.dcloud.io/worktile/auto/uniapp-cli-project.html),安装了依赖包。
  3. 创建了jest.config.js及相应测试代码
    以下是最终package.json文件内容
    {  
    "name": "uni-preset-vue",  
    "version": "0.0.0",  
    "scripts": {  
    "dev:app": "uni -p app",  
    "dev:custom": "uni -p",  
    "dev:h5": "uni",  
    "dev:h5:ssr": "uni --ssr",  
    "dev:mp-alipay": "uni -p mp-alipay",  
    "dev:mp-baidu": "uni -p mp-baidu",  
    "dev:mp-kuaishou": "uni -p mp-kuaishou",  
    "dev:mp-lark": "uni -p mp-lark",  
    "dev:mp-qq": "uni -p mp-qq",  
    "dev:mp-toutiao": "uni -p mp-toutiao",  
    "dev:mp-weixin": "uni -p mp-weixin",  
    "dev:quickapp-webview": "uni -p quickapp-webview",  
    "dev:quickapp-webview-huawei": "uni -p quickapp-webview-huawei",  
    "dev:quickapp-webview-union": "uni -p quickapp-webview-union",  
    "build:app": "uni build -p app",  
    "build:custom": "uni build -p",  
    "build:h5": "uni build",  
    "build:h5:ssr": "uni build --ssr",  
    "build:mp-alipay": "uni build -p mp-alipay",  
    "build:mp-baidu": "uni build -p mp-baidu",  
    "build:mp-kuaishou": "uni build -p mp-kuaishou",  
    "build:mp-lark": "uni build -p mp-lark",  
    "build:mp-qq": "uni build -p mp-qq",  
    "build:mp-toutiao": "uni build -p mp-toutiao",  
    "build:mp-weixin": "uni build -p mp-weixin",  
    "build:quickapp-webview": "uni build -p quickapp-webview",  
    "build:quickapp-webview-huawei": "uni build -p quickapp-webview-huawei",  
    "build:quickapp-webview-union": "uni build -p quickapp-webview-union",  
    "test:h5": "cross-env UNI_PLATFORM=h5 jest -i",  
    "test:android": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=android jest -i",  
    "test:ios": "cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=ios jest -i",  
    "test:mp-weixin": "cross-env UNI_PLATFORM=mp-weixin jest -i",  
    "test:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu jest -i"  
    },  
    "dependencies": {  
    "@dcloudio/uni-app": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-app-plus": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-components": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-h5": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-alipay": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-baidu": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-kuaishou": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-lark": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-qq": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-toutiao": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-mp-weixin": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-quickapp-webview": "3.0.0-alpha-3041520220609002",  
    "@playwright/test": "^1.22.2",  
    "adbkit": "^2.11.1",  
    "jest": "^28.1.1",  
    "node-simctl": "^7.0.3",  
    "playwright": "^1.22.2",  
    "puppeteer": "^14.4.1",  
    "vue": "^3.2.33",  
    "vue-i18n": "^9.1.9",  
    "vuex": "^4.0.2"  
    },  
    "devDependencies": {  
    "@dcloudio/types": "^2.6.4",  
    "@dcloudio/uni-automator": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/uni-cli-shared": "3.0.0-alpha-3041520220609002",  
    "@dcloudio/vite-plugin-uni": "3.0.0-alpha-3041520220609002",  
    "cross-env": "^7.0.3",  
    "postcss": "^8.4.14",  
    "sass": "^1.52.3",  
    "typescript": "^4.7.3",  
    "vite": "^2.9.6"  
    }  
    }
2022-06-20 15:19 负责人:DCloud_UNI_Anne 分享
已邀请:
DCloud_UNI_Anne

DCloud_UNI_Anne

相关依赖不要高于以下版本,再试下

jest@27.0.4  
jest-environment-node@27.5.1
  • m***@126.com (作者)

    更新了jest版本后,npm run test:h5 成功,但是npm run test:ios 出现了新的出错信息


    > uni-preset-vue@0.0.0 test:ios  
    > cross-env UNI_PLATFORM=app-plus UNI_OS_NAME=ios jest -i

    node:internal/modules/cjs/loader:936
    throw err;
    ^

    Error: Cannot find module '/Users/xxx/Documents/Work/my-vue3-project/bin/uniapp-cli.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
    }

    2022-06-28 23:24

DCloud_UNI_Anne

DCloud_UNI_Anne

在package.json文件scripts节点下,把UNI_PLATFORM=app-plus 改成 UNI_PLATFORM=app,如下:

    "test:android": "cross-env UNI_PLATFORM=app UNI_OS_NAME=android jest -i",  
    "test:ios": "cross-env UNI_PLATFORM=app UNI_OS_NAME=ios jest -i",
  • m***@126.com (作者)

    改成 UNI_PLATFORM=app后,可以测试了,非常感谢!

    2022-06-29 18:50

DCloud_UNI_LXH

DCloud_UNI_LXH

测试代码怎么写的?

  • m***@126.com (作者)

    测试代码如下


    // uni-app自动化测试教程: uni-app自动化测试教程: https://uniapp.dcloud.io/collocation/auto/hbuilderx-extension/index  

    describe('test title', () => {
    let page
    beforeAll(async () => {
    page = await program.currentPage()
    await page.waitFor(3000)
    })

    it('check page title', async () => {
    const el = await page.$('.title')
    const titleText = await el.text()
    expect(titleText).toEqual('Hello2')
    })
    })

    2022-06-24 18:30

该问题目前已经被锁定, 无法添加新回复