This guide assumes you've got a project using Detox with Jest, and you want to write your Detox tests in TypeScript.
- Refer to this guide if you need to set up such a project.
We'll be using ts-jest to run Jest tests with TypeScript.
npm install --save-dev typescript ts-jest
Modify your Jest configuration (e2e/config.json
by default) to include the following properties
{
"preset": "ts-jest",
"testEnvironment": "node",
"setupTestFrameworkScriptFile": "./init.ts"
}
NB: this is mostly the same output of running ts-jest config:init
, with setupTestFrameworkScriptFile
being the only added property.
Convert all files in the e2e
directory ending in .js
to .ts
Add typings for Detox, Jest, and Jasmine (the latter two are used in init.ts
), as well as for other modules that you use in your Detox tests.
npm install --save-dev @types/detox @types/jest @types/jasmine
It's recommended that you import the Detox methods instead of their globally defined counterparts, to avoid a typing issue between Detox and Jest. This can be enforced by calling detox.init
with { initGlobals: false }
Your init.ts
would look simliar to this:
import { cleanup, init } from 'detox';
import * as adapter from 'detox/runners/jest/adapter';
const config = require('../package.json').detox;
jest.setTimeout(120000);
jasmine.getEnv().addReporter(adapter);
beforeAll(async () => {
await init(config, { initGlobals: false });
});
beforeEach(async () => {
await adapter.beforeEach();
});
afterAll(async () => {
await adapter.afterAll();
await cleanup();
});
Note: the global constants are still defined in the typings, so using the globals will not result in a tsc
error.
Your tests would then import the Detox methods from the detox
module like so:
import { by, device, expect, element, waitFor } from 'detox';
Note: @types/detox
is maintained by the community and not by Wix.
You should now be able to run your Detox tests, written in TypeScript!
@preetb123
I hope you are well. I am attempting to setup Detox e2e tests on an Expo project but I am having some trouble with
jasmine is not defined
Here's the full stack trace
environment.js
init.ts
.detoxrc.json
My
firstTest.e2e.ts
in package.json I have a "jest" block which looks like;
Here are my devDependencies
I am using a script to get the Exponent.app files for the iOS Expo app."dl_expo_bins": "./scripts/dl_expo_bins"
/scripts/dl_expo_bins
There is also an issue in
detox-expo-helpers
which needs a patch. I am using npm patch-package to make the following changes. This is to stop the emulator just hanging after it opens. More information can be found here;https://stackoverflow.com/questions/63137314/error-running-tests-with-detox-in-expo-react-native-project
When I run;
"test:e2e": "detox test -c ios.sim",
I get the error about jasmine not being defined. Any ideas what I am missing? Could really do with some help and happy to help you out in the future too. I've been working on this for two days and can't figure it out.