Detox: Typescript documentation is missing

Created on 25 Sep 2018  路  5Comments  路  Source: wix/Detox

First, thanks for the great testing framework!

I have seen @types/detox done for types but could not find anywhere how I can run ts files with Detox config? Tried using ts-jest as test-runner but says it's not supported. Do I have to manually transform typescript files to Javascript file before test? I looked everywhere in the documentation but could not anything.

  • Detox: 9.0.1
  • React Native: 0.56.1
  • Node: 8.12.0
  • Device: iPhone 7 Plus Simulator
  • Xcode: 10
  • macOS: 10.13.6
looking for contributors documentation

Most helpful comment

I've created https://github.com/solkaz/ts-detox-example as an example of adding TypeScript to your Detox tests

The trickiest part is ensuring that Detox's typings don't trample over Jest's, specifically for your unit tests. I solved this by putting all of my end-to-end tests in an e2e directory at my project's root directory, then configured my main tsconfig to not include the typings in e2e/node_modules/@types.

Briefly, the steps are as follows:

  • Add TypeScript, ts-jest and all necessary typings to your project
  • Use npx ts-jest config:init to create jest.config.js (must be a .js file), then copy the setupTestFrameworkScriptFile over from config.json; then make this your runner-config in package.json.
  • Convert your js files into ts files

All 5 comments

@jinshin1013, unfortunately, this is not documented, but you could contribute to the knowledge base.
I assume you can examine these examples:

https://github.com/basarat/typescript-book/blob/master/docs/testing/jest.md
https://github.com/facebook/jest/tree/master/examples/typescript

Create a project with detox init -r jest, make sure you can run it, then try installing TypeScript, @types/detox and other required things according to the docs above. It won't work out of the box, of course, but you can see what are the errors and solve them step by step.

I'd appreciate if you summarize your findings as a pull request to the documentation.

I've successfully integrated Detox into a TS + RN + Jest project; for transpiling TS, I used ts-jest, there wasn't much setup otherwise (besides separating the typeRoots for the implementation code and the e2e tests, since I use Jest for my unit tests, and Detox overrides Jest's typedefs). Will document the process in the next few days

I've created https://github.com/solkaz/ts-detox-example as an example of adding TypeScript to your Detox tests

The trickiest part is ensuring that Detox's typings don't trample over Jest's, specifically for your unit tests. I solved this by putting all of my end-to-end tests in an e2e directory at my project's root directory, then configured my main tsconfig to not include the typings in e2e/node_modules/@types.

Briefly, the steps are as follows:

  • Add TypeScript, ts-jest and all necessary typings to your project
  • Use npx ts-jest config:init to create jest.config.js (must be a .js file), then copy the setupTestFrameworkScriptFile over from config.json; then make this your runner-config in package.json.
  • Convert your js files into ts files

The only way is to create a nested project for e2e tests? typescript compiler still outputs TS2451: Cannot redeclare block-scoped variable 'expect'.

This is out of scope for this project. This should come as a community project.

Was this page helpful?
0 / 5 - 0 ratings