How to use createMochaInstanceAlreadyRunningError method in Mocha

Best JavaScript code snippet using mocha

Run Mocha automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

errors.js

Source: errors.js Github

copy
1var format = require('util').format
2
3/**
4 * Factory functions to create throwable error objects
5 * @module Errors
6 */
7
8/**
9 * When Mocha throw exceptions (or otherwise errors), it attempts to assign a
10 * `code` property to the `Error` object, for easier handling.  These are the
11 * potential values of `code`.
12 */
13var constants = {
14  /**
15   * An unrecoverable error.
16   */
17  FATAL: 'ERR_MOCHA_FATAL',
18
19  /**
20   * The type of an argument to a function call is invalid
21   */
22  INVALID_ARG_TYPE: 'ERR_MOCHA_INVALID_ARG_TYPE',
23
24  /**
25   * The value of an argument to a function call is invalid
26   */
27  INVALID_ARG_VALUE: 'ERR_MOCHA_INVALID_ARG_VALUE',
28
29  /**
30   * Something was thrown, but it wasn't an `Error`
31   */
32  INVALID_EXCEPTION: 'ERR_MOCHA_INVALID_EXCEPTION',
33
34  /**
35   * An interface (e.g., `Mocha.interfaces`) is unknown or invalid
36   */
37  INVALID_INTERFACE: 'ERR_MOCHA_INVALID_INTERFACE',
38
39  /**
40   * A reporter (.e.g, `Mocha.reporters`) is unknown or invalid
41   */
42  INVALID_REPORTER: 'ERR_MOCHA_INVALID_REPORTER',
43
44  /**
45   * `done()` was called twice in a `Test` or `Hook` callback
46   */
47  MULTIPLE_DONE: 'ERR_MOCHA_MULTIPLE_DONE',
48
49  /**
50   * No files matched the pattern provided by the user
51   */
52  NO_FILES_MATCH_PATTERN: 'ERR_MOCHA_NO_FILES_MATCH_PATTERN',
53
54  /**
55   * Known, but unsupported behavior of some kind
56   */
57  UNSUPPORTED: 'ERR_MOCHA_UNSUPPORTED',
58
59  /**
60   * Invalid state transition occuring in `Mocha` instance
61   */
62  INSTANCE_ALREADY_RUNNING: 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING',
63
64  /**
65   * Invalid state transition occuring in `Mocha` instance
66   */
67  INSTANCE_ALREADY_DISPOSED: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED',
68}
69
70/**
71 * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
72 *
73 * @public
74 * @param {string} message - Error message to be displayed.
75 * @param {string} pattern - User-specified argument value.
76 * @returns {Error} instance detailing the error condition
77 */
78function createNoFilesMatchPatternError(message, pattern) {
79  var err = new Error(message)
80  err.code = constants.NO_FILES_MATCH_PATTERN
81  err.pattern = pattern
82  return err
83}
84
85/**
86 * Creates an error object to be thrown when the reporter specified in the options was not found.
87 *
88 * @public
89 * @param {string} message - Error message to be displayed.
90 * @param {string} reporter - User-specified reporter value.
91 * @returns {Error} instance detailing the error condition
92 */
93function createInvalidReporterError(message, reporter) {
94  var err = new TypeError(message)
95  err.code = constants.INVALID_REPORTER
96  err.reporter = reporter
97  return err
98}
99
100/**
101 * Creates an error object to be thrown when the interface specified in the options was not found.
102 *
103 * @public
104 * @param {string} message - Error message to be displayed.
105 * @param {string} ui - User-specified interface value.
106 * @returns {Error} instance detailing the error condition
107 */
108function createInvalidInterfaceError(message, ui) {
109  var err = new Error(message)
110  err.code = constants.INVALID_INTERFACE
111  err.interface = ui
112  return err
113}
114
115/**
116 * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
117 *
118 * @public
119 * @param {string} message - Error message to be displayed.
120 * @returns {Error} instance detailing the error condition
121 */
122function createUnsupportedError(message) {
123  var err = new Error(message)
124  err.code = constants.UNSUPPORTED
125  return err
126}
127
128/**
129 * Creates an error object to be thrown when an argument is missing.
130 *
131 * @public
132 * @param {string} message - Error message to be displayed.
133 * @param {string} argument - Argument name.
134 * @param {string} expected - Expected argument datatype.
135 * @returns {Error} instance detailing the error condition
136 */
137function createMissingArgumentError(message, argument, expected) {
138  return createInvalidArgumentTypeError(message, argument, expected)
139}
140
141/**
142 * Creates an error object to be thrown when an argument did not use the supported type
143 *
144 * @public
145 * @param {string} message - Error message to be displayed.
146 * @param {string} argument - Argument name.
147 * @param {string} expected - Expected argument datatype.
148 * @returns {Error} instance detailing the error condition
149 */
150function createInvalidArgumentTypeError(message, argument, expected) {
151  var err = new TypeError(message)
152  err.code = constants.INVALID_ARG_TYPE
153  err.argument = argument
154  err.expected = expected
155  err.actual = typeof argument
156  return err
157}
158
159/**
160 * Creates an error object to be thrown when an argument did not use the supported value
161 *
162 * @public
163 * @param {string} message - Error message to be displayed.
164 * @param {string} argument - Argument name.
165 * @param {string} value - Argument value.
166 * @param {string} [reason] - Why value is invalid.
167 * @returns {Error} instance detailing the error condition
168 */
169function createInvalidArgumentValueError(message, argument, value, reason) {
170  var err = new TypeError(message)
171  err.code = constants.INVALID_ARG_VALUE
172  err.argument = argument
173  err.value = value
174  err.reason = typeof reason !== 'undefined' ? reason : 'is invalid'
175  return err
176}
177
178/**
179 * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
180 *
181 * @public
182 * @param {string} message - Error message to be displayed.
183 * @returns {Error} instance detailing the error condition
184 */
185function createInvalidExceptionError(message, value) {
186  var err = new Error(message)
187  err.code = constants.INVALID_EXCEPTION
188  err.valueType = typeof value
189  err.value = value
190  return err
191}
192
193/**
194 * Creates an error object to be thrown when an unrecoverable error occurs.
195 *
196 * @public
197 * @param {string} message - Error message to be displayed.
198 * @returns {Error} instance detailing the error condition
199 */
200function createFatalError(message, value) {
201  var err = new Error(message)
202  err.code = constants.FATAL
203  err.valueType = typeof value
204  err.value = value
205  return err
206}
207
208/**
209 * Dynamically creates a plugin-type-specific error based on plugin type
210 * @param {string} message - Error message
211 * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed
212 * @param {string} [pluginId] - Name/path of plugin, if any
213 * @throws When `pluginType` is not known
214 * @public
215 * @returns {Error}
216 */
217function createInvalidPluginError(message, pluginType, pluginId) {
218  switch (pluginType) {
219    case 'reporter':
220      return createInvalidReporterError(message, pluginId)
221    case 'interface':
222      return createInvalidInterfaceError(message, pluginId)
223    default:
224      throw new Error('unknown pluginType "' + pluginType + '"')
225  }
226}
227
228/**
229 * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed.
230 * @param {string} message The error message to be displayed.
231 * @param {boolean} cleanReferencesAfterRun the value of `cleanReferencesAfterRun`
232 * @param {Mocha} instance the mocha instance that throw this error
233 */
234function createMochaInstanceAlreadyDisposedError(
235  message,
236  cleanReferencesAfterRun,
237  instance
238) {
239  var err = new Error(message)
240  err.code = constants.INSTANCE_ALREADY_DISPOSED
241  err.cleanReferencesAfterRun = cleanReferencesAfterRun
242  err.instance = instance
243  return err
244}
245
246/**
247 * Creates an error object to be thrown when a mocha object's `run` method is called while a test run is in progress.
248 * @param {string} message The error message to be displayed.
249 */
250function createMochaInstanceAlreadyRunningError(message, instance) {
251  var err = new Error(message)
252  err.code = constants.INSTANCE_ALREADY_RUNNING
253  err.instance = instance
254  return err
255}
256
257/*
258 * Creates an error object to be thrown when done() is called multiple times in a test
259 *
260 * @public
261 * @param {Runnable} runnable - Original runnable
262 * @param {Error} [originalErr] - Original error, if any
263 * @returns {Error} instance detailing the error condition
264 */
265function createMultipleDoneError(runnable, originalErr) {
266  var title
267  try {
268    title = format('<%s>', runnable.fullTitle())
269    if (runnable.parent.root) {
270      title += ' (of root suite)'
271    }
272  } catch (ignored) {
273    title = format('<%s> (of unknown suite)', runnable.title)
274  }
275  var message = format(
276    'done() called multiple times in %s %s',
277    runnable.type ? runnable.type : 'unknown runnable',
278    title
279  )
280  if (runnable.file) {
281    message += format(' of file %s', runnable.file)
282  }
283  if (originalErr) {
284    message += format('; in addition, done() received error: %s', originalErr)
285  }
286
287  var err = new Error(message)
288  err.code = constants.MULTIPLE_DONE
289  err.valueType = typeof originalErr
290  err.value = originalErr
291  return err
292}
293
294module.exports = {
295  createInvalidArgumentTypeError: createInvalidArgumentTypeError,
296  createInvalidArgumentValueError: createInvalidArgumentValueError,
297  createInvalidExceptionError: createInvalidExceptionError,
298  createInvalidInterfaceError: createInvalidInterfaceError,
299  createInvalidReporterError: createInvalidReporterError,
300  createMissingArgumentError: createMissingArgumentError,
301  createNoFilesMatchPatternError: createNoFilesMatchPatternError,
302  createUnsupportedError: createUnsupportedError,
303  createInvalidPluginError: createInvalidPluginError,
304  createMochaInstanceAlreadyDisposedError: createMochaInstanceAlreadyDisposedError,
305  createMochaInstanceAlreadyRunningError: createMochaInstanceAlreadyRunningError,
306  createFatalError: createFatalError,
307  createMultipleDoneError: createMultipleDoneError,
308  constants: constants,
309}
310
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Mocha on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)