forked from wix/Detox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Screenshots and video recordings of tests (wix#734)
- Loading branch information
Showing
100 changed files
with
4,171 additions
and
559 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,119 @@ | ||
const fs = require('fs'); | ||
const program = require('commander'); | ||
const mochaTemplates = require('./templates/mocha.js'); | ||
const _ = require("lodash"); | ||
const log = require("npmlog"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const program = require("commander"); | ||
const mochaTemplates = require("./templates/mocha"); | ||
const jestTemplates = require("./templates/jest"); | ||
|
||
const PREFIX = "detox-init"; | ||
|
||
program | ||
.option('-r, --runner [runner]', 'Test runner (currently supports mocha)', 'mocha') | ||
.name('detox init') | ||
.description("Scaffolds initial E2E test folder structure for a specific test runner") | ||
.usage('-r <test-runner-name>') | ||
.option( | ||
"-r, --runner <test-runner-name>", | ||
"test runner name (supported values: mocha, jest)" | ||
) | ||
.parse(process.argv); | ||
|
||
function createFile(dir, content) { | ||
if (program.runner) { | ||
main(program); | ||
} else { | ||
program.help(); | ||
} | ||
|
||
function createFolder(dir, files) { | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
|
||
for (const entry of Object.entries(files)) { | ||
const [filename, content] = entry; | ||
createFile(path.join(dir, filename), content); | ||
} | ||
} else { | ||
log.error(PREFIX, "./e2e folder already exists at path: %s", path.resolve(dir)); | ||
} | ||
} | ||
|
||
function createFile(filename, content) { | ||
try { | ||
fs.writeFileSync(dir, content); | ||
console.log(`A file was created in "${dir}" `); | ||
fs.writeFileSync(filename, content); | ||
log.info(PREFIX, "A file was created in: %s", filename); | ||
} catch (e) { | ||
log.error(PREFIX, "Failed to create file in: %s.", filename); | ||
log.error(PREFIX, e); | ||
} | ||
} | ||
|
||
function createMochaFolderE2E() { | ||
createFolder("e2e", { | ||
"mocha.opts": mochaTemplates.runnerConfig, | ||
"init.js": mochaTemplates.initjs, | ||
"firstTest.spec.js": mochaTemplates.firstTest | ||
}); | ||
} | ||
|
||
function createJestFolderE2E() { | ||
createFolder("e2e", { | ||
"config.json": jestTemplates.runnerConfig, | ||
"init.js": jestTemplates.initjs, | ||
"firstTest.spec.js": jestTemplates.firstTest | ||
}); | ||
} | ||
|
||
function parsePackageJson(filepath) { | ||
try { | ||
return require(filepath); | ||
} catch (err) { | ||
return err; | ||
log.error(PREFIX, `Failed to parse ./package.json due to the error:\n%s`, err.message); | ||
} | ||
} | ||
|
||
const dir = './e2e'; | ||
function patchPackageJson(packageJson, runnerName) { | ||
_.set(packageJson, ['detox', 'test-runner'], runnerName); | ||
|
||
function createFolder(firstTestContent, runnerConfig, initjsContent) { | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
createFile("./e2e/mocha.opts", runnerConfig); | ||
createFile("./e2e/init.js", initjsContent); | ||
createFile("./e2e/firstTest.spec.js", firstTestContent) | ||
} else { | ||
return console.log('e2e folder already exists') | ||
log.info(PREFIX, 'Patched ./package.json with the command:'); | ||
log.info(PREFIX, `_.set(packageJson, ['detox', 'test-runner'], "${runnerName}")`); | ||
} | ||
|
||
function savePackageJson(filepath, json) { | ||
try { | ||
fs.writeFileSync(filepath, JSON.stringify(json, null, 2)); | ||
} catch (err) { | ||
log.error(PREFIX, 'Failed to write changes into ./package.json due to the error:\n%s', err.message); | ||
} | ||
} | ||
|
||
switch (program.runner) { | ||
case 'mocha': | ||
createFolder(mochaTemplates.firstTest, mochaTemplates.runnerConfig, mochaTemplates.initjs); | ||
break; | ||
default: | ||
createFolder(mochaTemplates.firstTest, mochaTemplates.runnerConfig, mochaTemplates.initjs); | ||
function patchTestRunnerFieldInPackageJSON(runnerName) { | ||
const packageJsonPath = path.join(process.cwd(), 'package.json'); | ||
const packageJson = parsePackageJson(packageJsonPath); | ||
|
||
if (packageJson) { | ||
patchPackageJson(packageJson, runnerName); | ||
savePackageJson(packageJsonPath, packageJson); | ||
} | ||
} | ||
|
||
function main({ runner }) { | ||
switch (runner) { | ||
case "mocha": | ||
createMochaFolderE2E(); | ||
patchTestRunnerFieldInPackageJSON("mocha"); | ||
break; | ||
case "jest": | ||
createJestFolderE2E(); | ||
patchTestRunnerFieldInPackageJSON("jest"); | ||
break; | ||
default: | ||
log.error(PREFIX, "Convenience scaffolding for `%s` test runner is not supported currently.\n", runner); | ||
log.info(PREFIX, "Supported runners at the moment are `mocha` and `jest`:"); | ||
log.info(PREFIX, "* detox init -r mocha"); | ||
log.info(PREFIX, "* detox init -r jest\n"); | ||
log.info(PREFIX, "If it is not a typo, and you plan to work with `%s` runner, then you have to create test setup files manually.", runner); | ||
log.info(PREFIX, "HINT: Try running one of the commands above, watch what it does, and do the similar steps for your use case."); | ||
|
||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const firstTestContent = `describe('Example', () => { | ||
beforeEach(async () => { | ||
await device.reloadReactNative(); | ||
}); | ||
it('should have welcome screen', async () => { | ||
await expect(element(by.id('welcome'))).toBeVisible(); | ||
}); | ||
it('should show hello screen after tap', async () => { | ||
await element(by.id('hello_button')).tap(); | ||
await expect(element(by.text('Hello!!!'))).toBeVisible(); | ||
}); | ||
it('should show world screen after tap', async () => { | ||
await element(by.id('world_button')).tap(); | ||
await expect(element(by.text('World!!!'))).toBeVisible(); | ||
}); | ||
})`; | ||
|
||
module.exports = firstTestContent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const firstTestContent = require('./firstTestContent'); | ||
const runnerConfig = `{ | ||
"setupTestFrameworkScriptFile": "./init.js" | ||
}`; | ||
|
||
const initjsContent = `const detox = require('detox'); | ||
const config = require('../package.json').detox; | ||
const adapter = require('detox/runners/jest/adapter'); | ||
jest.setTimeout(120000); | ||
jasmine.getEnv().addReporter(adapter); | ||
beforeAll(async () => { | ||
await detox.init(config); | ||
}); | ||
beforeEach(async () => { | ||
await adapter.beforeEach(); | ||
}); | ||
afterAll(async () => { | ||
await adapter.afterAll(); | ||
await detox.cleanup(); | ||
});`; | ||
|
||
exports.initjs = initjsContent; | ||
exports.firstTest = firstTestContent; | ||
exports.runnerConfig = runnerConfig; |
Oops, something went wrong.