-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Basic support for Jest runner #335
Changes from 19 commits
d5d93cc
ba08fba
8bf5abd
bcb0174
9157bb0
efe3b39
c3c0077
92a1c49
14d6965
256af2b
afcfc0e
8fde423
8c9dc3b
8092625
dc45bc2
63b4519
35e0f65
3803b47
21690b8
5fcfa4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ const program = require('commander'); | |
const path = require('path'); | ||
const cp = require('child_process'); | ||
program | ||
.option('-r, --runner [runner]', 'Test runner (currently supports mocha)', 'mocha') | ||
.option('-r, --runner [runner]', 'Test runner (currently supports mocha)') | ||
.option('-o, --runner-config [config]', 'Test runner config file', 'mocha.opts') | ||
.option('-l, --loglevel [value]', 'info, debug, verbose, silly, wss') | ||
.option('-c, --configuration [device configuration]', 'Select a device configuration from your defined configurations,' | ||
|
@@ -21,26 +21,54 @@ program | |
const config = require(path.join(process.cwd(), 'package.json')).detox; | ||
const testFolder = config.specs || 'e2e'; | ||
|
||
const loglevel = program.loglevel ? `--loglevel ${program.loglevel}` : ''; | ||
const configuration = program.configuration ? `--configuration ${program.configuration}` : ''; | ||
const cleanup = program.cleanup ? `--cleanup` : ''; | ||
const reuse = program.reuse ? `--reuse` : ''; | ||
const artifactsLocation = program.artifactsLocation ? `--artifacts-location ${program.artifactsLocation}` : ''; | ||
let runner = config.runner || 'mocha'; | ||
|
||
if (typeof program.debugSynchronization === "boolean") { | ||
program.debugSynchronization = 3000; | ||
if (program.runner) { | ||
runner = program.runner; | ||
} | ||
let debugSynchronization = program.debugSynchronization ? `--debug-synchronization ${program.debugSynchronization}` : ''; | ||
|
||
function runMocha() { | ||
const loglevel = program.loglevel ? `--loglevel ${program.loglevel}` : ''; | ||
const configuration = program.configuration ? `--configuration ${program.configuration}` : ''; | ||
const cleanup = program.cleanup ? `--cleanup` : ''; | ||
const reuse = program.reuse ? `--reuse` : ''; | ||
const artifactsLocation = program.artifactsLocation ? `--artifacts-location ${program.artifactsLocation}` : ''; | ||
|
||
let command; | ||
switch (program.runner) { | ||
if (typeof program.debugSynchronization === "boolean") { | ||
program.debugSynchronization = 3000; | ||
} | ||
|
||
const debugSynchronization = program.debugSynchronization ? `--debug-synchronization ${program.debugSynchronization}` : ''; | ||
const command = `node_modules/.bin/mocha ${testFolder} --opts ${testFolder}/${program.runnerConfig} ${configuration} ${loglevel} ${cleanup} ${reuse} ${debugSynchronization} ${artifactsLocation}`; | ||
|
||
console.log(command); | ||
cp.execSync(command, {stdio: 'inherit'}); | ||
} | ||
|
||
function runJest() { | ||
const command = `node_modules/.bin/jest ${testFolder} --runInBand`; | ||
console.log(command); | ||
cp.execSync(command, { | ||
stdio: 'inherit', | ||
env: { | ||
...process.env, | ||
configuration: program.configuration, | ||
loglevel: program.loglevel, | ||
cleanup: program.cleanup, | ||
reuse: program.reuse, | ||
debugSynchronization: program.debugSynchronization ? 3000 : '', | ||
artifactsLocation: program.artifactsLocation | ||
} | ||
}); | ||
} | ||
|
||
switch (runner) { | ||
case 'mocha': | ||
command = `node_modules/.bin/${program.runner} ${testFolder} --opts ${testFolder}/${program.runnerConfig} ${configuration} ${loglevel} ${cleanup} ${reuse} ${debugSynchronization} ${artifactsLocation}`; | ||
runMocha(); | ||
break; | ||
case 'jest': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about passing detox command line args? Will those work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sorry, misread your comment. What do you mean by "detox cli args"? Can you make an example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is, the feature is not 100% usable without it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which flags are important? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rotemmiz what is the goal?
If we talk about the second one, then it already works like this (commander parses arguments before passing them to test runner) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, but we execute a child process and these argument are not being passed into it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, now I see. Indeed, we just need to pass arguments to the child process. Then there will be no point in passing all these flags in the command itself There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like Using ENV vars will imply changes on the mocha part and the way test cases gets parameters overall. UPD: I can update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea @Kureev, thank you so much for your efforts here 👍 |
||
runJest(); | ||
break; | ||
default: | ||
throw new Error(`${program.runner} is not supported in detox cli tools. You can still run your tests with the runner's own cli tool`); | ||
throw new Error(`${runner} is not supported in detox cli tools. You can still run your tests with the runner's own cli tool`); | ||
} | ||
|
||
console.log(command); | ||
cp.execSync(command, {stdio: 'inherit'}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
const _ = require('lodash'); | ||
jest.unmock('process'); | ||
|
||
describe('argparse', () => { | ||
let argparse; | ||
describe('using env variables', () => { | ||
let argparse; | ||
|
||
beforeEach(() => { | ||
jest.mock('minimist'); | ||
const minimist = require('minimist'); | ||
minimist.mockReturnValue({test: 'a value'}); | ||
argparse = require('./argparse'); | ||
}); | ||
beforeEach(() => { | ||
process.env.fooBar = 'a value'; | ||
argparse = require('./argparse'); | ||
}); | ||
|
||
it(`nonexistent key should return undefined result`, () => { | ||
expect(argparse.getArgValue('blah')).not.toBeDefined(); | ||
}); | ||
|
||
it(`nonexistent key should return undefined result`, () => { | ||
expect(argparse.getArgValue('blah')).not.toBeDefined(); | ||
it(`existing key should return a result`, () => { | ||
expect(argparse.getArgValue('foo-bar')).toBe('a value'); | ||
}); | ||
}); | ||
|
||
it(`existing key should return a result`, () => { | ||
expect(argparse.getArgValue('test')).toBe('a value'); | ||
describe('using arguments', () => { | ||
let argparse; | ||
|
||
beforeEach(() => { | ||
jest.mock('minimist'); | ||
const minimist = require('minimist'); | ||
minimist.mockReturnValue({'kebab-case-key': 'a value'}); | ||
argparse = require('./argparse'); | ||
}); | ||
|
||
it(`nonexistent key should return undefined result`, () => { | ||
expect(argparse.getArgValue('blah')).not.toBeDefined(); | ||
}); | ||
|
||
it(`existing key should return a result`, () => { | ||
expect(argparse.getArgValue('kebab-case-key')).toBe('a value'); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,9 @@ npm install --save-dev jest | |
|
||
You should remove `e2e/mocha.opts`, you no longer need it. | ||
|
||
### 3. Write a detox setup file | ||
### 3. Replace generated detox setup file (e2e/init.js) | ||
|
||
```js | ||
// ./jest/setup/e2e.js | ||
const detox = require('detox'); | ||
const config = require('../package.json').detox; | ||
|
||
|
@@ -44,15 +43,15 @@ beforeEach(async () => { | |
Add this part to your `package.json`: | ||
```json | ||
"jest": { | ||
"setupTestFrameworkScriptFile": "<rootDir>/jest/setup.js" | ||
"setupTestFrameworkScriptFile": "./e2e/init.js" | ||
}, | ||
"scripts": { | ||
"test:e2e": "jest __e2e__ --setupTestFrameworkScriptFile=./jest/setup/e2e-tests.js --runInBand", | ||
"test:e2e": "detox test", | ||
"test:e2e:build": "detox build" | ||
} | ||
``` | ||
|
||
We need the `--runInBand` as detox doesn't support parallelism yet. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will running without the flag run tests serially? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the flag here so if you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is stopping detox from parallelism? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a few things, mainly controlling multiple simulators. It's in our roadmap, shouldn't be too hard. |
||
In the `detox` part of your `package.json`, add `"runner": "jest"` to tell detox that you want to use jest runner instead of mocha. | ||
|
||
### Writing Tests | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Kureev , wouldn't this override every number value with 3000 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @rotemmiz! Nice catch, fixed! 😉