codeceptjs 3.6.6 → 4.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/bin/codecept.js +81 -84
  2. package/lib/actor.js +13 -13
  3. package/lib/ai.js +13 -10
  4. package/lib/assert/empty.js +21 -20
  5. package/lib/assert/equal.js +39 -37
  6. package/lib/assert/error.js +14 -14
  7. package/lib/assert/include.js +47 -46
  8. package/lib/assert/throws.js +11 -13
  9. package/lib/assert/truth.js +22 -19
  10. package/lib/assert.js +2 -4
  11. package/lib/cli.js +49 -57
  12. package/lib/codecept.js +155 -142
  13. package/lib/colorUtils.js +3 -3
  14. package/lib/command/configMigrate.js +52 -58
  15. package/lib/command/definitions.js +89 -88
  16. package/lib/command/dryRun.js +68 -71
  17. package/lib/command/generate.js +188 -197
  18. package/lib/command/gherkin/init.js +16 -27
  19. package/lib/command/gherkin/snippets.js +20 -20
  20. package/lib/command/gherkin/steps.js +8 -8
  21. package/lib/command/info.js +38 -40
  22. package/lib/command/init.js +288 -290
  23. package/lib/command/interactive.js +32 -32
  24. package/lib/command/list.js +26 -26
  25. package/lib/command/run-multiple/chunk.js +5 -5
  26. package/lib/command/run-multiple/collection.js +3 -3
  27. package/lib/command/run-multiple/run.js +2 -6
  28. package/lib/command/run-multiple.js +93 -113
  29. package/lib/command/run-rerun.js +25 -20
  30. package/lib/command/run-workers.js +66 -64
  31. package/lib/command/run.js +29 -26
  32. package/lib/command/utils.js +65 -80
  33. package/lib/command/workers/runTests.js +10 -10
  34. package/lib/config.js +9 -10
  35. package/lib/container.js +48 -40
  36. package/lib/data/context.js +59 -60
  37. package/lib/data/dataScenarioConfig.js +47 -47
  38. package/lib/data/dataTableArgument.js +29 -29
  39. package/lib/data/table.js +20 -26
  40. package/lib/dirname.js +5 -0
  41. package/lib/event.js +167 -163
  42. package/lib/heal.js +17 -13
  43. package/lib/helper/AI.js +41 -130
  44. package/lib/helper/ApiDataFactory.js +69 -73
  45. package/lib/helper/Appium.js +381 -412
  46. package/lib/helper/Expect.js +425 -0
  47. package/lib/helper/ExpectHelper.js +48 -40
  48. package/lib/helper/FileSystem.js +79 -80
  49. package/lib/helper/GraphQL.js +43 -44
  50. package/lib/helper/GraphQLDataFactory.js +50 -50
  51. package/lib/helper/JSONResponse.js +62 -65
  52. package/lib/helper/Mochawesome.js +28 -28
  53. package/lib/helper/MockServer.js +14 -12
  54. package/lib/helper/Nightmare.js +566 -662
  55. package/lib/helper/Playwright.js +1216 -1361
  56. package/lib/helper/Protractor.js +627 -663
  57. package/lib/helper/Puppeteer.js +1128 -1231
  58. package/lib/helper/REST.js +68 -159
  59. package/lib/helper/SoftExpectHelper.js +2 -2
  60. package/lib/helper/TestCafe.js +484 -490
  61. package/lib/helper/WebDriver.js +1156 -1297
  62. package/lib/helper/clientscripts/PollyWebDriverExt.js +1 -1
  63. package/lib/helper/errors/ConnectionRefused.js +1 -1
  64. package/lib/helper/errors/ElementAssertion.js +2 -2
  65. package/lib/helper/errors/ElementNotFound.js +2 -2
  66. package/lib/helper/errors/RemoteBrowserConnectionRefused.js +1 -1
  67. package/lib/helper/extras/Console.js +1 -1
  68. package/lib/helper/extras/PlaywrightPropEngine.js +2 -2
  69. package/lib/helper/extras/PlaywrightReactVueLocator.js +1 -1
  70. package/lib/helper/extras/PlaywrightRestartOpts.js +18 -21
  71. package/lib/helper/extras/Popup.js +1 -1
  72. package/lib/helper/extras/React.js +3 -3
  73. package/lib/helper/network/actions.js +7 -14
  74. package/lib/helper/network/utils.js +2 -3
  75. package/lib/helper/scripts/blurElement.js +1 -1
  76. package/lib/helper/scripts/focusElement.js +1 -1
  77. package/lib/helper/scripts/highlightElement.js +1 -1
  78. package/lib/helper/scripts/isElementClickable.js +1 -1
  79. package/lib/helper/testcafe/testControllerHolder.js +1 -1
  80. package/lib/helper/testcafe/testcafe-utils.js +7 -6
  81. package/lib/helper.js +3 -1
  82. package/lib/history.js +5 -6
  83. package/lib/hooks.js +6 -6
  84. package/lib/html.js +7 -7
  85. package/lib/index.js +41 -25
  86. package/lib/interfaces/bdd.js +64 -47
  87. package/lib/interfaces/featureConfig.js +19 -19
  88. package/lib/interfaces/gherkin.js +118 -124
  89. package/lib/interfaces/scenarioConfig.js +29 -29
  90. package/lib/listener/artifacts.js +9 -9
  91. package/lib/listener/config.js +24 -24
  92. package/lib/listener/exit.js +12 -12
  93. package/lib/listener/helpers.js +42 -42
  94. package/lib/listener/mocha.js +11 -11
  95. package/lib/listener/retry.js +30 -32
  96. package/lib/listener/steps.js +53 -50
  97. package/lib/listener/timeout.js +54 -54
  98. package/lib/locator.js +10 -6
  99. package/lib/mochaFactory.js +15 -18
  100. package/lib/output.js +10 -6
  101. package/lib/parser.js +12 -15
  102. package/lib/pause.js +33 -40
  103. package/lib/plugin/allure.js +15 -15
  104. package/lib/plugin/autoDelay.js +37 -29
  105. package/lib/plugin/autoLogin.js +65 -70
  106. package/lib/plugin/commentStep.js +18 -18
  107. package/lib/plugin/coverage.js +67 -115
  108. package/lib/plugin/customLocator.js +20 -21
  109. package/lib/plugin/debugErrors.js +24 -24
  110. package/lib/plugin/eachElement.js +38 -38
  111. package/lib/plugin/fakerTransform.js +6 -6
  112. package/lib/plugin/heal.js +108 -67
  113. package/lib/plugin/pauseOnFail.js +11 -11
  114. package/lib/plugin/retryFailedStep.js +39 -32
  115. package/lib/plugin/retryTo.js +40 -46
  116. package/lib/plugin/screenshotOnFail.js +87 -109
  117. package/lib/plugin/selenoid.js +118 -131
  118. package/lib/plugin/standardActingHelpers.js +8 -2
  119. package/lib/plugin/stepByStepReport.js +91 -110
  120. package/lib/plugin/stepTimeout.js +23 -24
  121. package/lib/plugin/subtitles.js +35 -34
  122. package/lib/plugin/tryTo.js +30 -40
  123. package/lib/plugin/wdio.js +75 -78
  124. package/lib/recorder.js +17 -14
  125. package/lib/rerun.js +10 -11
  126. package/lib/scenario.js +23 -25
  127. package/lib/secret.js +2 -4
  128. package/lib/session.js +10 -10
  129. package/lib/step.js +9 -12
  130. package/lib/store.js +3 -2
  131. package/lib/transform.js +1 -1
  132. package/lib/translation.js +8 -7
  133. package/lib/ui.js +14 -12
  134. package/lib/utils.js +72 -70
  135. package/lib/within.js +10 -10
  136. package/lib/workerStorage.js +25 -27
  137. package/lib/workers.js +32 -29
  138. package/package.json +53 -51
  139. package/translations/de-DE.js +1 -1
  140. package/translations/fr-FR.js +1 -1
  141. package/translations/index.js +13 -9
  142. package/translations/it-IT.js +1 -1
  143. package/translations/ja-JP.js +1 -1
  144. package/translations/pl-PL.js +1 -1
  145. package/translations/pt-BR.js +1 -1
  146. package/translations/ru-RU.js +1 -1
  147. package/translations/zh-CN.js +1 -1
  148. package/translations/zh-TW.js +1 -1
  149. package/typings/index.d.ts +65 -415
  150. package/typings/promiseBasedTypes.d.ts +32 -0
  151. package/typings/types.d.ts +32 -0
package/bin/codecept.js CHANGED
@@ -1,8 +1,26 @@
1
1
  #!/usr/bin/env node
2
- const program = require('commander')
3
- const Codecept = require('../lib/codecept')
4
- const { print, error } = require('../lib/output')
5
- const { printError } = require('../lib/command/utils')
2
+ import { Command } from 'commander';
3
+ import { version } from '../lib/codecept.js';
4
+ import * as outputLib from '../lib/output.js';
5
+ import { printError } from '../lib/command/utils.js';
6
+ import * as init from '../lib/command/init.js';
7
+ import * as configMigrate from '../lib/command/configMigrate.js';
8
+ import * as interactive from '../lib/command/interactive.js';
9
+ import * as definitions from '../lib/command/definitions.js';
10
+ import * as list from '../lib/command/list.js';
11
+ import * as gherkinInit from '../lib/command/gherkin/init.js';
12
+ import * as gherkinSteps from '../lib/command/gherkin/steps.js';
13
+ import * as gherkinSnippets from '../lib/command/gherkin/snippets.js';
14
+ import * as generate from '../lib/command/generate.js';
15
+ import * as run from '../lib/command/run.js';
16
+ import * as runWorkers from '../lib/command/run-workers.js';
17
+ import * as runMultiple from '../lib/command/run-multiple.js';
18
+ import { runRerun } from '../lib/command/run-rerun.js';
19
+ import * as dryRun from '../lib/command/dryRun.js';
20
+ import * as info from '../lib/command/info.js';
21
+ import { heal } from "../lib/command/generate.js";
22
+
23
+ const program = new Command();
6
24
 
7
25
  const commandFlags = {
8
26
  ai: {
@@ -29,120 +47,104 @@ const commandFlags = {
29
47
  flag: '--steps',
30
48
  description: 'show step-by-step execution',
31
49
  },
32
- }
33
-
34
- const errorHandler =
35
- (fn) =>
36
- async (...args) => {
37
- try {
38
- await fn(...args)
39
- } catch (e) {
40
- printError(e)
41
- process.exitCode = 1
42
- }
50
+ };
51
+
52
+ const errorHandler = (fn) => async (...args) => {
53
+ try {
54
+ await fn.default(...args);
55
+ } catch (e) {
56
+ printError(e);
57
+ process.exitCode = 1;
43
58
  }
59
+ };
44
60
 
45
61
  if (process.versions.node && process.versions.node.split('.') && process.versions.node.split('.')[0] < 12) {
46
- error('NodeJS >= 12 is required to run.')
47
- print()
48
- print('Please upgrade your NodeJS engine')
49
- print(`Current NodeJS version: ${process.version}`)
50
- process.exit(1)
62
+ outputLib.output.output.error('NodeJS >= 12 is required to run.');
63
+ outputLib.print();
64
+ outputLib.print('Please upgrade your NodeJS engine');
65
+ outputLib.print(`Current NodeJS version: ${process.version}`);
66
+ process.exit(1);
51
67
  }
52
68
 
53
- program.usage('<command> [options]')
54
- program.version(Codecept.version())
69
+ program.usage('<command> [options]');
70
+ program.version(version());
55
71
 
56
- program
57
- .command('init [path]')
72
+ program.command('init [path]')
58
73
  .description('Creates dummy config in current dir or [path]')
59
- .action(errorHandler(require('../lib/command/init')))
74
+ .action(errorHandler(init));
60
75
 
61
- program
62
- .command('migrate [path]')
76
+ program.command('migrate [path]')
63
77
  .description('Migrate json config to js config in current dir or [path]')
64
- .action(errorHandler(require('../lib/command/configMigrate')))
78
+ .action(errorHandler(configMigrate));
65
79
 
66
- program
67
- .command('shell [path]')
80
+ program.command('shell [path]')
68
81
  .alias('sh')
69
82
  .description('Interactive shell')
70
83
  .option(commandFlags.verbose.flag, commandFlags.verbose.description)
71
84
  .option(commandFlags.profile.flag, commandFlags.profile.description)
72
85
  .option(commandFlags.ai.flag, commandFlags.ai.description)
73
86
  .option(commandFlags.config.flag, commandFlags.config.description)
74
- .action(errorHandler(require('../lib/command/interactive')))
87
+ .action(errorHandler(interactive));
75
88
 
76
- program
77
- .command('list [path]')
89
+ program.command('list [path]')
78
90
  .alias('l')
79
91
  .description('List all actions for I.')
80
- .action(errorHandler(require('../lib/command/list')))
92
+ .action(errorHandler(list));
81
93
 
82
- program
83
- .command('def [path]')
94
+ program.command('def [path]')
84
95
  .description('Generates TypeScript definitions for all I actions.')
85
96
  .option(commandFlags.config.flag, commandFlags.config.description)
86
97
  .option('-o, --output [folder]', 'target folder to paste definitions')
87
- .action(errorHandler(require('../lib/command/definitions')))
98
+ .action(errorHandler(definitions));
88
99
 
89
- program
90
- .command('gherkin:init [path]')
100
+ program.command('gherkin:init [path]')
91
101
  .alias('bdd:init')
92
102
  .description('Prepare CodeceptJS to run feature files.')
93
103
  .option(commandFlags.config.flag, commandFlags.config.description)
94
- .action(errorHandler(require('../lib/command/gherkin/init')))
104
+ .action(errorHandler(gherkinInit));
95
105
 
96
- program
97
- .command('gherkin:steps [path]')
106
+ program.command('gherkin:steps [path]')
98
107
  .alias('bdd:steps')
99
108
  .description('Prints all defined gherkin steps.')
100
109
  .option(commandFlags.config.flag, commandFlags.config.description)
101
- .action(errorHandler(require('../lib/command/gherkin/steps')))
110
+ .action(errorHandler(gherkinSteps));
102
111
 
103
- program
104
- .command('gherkin:snippets [path]')
112
+ program.command('gherkin:snippets [path]')
105
113
  .alias('bdd:snippets')
106
114
  .description('Generate step definitions from steps.')
107
115
  .option('--dry-run', "don't save snippets to file")
108
116
  .option(commandFlags.config.flag, commandFlags.config.description)
109
117
  .option('--feature [file]', 'feature files(s) to scan')
110
118
  .option('--path [file]', 'file in which to place the new snippets')
111
- .action(errorHandler(require('../lib/command/gherkin/snippets')))
119
+ .action(errorHandler(gherkinSnippets));
112
120
 
113
- program
114
- .command('generate:test [path]')
121
+ program.command('generate:test [path]')
115
122
  .alias('gt')
116
123
  .description('Generates an empty test')
117
- .action(errorHandler(require('../lib/command/generate').test))
124
+ .action(errorHandler(generate.test));
118
125
 
119
- program
120
- .command('generate:pageobject [path]')
126
+ program.command('generate:pageobject [path]')
121
127
  .alias('gpo')
122
128
  .description('Generates an empty page object')
123
- .action(errorHandler(require('../lib/command/generate').pageObject))
129
+ .action(errorHandler(generate.pageObject));
124
130
 
125
- program
126
- .command('generate:object [path]')
131
+ program.command('generate:object [path]')
127
132
  .alias('go')
128
133
  .option('--type, -t [kind]', 'type of object to be created')
129
134
  .description('Generates an empty support object (page/step/fragment)')
130
- .action(errorHandler(require('../lib/command/generate').pageObject))
135
+ .action(errorHandler(generate.pageObject));
131
136
 
132
- program
133
- .command('generate:helper [path]')
137
+ program.command('generate:helper [path]')
134
138
  .alias('gh')
135
139
  .description('Generates a new helper')
136
- .action(errorHandler(require('../lib/command/generate').helper))
140
+ .action(errorHandler(generate.helper));
137
141
 
138
- program
139
- .command('generate:heal [path]')
142
+ program.command('generate:heal [path]')
140
143
  .alias('gr')
141
144
  .description('Generates basic heal recipes')
142
- .action(errorHandler(require('../lib/command/generate').heal))
145
+ .action(errorHandler(heal));
143
146
 
144
- program
145
- .command('run [test]')
147
+ program.command('run [test]')
146
148
  .description('Executes tests')
147
149
 
148
150
  // codecept-only options
@@ -178,10 +180,9 @@ program
178
180
  .option('--recursive', 'include sub directories')
179
181
  .option('--trace', 'trace function calls')
180
182
  .option('--child <string>', 'option for child processes')
181
- .action(errorHandler(require('../lib/command/run')))
183
+ .action(errorHandler(run));
182
184
 
183
- program
184
- .command('run-workers <workers> [selectedRuns...]')
185
+ program.command('run-workers <workers> [selectedRuns...]')
185
186
  .description('Executes tests in workers')
186
187
  .option(commandFlags.config.flag, commandFlags.config.description)
187
188
  .option('-g, --grep <pattern>', 'only run tests matching <pattern>')
@@ -197,10 +198,9 @@ program
197
198
  .option('-p, --plugins <k=v,k2=v2,...>', 'enable plugins, comma-separated')
198
199
  .option('-O, --reporter-options <k=v,k2=v2,...>', 'reporter-specific options')
199
200
  .option('-R, --reporter <name>', 'specify the reporter to use')
200
- .action(errorHandler(require('../lib/command/run-workers')))
201
+ .action(errorHandler(runWorkers));
201
202
 
202
- program
203
- .command('run-multiple [suites...]')
203
+ program.command('run-multiple [suites...]')
204
204
  .description('Executes tests multiple')
205
205
  .option(commandFlags.config.flag, commandFlags.config.description)
206
206
  .option(commandFlags.profile.flag, commandFlags.profile.description)
@@ -223,16 +223,14 @@ program
223
223
  // mocha options
224
224
  .option('--colors', 'force enabling of colors')
225
225
 
226
- .action(errorHandler(require('../lib/command/run-multiple')))
226
+ .action(errorHandler(runMultiple));
227
227
 
228
- program
229
- .command('info [path]')
228
+ program.command('info [path]')
230
229
  .description('Print debugging information concerning the local environment')
231
230
  .option('-c, --config', 'your config file path')
232
- .action(errorHandler(require('../lib/command/info')))
231
+ .action(errorHandler(info));
233
232
 
234
- program
235
- .command('dry-run [test]')
233
+ program.command('dry-run [test]')
236
234
  .description('Prints step-by-step scenario for a test without actually running it')
237
235
  .option('-p, --plugins <k=v,k2=v2,...>', 'enable plugins, comma-separated')
238
236
  .option('--bootstrap', 'enable bootstrap & teardown scripts for dry-run')
@@ -246,10 +244,9 @@ program
246
244
  .option(commandFlags.steps.flag, commandFlags.steps.description)
247
245
  .option(commandFlags.verbose.flag, commandFlags.verbose.description)
248
246
  .option(commandFlags.debug.flag, commandFlags.debug.description)
249
- .action(errorHandler(require('../lib/command/dryRun')))
247
+ .action(errorHandler(dryRun));
250
248
 
251
- program
252
- .command('run-rerun [test]')
249
+ program.command('run-rerun [test]')
253
250
  .description('Executes tests in more than one test suite run')
254
251
 
255
252
  // codecept-only options
@@ -284,15 +281,15 @@ program
284
281
  .option('--trace', 'trace function calls')
285
282
  .option('--child <string>', 'option for child processes')
286
283
 
287
- .action(require('../lib/command/run-rerun'))
284
+ .action(runRerun);
288
285
 
289
286
  program.on('command:*', (cmd) => {
290
- console.log(`\nUnknown command ${cmd}\n`)
291
- program.outputHelp()
292
- })
287
+ console.log(`\nUnknown command ${cmd}\n`);
288
+ program.outputHelp();
289
+ });
293
290
 
294
291
  if (process.argv.length <= 2) {
295
- program.outputHelp()
292
+ program.outputHelp();
296
293
  } else {
297
- program.parse(process.argv)
294
+ program.parse(process.argv);
298
295
  }
package/lib/actor.js CHANGED
@@ -1,17 +1,16 @@
1
- const Step = require('./step');
2
- const { MetaStep } = require('./step');
3
- const container = require('./container');
4
- const { methodsOfObject } = require('./utils');
5
- const recorder = require('./recorder');
6
- const event = require('./event');
7
- const store = require('./store');
8
- const output = require('./output');
1
+ import { Step, MetaStep } from './step.js';
2
+ import container from './container.js';
3
+ import { methodsOfObject } from './utils.js';
4
+ import recorder from './recorder.js';
5
+ import * as event from './event.js';
6
+ import { store } from './store.js';
7
+ import * as output from './output.js';
9
8
 
10
9
  /**
11
10
  * @interface
12
11
  * @alias ActorStatic
13
12
  */
14
- class Actor {
13
+ export class Actor {
15
14
  /**
16
15
  * Print the comment on log. Also, adding a step in the `Test.steps` object
17
16
  * @param {string} msg
@@ -41,7 +40,7 @@ class Actor {
41
40
  if (!store.timeouts) return this;
42
41
 
43
42
  event.dispatcher.prependOnceListener(event.step.before, (step) => {
44
- output.log(`Timeout to ${step}: ${timeout}s`);
43
+ output.output.log(`Timeout to ${step}: ${timeout}s`);
45
44
  step.setTimeout(timeout * 1000, Step.TIMEOUT_ORDER.codeLimitTime);
46
45
  });
47
46
 
@@ -69,7 +68,8 @@ class Actor {
69
68
  * Wraps helper methods into promises.
70
69
  * @ignore
71
70
  */
72
- module.exports = function (obj = {}) {
71
+ // eslint-disable-next-line import/prefer-default-export
72
+ export function actor(obj = {}) {
73
73
  if (!store.actor) {
74
74
  store.actor = new Actor();
75
75
  }
@@ -117,9 +117,9 @@ module.exports = function (obj = {}) {
117
117
  });
118
118
 
119
119
  return actor;
120
- };
120
+ }
121
121
 
122
- function recordStep(step, args) {
122
+ export function recordStep(step, args) {
123
123
  step.status = 'queued';
124
124
  step.setArguments(args);
125
125
 
package/lib/ai.js CHANGED
@@ -1,7 +1,9 @@
1
- const debug = require('debug')('codeceptjs:ai');
2
- const output = require('./output');
3
- const event = require('./event');
4
- const { removeNonInteractiveElements, minifyHtml, splitByChunks } = require('./html');
1
+ import debug from 'debug';
2
+ import * as event from './event.js';
3
+ import * as output from './output.js';
4
+ import { removeNonInteractiveElements, minifyHtml, splitByChunks } from './html.js';
5
+
6
+ debug('codeceptjs:ai');
5
7
 
6
8
  const defaultHtmlConfig = {
7
9
  maxLength: 50000,
@@ -122,8 +124,8 @@ class AiAssistant {
122
124
  if (this.config.request) return;
123
125
 
124
126
  const noRequestErrorMessage = `
125
- No request function is set for AI assistant.
126
- Please implement your own request function and set it in the config.
127
+ No request function is set for AI assistant.
128
+ Please implement your own request function and set it in the config.
127
129
 
128
130
  [!] AI request was decoupled from CodeceptJS. To connect to OpenAI or other AI service, please implement your own request function and set it in the config.
129
131
 
@@ -187,10 +189,11 @@ class AiAssistant {
187
189
  } catch (err) {
188
190
  debug(err.response);
189
191
  output.print('');
190
- output.error(`AI service error: ${err.message}`);
191
- if (err?.response?.data?.error?.code) output.error(err?.response?.data?.error?.code);
192
- if (err?.response?.data?.error?.message) output.error(err?.response?.data?.error?.message);
192
+ output.output.error(`AI service error: ${err.message}`);
193
+ if (err?.response?.data?.error?.code) output.output.error(err?.response?.data?.error?.code);
194
+ if (err?.response?.data?.error?.message) output.output.error(err?.response?.data?.error?.message);
193
195
  this.stopWhenReachingTokensLimit();
196
+
194
197
  return '';
195
198
  }
196
199
  }
@@ -295,4 +298,4 @@ function parseCodeBlocks(response) {
295
298
  return modifiedSnippets.filter(snippet => !!snippet);
296
299
  }
297
300
 
298
- module.exports = new AiAssistant();
301
+ export default new AiAssistant();
@@ -1,43 +1,44 @@
1
- const Assertion = require('../assert')
2
- const AssertionFailedError = require('./error')
3
- const { template } = require('../utils')
4
- const output = require('../output')
1
+ import Assertion from '../assert.js';
2
+ import AssertionFailedError from './error.js';
3
+ import { template } from '../utils.js';
4
+ import * as output from '../output.js';
5
5
 
6
6
  class EmptinessAssertion extends Assertion {
7
7
  constructor(params) {
8
8
  super((value) => {
9
9
  if (Array.isArray(value)) {
10
- return value.length === 0
10
+ return value.length === 0;
11
11
  }
12
- return !value
13
- }, params)
14
- this.params.type = 'to be empty'
12
+ return !value;
13
+ }, params);
14
+ this.params.type = 'to be empty';
15
15
  }
16
16
 
17
17
  getException() {
18
18
  if (Array.isArray(this.params.value)) {
19
- this.params.value = `[${this.params.value.join(', ')}]`
19
+ this.params.value = `[${this.params.value.join(', ')}]`;
20
20
  }
21
21
 
22
- const err = new AssertionFailedError(this.params, "{{customMessage}}expected {{subject}} '{{value}}' {{type}}")
22
+ const err = new AssertionFailedError(this.params, "{{customMessage}}expected {{subject}} '{{value}}' {{type}}");
23
23
 
24
24
  err.cliMessage = () => {
25
25
  const msg = err.template
26
26
  .replace('{{value}}', output.colors.bold('{{value}}'))
27
- .replace('{{subject}}', output.colors.bold('{{subject}}'))
28
- return template(msg, this.params)
29
- }
30
- return err
27
+ .replace('{{subject}}', output.colors.bold('{{subject}}'));
28
+ return template(msg, this.params);
29
+ };
30
+ return err;
31
31
  }
32
32
 
33
33
  addAssertParams() {
34
- this.params.value = this.params.actual = arguments[0]
35
- this.params.expected = []
36
- this.params.customMessage = arguments[1] ? `${arguments[1]}\n\n` : ''
34
+ this.params.value = this.params.actual = arguments[0];
35
+ this.params.expected = [];
36
+ this.params.customMessage = arguments[1] ? `${arguments[1]}\n\n` : '';
37
37
  }
38
38
  }
39
39
 
40
- module.exports = {
41
- Assertion: EmptinessAssertion,
42
- empty: (subject) => new EmptinessAssertion({ subject }),
40
+ export { EmptinessAssertion as Assertion };
41
+
42
+ export function empty(subject) {
43
+ return new EmptinessAssertion({ subject });
43
44
  }
@@ -1,57 +1,59 @@
1
- const Assertion = require('../assert')
2
- const AssertionFailedError = require('./error')
3
- const { template } = require('../utils')
4
- const output = require('../output')
1
+ import Assertion from '../assert.js';
2
+ import AssertionFailedError from './error.js';
3
+ import { template } from '../utils.js';
4
+ import * as output from '../output.js';
5
5
 
6
6
  class EqualityAssertion extends Assertion {
7
7
  constructor(params) {
8
8
  const comparator = function (a, b) {
9
9
  if (b.length === 0) {
10
- b = ''
10
+ b = '';
11
11
  }
12
- return a === b
13
- }
14
- super(comparator, params)
15
- this.params.type = 'to equal'
12
+ return a === b;
13
+ };
14
+ super(comparator, params);
15
+ this.params.type = 'to equal';
16
16
  }
17
17
 
18
18
  getException() {
19
- const params = this.params
20
- params.jar = template(params.jar, params)
21
- const err = new AssertionFailedError(
22
- params,
23
- '{{customMessage}}expected {{jar}} "{{expected}}" {{type}} "{{actual}}"',
24
- )
25
- err.showDiff = false
19
+ const params = this.params;
20
+ params.jar = template(params.jar, params);
21
+ const err = new AssertionFailedError(params, '{{customMessage}}expected {{jar}} "{{expected}}" {{type}} "{{actual}}"');
22
+ err.showDiff = false;
26
23
  if (typeof err.cliMessage === 'function') {
27
- err.message = err.cliMessage()
24
+ err.message = err.cliMessage();
28
25
  }
29
26
  err.cliMessage = () => {
30
- const msg = err.template.replace('{{jar}}', output.colors.bold('{{jar}}'))
31
- return template(msg, this.params)
32
- }
33
- return err
27
+ const msg = err.template
28
+ .replace('{{jar}}', output.colors.bold('{{jar}}'));
29
+ return template(msg, this.params);
30
+ };
31
+ return err;
34
32
  }
35
33
 
36
34
  addAssertParams() {
37
- this.params.expected = arguments[0]
38
- this.params.actual = arguments[1]
39
- this.params.customMessage = arguments[2] ? `${arguments[2]}\n\n` : ''
35
+ this.params.expected = arguments[0];
36
+ this.params.actual = arguments[1];
37
+ this.params.customMessage = arguments[2] ? `${arguments[2]}\n\n` : '';
40
38
  }
41
39
  }
42
40
 
43
- module.exports = {
44
- Assertion: EqualityAssertion,
45
- equals: (jar) => new EqualityAssertion({ jar }),
46
- urlEquals: (baseUrl) => {
47
- const assert = new EqualityAssertion({ jar: 'url of current page' })
48
- assert.comparator = function (expected, actual) {
49
- if (expected.indexOf('http') !== 0) {
50
- actual = actual.slice(actual.indexOf(baseUrl) + baseUrl.length)
51
- }
52
- return actual === expected
41
+ export function fileEquals(file) {
42
+ return new EqualityAssertion({ file, jar: 'contents of {{file}}' });
43
+ }
44
+
45
+ export { EqualityAssertion as Assertion };
46
+ export function equals(jar) {
47
+ return new EqualityAssertion({ jar });
48
+ }
49
+
50
+ export function urlEquals(baseUrl) {
51
+ const assert = new EqualityAssertion({ jar: 'url of current page' });
52
+ assert.comparator = function (expected, actual) {
53
+ if (expected.indexOf('http') !== 0) {
54
+ actual = actual.slice(actual.indexOf(baseUrl) + baseUrl.length);
53
55
  }
54
- return assert
55
- },
56
- fileEquals: (file) => new EqualityAssertion({ file, jar: 'contents of {{file}}' }),
56
+ return actual === expected;
57
+ };
58
+ return assert;
57
59
  }
@@ -1,4 +1,4 @@
1
- const subs = require('../utils').template
1
+ import { template as subs } from '../utils.js';
2
2
 
3
3
  /**
4
4
  * Assertion errors, can provide a detailed error messages.
@@ -6,27 +6,27 @@ const subs = require('../utils').template
6
6
  * inspect() and cliMessage() added to display errors with params.
7
7
  */
8
8
  function AssertionFailedError(params, template) {
9
- this.params = params
10
- this.template = template
9
+ this.params = params;
10
+ this.template = template;
11
11
  // this.message = "AssertionFailedError";
12
12
  // this.showDiff = true;
13
13
 
14
14
  // @todo cut assert things nicer
15
- this.showDiff = true
15
+ this.showDiff = true;
16
16
 
17
- this.actual = this.params.actual
18
- this.expected = this.params.expected
17
+ this.actual = this.params.actual;
18
+ this.expected = this.params.expected;
19
19
 
20
20
  this.inspect = () => {
21
- const params = this.params || {}
22
- const msg = params.customMessage || ''
23
- return msg + subs(this.template, params)
24
- }
21
+ const params = this.params || {};
22
+ const msg = params.customMessage || '';
23
+ return msg + subs(this.template, params);
24
+ };
25
25
 
26
- this.cliMessage = () => this.inspect()
26
+ this.cliMessage = () => this.inspect();
27
27
  }
28
28
 
29
- AssertionFailedError.prototype = Object.create(Error.prototype)
30
- AssertionFailedError.constructor = AssertionFailedError
29
+ AssertionFailedError.prototype = Object.create(Error.prototype);
30
+ AssertionFailedError.constructor = AssertionFailedError;
31
31
 
32
- module.exports = AssertionFailedError
32
+ export default AssertionFailedError;