Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing jest expect with detox expect

How to mix Jest expect with Detox expect? Here is what I try to do. It seem expect has overrided the jest expect.

await mockServer.mockAnyResponse({
    httpRequest: {
      method: 'POST', 
      path: '/api/register',
    },
    httpResponse: {
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        token: 'xxx',
      }),
    }
});

await element(by.id('name')).typeText('Robert');
await element(by.id('password')).typeText('123456');
await element(by.id('register')).tap();

// Check if endpoint has been called
let result = await mockServer.checkIfRegisterEndPointHasBeenCalled();
expect(result).toBe(true); // <-- how to do something like this?
like image 736
invisal Avatar asked Mar 06 '23 20:03

invisal


2 Answers

This is done in two steps:

  1. When you use detox.init(), pass a false initGlobals parameter, e.g.: detox.init({ initGlobals: false }). This will disable overriding global vars like expect of Jest.
  2. Use detox public variables through const { device, expect } = require('detox'); or a similar ES6 import.
like image 191
noomorph Avatar answered Mar 16 '23 00:03

noomorph


Another alternative is to ignore the fact that Detox overwrote expect, and re-import Jest's expect under a different name.

const jestExpect = require('expect');

like image 39
tom Avatar answered Mar 15 '23 22:03

tom