获取 ReferenceError:在 react-native ios 上运行 detox 测试时未定义设备
Posted
技术标签:
【中文标题】获取 ReferenceError:在 react-native ios 上运行 detox 测试时未定义设备【英文标题】:Getting ReferenceError: device is not defined when running detox test on react-native ios 【发布时间】:2020-03-09 08:18:10 【问题描述】:我正在尝试将排毒测试添加到工作的 react-native 应用程序中,但在运行 detox test
时出现错误
这是错误消息:
detox test
detox[45074] INFO: [test.js] configuration="ios.sim.debug" reportSpecs=true DETOX_START_TIMESTAMP=1573653557623 node_modules/.bin/jest --config e2e/config.json '--testNamePattern=^((?!:android:).)*$' --maxWorkers 1 "e2e"
● Validation Warning:
Unknown option "setupFilesAfterEnv" with value ["./init.js"] was found.
This is probably a typing mistake. Fixing it will remove this message.
Configuration Documentation:
https://jestjs.io/docs/configuration.html
FAIL e2e/firstTest.spec.jsun...
loading search screen and using search inputText
✕ search screen should be visible (1ms)
✕ should show search filter Menu after tap (1ms)
✕ should show search list after writing in input field
● loading search screen and using search inputText › search screen should
be visible
ReferenceError: device is not defined
1 | describe('loading search screen and using search inputText', () =>
2 | beforeAll(async () =>
> 3 | await device.reloadReactNative();
| ^
4 | );
5 |
6 | it('search screen should be visible', async () =>
at Object._callee$ (firstTest.spec.js:3:13)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
● loading search screen and using search inputText › search screen should be visible
ReferenceError: element is not defined
4 | );
5 |
> 6 | it('search screen should be visible', async () =>
| ^
7 | await expect(element(by.id('searchPageContainer'))).toBeVisible();
8 | );
9 |
at Object._callee2$ (firstTest.spec.js:6:267)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
● loading search screen and using search inputText › should show search filter Menu after tap
ReferenceError: device is not defined
1 | describe('loading search screen and using search inputText', () =>
2 | beforeAll(async () =>
> 3 | await device.reloadReactNative();
| ^
4 | );
5 |
6 | it('search screen should be visible', async () =>
at Object._callee$ (firstTest.spec.js:3:13)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
● loading search screen and using search inputText › should show search filter Menu after tap
ReferenceError: element is not defined
8 | );
9 |
> 10 | it('should show search filter Menu after tap', async () =>
| ^
11 | await element(by.id('filterMenuButton')).tap();
12 | await expect(element(by.id('allMosqueMenuItem'))).toBeVisible();
13 | );
at Object._callee3$ (firstTest.spec.js:10:276)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
● loading search screen and using search inputText › should show search list after writing in input field
ReferenceError: device is not defined
1 | describe('loading search screen and using search inputText', () =>
2 | beforeAll(async () =>
> 3 | await device.reloadReactNative();
| ^
4 | );
5 |
6 | it('search screen should be visible', async () =>
at Object._callee$ (firstTest.spec.js:3:13)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
● loading search screen and using search inputText › should show search list after writing in input field
ReferenceError: element is not defined
13 | );
14 |
> 15 | it('should show search list after writing in input field', async () =>
| ^
16 | await element(by.id('searchTextInput')).typeText('paris fr');
17 | await expect(element(by.id('searchList'))).toBeVisible();
18 | );
at Object._callee4$ (firstTest.spec.js:15:288)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:114:21)
at tryCatch (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:62:40)
at invoke (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:152:20)
at ../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:187:11
at callInvokeWithMethodAndArg (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:186:16)
at AsyncIterator.enqueue (../node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:209:13)
detox[45074] ERROR: [cli.js] Error: Command failed: node_modules/.bin/jest
--config e2e/config.json '--testNamePattern=^((?!:android:).)*$' --maxWorke
rs 1 "e2e"
这是我在 package.json 中的排毒配置:
"detox":
"configurations":
"ios.sim.debug":
"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/MdcIos.app",
"build": "xcodebuild -workspace ios/MdcIos.xcworkspace -scheme MdcIos -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
"type": "ios.simulator",
"device":
"type": "iPhone 11"
,
"test-runner": "jest"
还有 e2e 文件夹中的 config.json:
"setupFilesAfterEnv": ["./init.js"],
"testEnvironment": "node",
"reporters": ["detox/runners/jest/streamlineReporter"],
"verbose": true,
"transform":
"^.+\\.js$": "<rootDir>/../node_modules/react-native/jest/preprocessor.js"
最后是 e2e 中的 init.js:
const detox = require('detox');
const config = require('../package.json').detox;
const adapter = require('detox/runners/jest/adapter');
const specReporter = require('detox/runners/jest/specReporter');
// Set the default timeout
jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);
// This takes care of generating status logs on a per-spec basis. By default, jest only reports at file-level.
// This is strictly optional.
jasmine.getEnv().addReporter(specReporter);
beforeAll(async () =>
await detox.init(config);
);
beforeEach(async () =>
await adapter.beforeEach();
);
afterAll(async () =>
await adapter.afterAll();
await detox.cleanup();
);
谢谢!
【问题讨论】:
【参考方案1】:我发现了问题; package.json 中 detox 配置中的 binaryPath 不好。
"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/APP_NAME_WAS_FALSE.app"
【讨论】:
【参考方案2】:运行adb devices
命令并检查设备ID并将ID赋予package.json中的名称
"detox":
"configurations":
"ios.sim.debug":
"binaryPath": "ios/build/Build/Products/Debug-iphonesimulator/MdcIos.app",
"build": "xcodebuild -workspace ios/MdcIos.xcworkspace -scheme MdcIos -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build",
"type": "ios.simulator",
"name" : 520084b3b6ef7473(device id),
"device":
"type": "iPhone 11"
,
"test-runner": "jest"
【讨论】:
以上是关于获取 ReferenceError:在 react-native ios 上运行 detox 测试时未定义设备的主要内容,如果未能解决你的问题,请参考以下文章
ReferenceError:在开玩笑测试中没有定义 React
ReferenceError:找不到变量:React -- bundle android
React + Jest 测试错误 - ReferenceError: 未定义期望