Skip to content

Commit e7fde68

Browse files
authored
Build Nightwatch using TypeScript. (nightwatchjs#3774)
* Setup TypeScript. * Add prepublishOnly script. * Copy .ejs file after ts build. * Add dev and mocha scripts. * Update CONTRIBUTING.md * Directly import methods from Utils. * Use ts-node for dev runs.
1 parent 49d5a65 commit e7fde68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+803
-133
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ package.json
33
*.json
44
lib/utils/requireModule.js
55
lib/reporter/reporters/html
6+
dist

.github/CONTRIBUTING.md

+15-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@ Feature requests are welcome.
3333
## Submitting a pull request
3434
Thanks in advance for your contribution.
3535

36-
1. Follow the usual git workflow for submitting a pull request
37-
38-
* fork the project
39-
* create a new branch from master (e.g. `features/my-new-feature` or `issue/123-my-bugfix`)
40-
2. If you're fixing a bug also create an issue if one doesn't exist yet
41-
3. If it's a new feature/enhancemnt explain why do you think it's necessary
42-
4. If your change include drastic or low level changes please discuss them to make sure they will be accepted and what the impact will be
43-
5. If your change is based on exisiting functionality please consider refactoring first. Pull requests that duplicate code will not make it in very quick, if at all
44-
6. Do not include changes that are not related to the issue at hand
36+
1. Follow the usual git workflow for submitting a pull request:
37+
* fork the project
38+
* create a new branch from main (e.g. `features/my-new-feature` or `issue/123-my-bugfix`)
39+
* add your changes
40+
* try to run some Nightwatch tests locally to test the functionality after making your changes: `npm run dev -- examples/tests/ecosia.js --env chrome`
41+
* run Nightwatch unit tests locally: `npm test`
42+
or, run individual tests: `npm run mocha -- test/src/api/commands/client/testWaitUntil.js`
43+
* commit your changes and create a pull request
44+
45+
2. If you're fixing a bug, also create an issue if one doesn't exist yet.
46+
3. If it's a new feature/enhancement, explain why do you think it's necessary.
47+
4. If your change include drastic or low level changes please discuss them to make sure they will be accepted and what the impact will be.
48+
5. If your change is based on existing functionality, please consider refactoring first. Pull requests that duplicate code will not make it in very quick, if at all.
49+
6. Do not include changes that are not related to the issue at hand.
4550
6. Follow the same coding style with regards to spaces, semicolons, variable naming etc.
46-
7. Always add tests - after all this _is_ a testing framework
51+
7. Always add tests - after all this _is_ a testing framework.
4752

4853

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/nightwatch.json
99
/nightwatch.conf.js
1010
node_modules
11+
dist
1112
npm-debug.log
1213
lib-cov
1314
coverage.html

api/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const exportedCommands = [
1313
'protocol/quit.js'
1414
];
1515

16-
const basePath = '../lib/api';
16+
const basePath = '../dist/api';
1717
const Commands = {};
1818
const props = exportedCommands.reduce((prev, fileName) => {
1919
const commandName = fileName.substring(fileName.lastIndexOf('/')+1).replace('.js', '');

bin/nightwatch

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/env node
22

3+
const utilsPath = process.env.NIGHTWATCH_TS_NODE_DEV ?
4+
'../lib/utils' :
5+
'../dist/utils';
6+
37
const semver = require('semver');
4-
const {Logger} = require('../lib/utils');
8+
const {Logger} = require(utilsPath);
59
const requiredVersion = require('../package.json').engines.node;
610

711
function checkNodeVersion (wanted, id) {

bin/runner.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
22
* Module dependencies
33
*/
4-
const Nightwatch = require('../lib/index.js');
5-
const {Logger, shouldReplaceStack, alwaysDisplayError} = require('../lib/utils');
4+
const mainDir = process.env.NIGHTWATCH_TS_NODE_DEV ? 'lib' : 'dist';
5+
6+
const Nightwatch = require(`../${mainDir}/index.js`);
7+
const {Logger, shouldReplaceStack, alwaysDisplayError} = require(`../${mainDir}/utils`);
68

79
try {
810
Nightwatch.cli(function (argv) {

cucumber-js/_setup_cucumber_runner.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
const Nightwatch = require('../lib');
1+
const nightwatchMain = process.env.NIGHTWATCH_TS_NODE_DEV ?
2+
'../lib' :
3+
'../dist';
4+
5+
const Nightwatch = require(nightwatchMain);
26
const {Before, After, setDefaultTimeout} = require('@cucumber/cucumber');
37

48
setDefaultTimeout(-1);

examples/unittests/testUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const assert = require('assert');
2-
const Utils = require('../../lib/utils/');
2+
const Utils = require('../../dist/utils/');
33

44
module.exports = {
55
testFormatElapsedTime: function() {

examples/unittests/testUtilsWithChai.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const Utils = require('../../lib/utils/');
1+
const Utils = require('../../dist/utils/');
22
const expect = require('chai').expect;
33

44
module.exports = {

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = process.env.NIGHTWATCH_COV ?
22
require('./lib-cov/index') :
3-
require('./lib/index');
3+
require('./dist/index');

lib/api/_loaders/command.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const EventEmitter = require('events');
22
const BaseCommandLoader = require('./_command-loader.js');
3-
const Utils = require('../../utils');
4-
const {Logger} = require('../../../lib/utils');
3+
const {Logger, isES6AsyncFn, isFunction, isObject, makePromise} = require('../../utils');
54

65
class CommandLoader extends BaseCommandLoader {
76

@@ -12,7 +11,7 @@ class CommandLoader extends BaseCommandLoader {
1211
}
1312

1413
static isDeprecatedCommandStyle(CommandModule) {
15-
return Utils.isObject(CommandModule) && Utils.isFunction(CommandModule.command);
14+
return isObject(CommandModule) && isFunction(CommandModule.command);
1615
}
1716

1817
/**
@@ -24,7 +23,7 @@ class CommandLoader extends BaseCommandLoader {
2423
static createFromObject(CommandModule) {
2524
return class CommandClass extends EventEmitter {
2625
command(...args) {
27-
if (Utils.isES6AsyncFn(CommandModule.command)) {
26+
if (isES6AsyncFn(CommandModule.command)) {
2827
return CommandModule.command.apply(this.api, args);
2928
}
3029

@@ -43,7 +42,7 @@ class CommandLoader extends BaseCommandLoader {
4342
return function(...args) {
4443
let callback;
4544
let method;
46-
let isLastArgFunction = Utils.isFunction(args[args.length-1]);
45+
const isLastArgFunction = isFunction(args[args.length-1]);
4746

4847
if (isLastArgFunction) {
4948
callback = args.pop();
@@ -62,7 +61,7 @@ class CommandLoader extends BaseCommandLoader {
6261
method = target[name];
6362
}
6463

65-
return method(definition).then((result) => Utils.makePromise(callback, api, [result]));
64+
return method(definition).then((result) => makePromise(callback, api, [result]));
6665
};
6766
}
6867
});
@@ -89,7 +88,7 @@ class CommandLoader extends BaseCommandLoader {
8988
}
9089

9190
get isES6AsyncCommand() {
92-
return Utils.isES6AsyncFn(
91+
return isES6AsyncFn(
9392
CommandLoader.isDeprecatedCommandStyle(CommandModule) ? CommandModule.command: this.command
9493
);
9594
}
@@ -123,7 +122,7 @@ class CommandLoader extends BaseCommandLoader {
123122
}
124123

125124
complete(...args) {
126-
if (Utils.isFunction(super.complete)) {
125+
if (isFunction(super.complete)) {
127126
return super.complete(...args);
128127
}
129128

@@ -134,7 +133,7 @@ class CommandLoader extends BaseCommandLoader {
134133
const instance = new CommandInstance();
135134

136135
Object.keys(CommandLoader.interfaceMethods).forEach(method => {
137-
let type = CommandLoader.interfaceMethods[method];
136+
const type = CommandLoader.interfaceMethods[method];
138137
if (!BaseCommandLoader.isTypeImplemented(instance, method, type)) {
139138
throw new Error(`Command class must implement method .${method}()`);
140139
}
@@ -167,7 +166,7 @@ class CommandLoader extends BaseCommandLoader {
167166
}
168167

169168
if (instance.w3c_deprecated) {
170-
let extraMessage = instance.deprecationNotice ? `\n ${instance.deprecationNotice}` : '';
169+
const extraMessage = instance.deprecationNotice ? `\n ${instance.deprecationNotice}` : '';
171170
// eslint-disable-next-line no-console
172171
console.warn(`This command has been deprecated and is removed from the W3C Webdriver standard. It is only working with legacy Selenium JSONWire protocol.${extraMessage}`);
173172
}
@@ -198,15 +197,15 @@ class CommandLoader extends BaseCommandLoader {
198197
})
199198
.then(result => {
200199
let reportErrors = instance.client.settings.report_command_errors;
201-
let reportNetworkErrors = instance.client.settings.report_network_errors;
200+
const reportNetworkErrors = instance.client.settings.report_network_errors;
202201

203202
if (result && result.error && result.error.code && result.status === -1 && reportNetworkErrors) {
204203
// node.js errors, e.g. ECONNRESET
205204
reportErrors = true;
206205
}
207206

208207
if (result && result.status === -1 && instance.reportProtocolErrors(result) && reportErrors) {
209-
let err = new Error(`Error while running .${this.commandName}(): ${result.error}`);
208+
const err = new Error(`Error while running .${this.commandName}(): ${result.error}`);
210209

211210
if (result.stack) {
212211
err.stack = result.stack;

lib/index.d.ts

-1
This file was deleted.

lib/index.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -368,19 +368,6 @@ function throwIfBrowserNotAvailable() {
368368
}
369369
}
370370

371-
const globalBrowserDescriptor = {
372-
configurable: true,
373-
get() {
374-
throwIfBrowserNotAvailable();
375-
376-
return global.browser;
377-
}
378-
};
379-
380-
Object.defineProperty(Nightwatch, 'browser', globalBrowserDescriptor);
381-
382-
Object.defineProperty(Nightwatch, 'app', globalBrowserDescriptor);
383-
384371
Object.defineProperty(Nightwatch, 'Key', {
385372
configurable: true,
386373
get() {
@@ -401,7 +388,7 @@ const {
401388
chrome, firefox, assert, verify, expect, element
402389
} = namespacedApi;
403390

404-
// browser and app are overwritten above using `Object.defineProperty()`.
391+
// browser and app are overwritten below using `Object.defineProperty()`.
405392
module.exports.browser = browser;
406393
module.exports.app = app;
407394

@@ -449,3 +436,14 @@ module.exports.element = new Proxy(element, {
449436
return global.browser.element[name];
450437
}
451438
});
439+
440+
const globalBrowserDescriptor = {
441+
configurable: true,
442+
get() {
443+
throwIfBrowserNotAvailable();
444+
445+
return global.browser;
446+
}
447+
};
448+
Object.defineProperty(Nightwatch, 'browser', globalBrowserDescriptor);
449+
Object.defineProperty(Nightwatch, 'app', globalBrowserDescriptor);

0 commit comments

Comments
 (0)