How to use runMochaAsync method in Mocha

Best JavaScript code snippet using mocha

parallel.spec.js

Source:parallel.spec.js Github

copy

Full Screen

...6var invokeMochaAsync = helpers.invokeMochaAsync;7var getSummary = helpers.getSummary;8var utils = require('../../../lib/utils');9function compareReporters(reporter) {10 return runMochaAsync(path.join('options', 'parallel', 'test-a.fixture.js'), [11 '--reporter',12 reporter,13 '--no-parallel'14 ]).then(function(expected) {15 expected.output = expected.output.replace(/\d+m?s/g, '100ms');16 return runMochaAsync(17 path.join('options', 'parallel', 'test-a.fixture.js'),18 ['--reporter', reporter, '--parallel']19 ).then(function(actual) {20 actual.output = actual.output.replace(/\d+m?s/g, '100ms');21 return [actual, expected];22 });23 });24}25function runGenericReporterTest(reporter) {26 return compareReporters.call(this, reporter).then(function(result) {27 var expected = result.shift();28 var actual = result.shift();29 return expect(actual, 'to satisfy', {30 passing: expected.passing,31 failing: expected.failing,32 pending: expected.pending,33 code: expected.code,34 output: expected.output35 });36 });37}38describe('--parallel', function() {39 describe('when a test has a syntax error', function() {40 describe('when there is only a single test file', function() {41 it('should fail gracefully', function() {42 return expect(43 runMochaAsync('options/parallel/syntax-err', ['--parallel']),44 'when fulfilled',45 'to have failed with output',46 /SyntaxError/47 );48 });49 });50 describe('when there are multiple test files', function() {51 it('should fail gracefully', function() {52 return expect(53 invokeMochaAsync(54 [55 require.resolve(56 '../fixtures/options/parallel/syntax-err.fixture.js'57 ),58 '--parallel'59 ],60 'pipe'61 )[1],62 'when fulfilled',63 'to have failed'64 );65 });66 });67 });68 describe('when used with CJS tests', function() {69 it('should have the same result as with --no-parallel', function() {70 return runMochaAsync(71 path.join('options', 'parallel', 'test-*.fixture.js'),72 ['--no-parallel']73 ).then(function(expected) {74 return expect(75 runMochaAsync(path.join('options', 'parallel', 'test-*.fixture.js'), [76 '--parallel'77 ]),78 'to be fulfilled with value satisfying',79 {80 passing: expected.passing,81 failing: expected.failing,82 pending: expected.pending,83 code: expected.code84 }85 );86 });87 });88 });89 describe('when used with ESM tests', function() {90 var esmArgs =91 Number(process.versions.node.split('.')[0]) >= 1392 ? []93 : ['--experimental-modules'];94 before(function() {95 if (!utils.supportsEsModules()) this.skip();96 });97 it('should have the same result as with --no-parallel', function() {98 var glob = path.join(__dirname, '..', 'fixtures', 'esm', '*.fixture.mjs');99 return invokeMochaAsync(esmArgs.concat('--no-parallel', glob))[1].then(100 function(expected) {101 expected = getSummary(expected);102 return invokeMochaAsync(esmArgs.concat('--parallel', glob))[1].then(103 function(actual) {104 actual = getSummary(actual);105 expect(actual, 'to satisfy', {106 pending: expected.pending,107 passing: expected.passing,108 failing: expected.failing109 });110 }111 );112 }113 );114 });115 });116 describe('when used with --retries', function() {117 it('should retry tests appropriately', function() {118 return expect(119 runMochaAsync(120 path.join('options', 'parallel', 'retries-*.fixture.js'),121 ['--parallel']122 ),123 'when fulfilled',124 'to have failed'125 )126 .and('when fulfilled', 'to have passed test count', 1)127 .and('when fulfilled', 'to have pending test count', 0)128 .and('when fulfilled', 'to have failed test count', 1)129 .and('when fulfilled', 'to contain output', /count: 3/);130 });131 });132 describe('when used with --allow-uncaught', function() {133 it('should bubble up an exception', function() {134 return expect(135 invokeMochaAsync(136 [137 require.resolve('../fixtures/options/parallel/uncaught.fixture.js'),138 '--parallel',139 '--allow-uncaught'140 ],141 'pipe'142 )[1],143 'when fulfilled',144 'to contain output',145 /Error: existential isolation/i146 ).and('when fulfilled', 'to have exit code', 1);147 });148 });149 describe('when used with --file', function() {150 it('should error out', function() {151 return expect(152 invokeMochaAsync(153 [154 '--file',155 path.join('options', 'parallel', 'test-a.fixture.js'),156 '--parallel'157 ],158 'pipe'159 )[1],160 'when fulfilled',161 'to contain output',162 /mutually exclusive with --file/163 );164 });165 });166 describe('when used with --sort', function() {167 it('should error out', function() {168 return expect(169 invokeMochaAsync(170 [171 '--sort',172 path.join(173 __dirname,174 '..',175 'fixtures',176 'options',177 'parallel',178 'test-*.fixture.js'179 ),180 '--parallel'181 ],182 'pipe'183 )[1],184 'when fulfilled',185 'to contain output',186 /mutually exclusive with --sort/187 );188 });189 });190 describe('when used with exclusive tests', function() {191 it('should error out', function() {192 return expect(193 invokeMochaAsync(194 [195 path.join(196 __dirname,197 '..',198 'fixtures',199 'options',200 'parallel',201 'exclusive-test-*.fixture.js'202 ),203 '--parallel'204 ],205 'pipe'206 )[1],207 'when fulfilled',208 'to contain output',209 /`\.only` is not supported in parallel mode/210 );211 });212 });213 describe('when used with --bail', function() {214 it('should skip some tests', function() {215 return runMochaAsync(216 path.join('options', 'parallel', 'test-*.fixture.js'),217 ['--parallel', '--bail']218 ).then(function(result) {219 // we don't know _exactly_ how many tests will be skipped here220 // due to the --bail, but the number of tests completed should be221 // less than the total, which is 5.222 return expect(223 result.passing + result.pending + result.failing,224 'to be less than',225 5226 );227 });228 });229 it('should fail', function() {230 return expect(231 runMochaAsync(path.join('options', 'parallel', 'test-*.fixture.js'), [232 '--parallel',233 '--bail'234 ]),235 'when fulfilled',236 'to have failed'237 );238 });239 });240 describe('when encountering a "bail" in context', function() {241 it('should skip some tests', function() {242 return runMochaAsync('options/parallel/bail', ['--parallel']).then(243 function(result) {244 return expect(245 result.passing + result.pending + result.failing,246 'to be less than',247 2248 );249 }250 );251 });252 it('should fail', function() {253 return expect(254 runMochaAsync('options/parallel/bail', ['--parallel', '--bail']),255 'when fulfilled',256 'to have failed'257 );258 });259 });260 describe('when used with "grep"', function() {261 it('should be equivalent to running in serial', function() {262 return runMochaAsync(263 path.join('options', 'parallel', 'test-*.fixture.js'),264 ['--no-parallel', '--grep="suite d"']265 ).then(function(expected) {266 return expect(267 runMochaAsync(path.join('options', 'parallel', 'test-*.fixture.js'), [268 '--parallel',269 '--grep="suite d"'270 ]),271 'to be fulfilled with value satisfying',272 {273 passing: expected.passing,274 failing: expected.failing,275 pending: expected.pending,276 code: expected.code277 }278 );279 });280 });281 });282 describe('reporter equivalence', function() {283 // each reporter name is duplicated; one is in all lower-case284 // 'base' is abstract, 'html' is browser-only, others are incompatible285 var DENY = ['progress', 'base', 'html', 'markdown', 'json-stream'];286 Object.keys(Mocha.reporters)287 .filter(function(name) {288 return /^[a-z]/.test(name) && DENY.indexOf(name) === -1;289 })290 .forEach(function(reporter) {291 describe(292 'when multiple test files run with --reporter=' + reporter,293 function() {294 it('should have the same result as when run with --no-parallel', function() {295 // note that the output may not be in the same order, as running file296 // order is non-deterministic in parallel mode297 return runMochaAsync(298 path.join('options', 'parallel', 'test-*.fixture.js'),299 ['--reporter', reporter, '--no-parallel']300 ).then(function(expected) {301 return expect(302 runMochaAsync(303 path.join('options', 'parallel', 'test-*.fixture.js'),304 ['--reporter', reporter, '--parallel']305 ),306 'to be fulfilled with value satisfying',307 {308 passing: expected.passing,309 failing: expected.failing,310 pending: expected.pending,311 code: expected.code312 }313 );314 });315 });316 }...

Full Screen

Full Screen

helpers.js

Source:helpers.js Github

copy

Full Screen

...158 * @param {Options} [args] - Command-line arguments to the `mocha` executable159 * @param {Object} [opts] - Options for `child_process.spawn`.160 * @returns {Promise<Summary>}161 */162function runMochaAsync(fixturePath, args, opts) {163 return new Promise(function(resolve, reject) {164 runMocha(165 fixturePath,166 args,167 function(err, result) {168 if (err) {169 return reject(err);170 }171 resolve(result);172 },173 opts174 );175 });176}...

Full Screen

Full Screen

global-fixtures.spec.js

Source:global-fixtures.spec.js Github

copy

Full Screen

...12describe('global setup/teardown', function() {13 describe('when mocha run in serial mode', function() {14 it('should execute global setup and teardown', async function() {15 return expect(16 runMochaAsync(DEFAULT_FIXTURE, [17 '--require',18 resolveFixturePath('plugins/global-fixtures/global-setup-teardown')19 ]),20 'when fulfilled',21 'to have passed'22 );23 });24 describe('when only global teardown is supplied', function() {25 it('should run global teardown', async function() {26 return expect(27 runMochaAsync(DEFAULT_FIXTURE, [28 '--require',29 resolveFixturePath('plugins/global-fixtures/global-teardown')30 ]),31 'when fulfilled',32 'to contain once',33 /teardown schmeardown/34 );35 });36 });37 describe('when only global setup is supplied', function() {38 it('should run global setup', async function() {39 return expect(40 runMochaAsync(DEFAULT_FIXTURE, [41 '--require',42 resolveFixturePath('plugins/global-fixtures/global-setup')43 ]),44 'when fulfilled',45 'to contain once',46 /setup schmetup/47 );48 });49 });50 it('should share context', async function() {51 return expect(52 runMochaAsync(DEFAULT_FIXTURE, [53 '--require',54 resolveFixturePath('plugins/global-fixtures/global-setup-teardown')55 ]),56 'when fulfilled',57 'to contain once',58 /setup: this\.foo = bar[\s\S]+teardown: this\.foo = bar/59 );60 });61 describe('when supplied multiple functions', function() {62 it('should execute them sequentially', async function() {63 return expect(64 runMochaAsync(DEFAULT_FIXTURE, [65 '--require',66 resolveFixturePath(67 'plugins/global-fixtures/global-setup-teardown-multiple'68 )69 ]),70 'when fulfilled',71 'to contain once',72 /teardown: this.foo = 3/73 );74 });75 });76 describe('when run in watch mode', function() {77 let tempDir;78 let testFile;79 let removeTempDir;80 beforeEach(async function() {81 const tempInfo = await createTempDir();82 tempDir = tempInfo.dirpath;83 removeTempDir = tempInfo.removeTempDir;84 testFile = path.join(tempDir, 'test.js');85 copyFixture(DEFAULT_FIXTURE, testFile);86 });87 afterEach(async function() {88 if (removeTempDir) {89 return removeTempDir();90 }91 });92 it('should execute global setup and teardown', async function() {93 return expect(94 runMochaWatchAsync(95 [96 '--require',97 resolveFixturePath(98 'plugins/global-fixtures/global-setup-teardown'99 ),100 testFile101 ],102 tempDir,103 () => {104 touchFile(testFile);105 }106 ),107 'when fulfilled',108 'to have passed'109 );110 });111 describe('when only global teardown is supplied', function() {112 it('should run global teardown', async function() {113 return expect(114 runMochaWatchAsync(115 [116 '--require',117 resolveFixturePath('plugins/global-fixtures/global-teardown'),118 testFile119 ],120 tempDir,121 () => {122 touchFile(testFile);123 }124 ),125 'when fulfilled',126 'to contain once',127 /teardown schmeardown/128 );129 });130 });131 describe('when only global setup is supplied', function() {132 it('should run global setup', async function() {133 return expect(134 runMochaWatchAsync(135 [136 '--require',137 resolveFixturePath('plugins/global-fixtures/global-setup'),138 testFile139 ],140 tempDir,141 () => {142 touchFile(testFile);143 }144 ),145 'when fulfilled',146 'to contain once',147 /setup schmetup/148 );149 });150 });151 it('should not re-execute the global fixtures', async function() {152 return expect(153 runMochaWatchAsync(154 [155 '--require',156 resolveFixturePath(157 'plugins/global-fixtures/global-setup-teardown-multiple'158 ),159 testFile160 ],161 tempDir,162 () => {163 touchFile(testFile);164 }165 ),166 'when fulfilled',167 'to contain once',168 /teardown: this.foo = 3/169 );170 });171 });172 });173 describe('when mocha run in parallel mode', function() {174 it('should execute global setup and teardown', async function() {175 return expect(176 runMochaAsync(DEFAULT_FIXTURE, [177 '--parallel',178 '--require',179 resolveFixturePath('plugins/global-fixtures/global-setup-teardown')180 ]),181 'when fulfilled',182 'to have passed'183 );184 });185 it('should share context', async function() {186 return expect(187 runMochaAsync(DEFAULT_FIXTURE, [188 '--parallel',189 '--require',190 resolveFixturePath('plugins/global-fixtures/global-setup-teardown')191 ]),192 'when fulfilled',193 'to contain once',194 /setup: this.foo = bar/195 ).and('when fulfilled', 'to contain once', /teardown: this.foo = bar/);196 });197 describe('when supplied multiple functions', function() {198 it('should execute them sequentially', async function() {199 return expect(200 runMochaAsync(DEFAULT_FIXTURE, [201 '--parallel',202 '--require',203 resolveFixturePath(204 'plugins/global-fixtures/global-setup-teardown-multiple'205 )206 ]),207 'when fulfilled',208 'to contain once',209 /teardown: this.foo = 3/210 );211 });212 });213 describe('when run in watch mode', function() {214 let tempDir;...

Full Screen

Full Screen

test-runner.js

Source:test-runner.js Github

copy

Full Screen

...61 const wasmFile = file.replace(/test\.js$/, "asc.wasm");62 this.rawModule = await fs.readFile(wasmFile);63 }64 });65 const numFailures = await runMochaAsync(mocha);66 return numFailures;67}68async function runMochaAsync(mocha) {69 await mocha.loadFilesAsync();70 return new Promise(resolve => mocha.run(resolve));71}72const PORT = process.env.PORT ?? 50123;73const OPEN_DEVTOOLS = !!process.env.OPEN_DEVTOOLS;74async function getNumFailingTestsInPuppeteer() {75 const testFiles = await glob("./tests/**/test.js");76 const browser = await pptr.launch({77 devtools: OPEN_DEVTOOLS,78 ...(process.env.PUPPETEER_EXECUTABLE_PATH?.length > 079 ? { executablePath: process.env.PUPPETEER_EXECUTABLE_PATH }80 : {})81 });82 const page = await browser.newPage();...

Full Screen

Full Screen

esm.spec.js

Source:esm.spec.js Github

copy

Full Screen

...35 });36 });37 it('should show file location when there is a syntax error in the test', async function() {38 var fixture = 'esm/syntax-error/esm-syntax-error.fixture.mjs';39 const err = await runMochaAsync(fixture, args, {stdio: 'pipe'}).catch(40 err => err41 );42 expect(err.output, 'to contain', 'SyntaxError').and(43 'to contain',44 'esm-syntax-error.fixture.mjs'45 );46 });47 it('should recognize esm files ending with .js due to package.json type flag', function(done) {48 if (!utils.supportsEsModules(false)) return this.skip();49 var fixture = 'esm/js-folder/esm-in-js.fixture.js';50 run(fixture, args, function(err, result) {51 if (err) {52 done(err);53 return;...

Full Screen

Full Screen

common-js-require.spec.js

Source:common-js-require.spec.js Github

copy

Full Screen

1'use strict';2const {runMochaAsync} = require('./helpers');3describe('common js require', () => {4 it('should be able to run a test where all mocha exports are used', async () => {5 const result = await runMochaAsync('common-js-require.fixture.js', [6 '--delay'7 ]);8 expect(result.output, 'to contain', 'running before');9 expect(result.output, 'to contain', 'running suiteSetup');10 expect(result.output, 'to contain', 'running setup');11 expect(result.output, 'to contain', 'running beforeEach');12 expect(result.output, 'to contain', 'running it');13 expect(result.output, 'to contain', 'running afterEach');14 expect(result.output, 'to contain', 'running teardown');15 expect(result.output, 'to contain', 'running suiteTeardown');16 expect(result.output, 'to contain', 'running after');17 });...

Full Screen

Full Screen

jobs.spec.js

Source:jobs.spec.js Github

copy

Full Screen

...4describe('--jobs', function() {5 describe('when set to a number less than 2', function() {6 it('should run tests in serial', function() {7 return expect(8 runMochaAsync(9 'options/jobs/fail-in-parallel',10 ['--parallel', '--jobs', '1'],11 'pipe'12 ),13 'when fulfilled',14 'to have passed'15 );16 });17 });18 describe('when set to a number greater than 1', function() {19 it('should run tests in parallel', function() {20 return expect(21 runMochaAsync(22 'options/jobs/fail-in-parallel',23 ['--parallel', '--jobs', '2'],24 'pipe'25 ),26 'when fulfilled',27 'to have failed'28 );29 });30 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var Mocha = require('mocha');2var mocha = new Mocha();3mocha.addFile('test.js');4mocha.run(function(failures){5 process.on('exit', function () {6 });7});8var Mocha = require('mocha');9var mocha = new Mocha();10mocha.addFile('test.js');11mocha.run(function(failures){12 process.on('exit', function () {13 });14});15var Mocha = require('mocha');16var mocha = new Mocha();17mocha.addFile('test.js');18mocha.run(function(failures){19 process.on('exit', function () {20 });21});22var Mocha = require('mocha');23var mocha = new Mocha();24mocha.addFile('test.js');25mocha.run(function(failures){26 process.on('exit', function () {27 });28});29var Mocha = require('mocha');30var mocha = new Mocha();31mocha.addFile('test.js');32mocha.run(function(failures){33 process.on('exit', function () {34 });35});36var Mocha = require('mocha');37var mocha = new Mocha();38mocha.addFile('test.js');39mocha.run(function(failures){40 process.on('exit', function () {41 });42});

Full Screen

Using AI Code Generation

copy

Full Screen

1const mocha = new Mocha();2mocha.addFile('./test/test.js');3mocha.run(function(failures){4 process.on('exit', function () {5 });6});7const assert = require('assert');8describe('Array', function() {9 describe('#indexOf()', function() {10 it('should return -1 when the value is not present', function() {11 assert.equal(-1, [1,2,3].indexOf(4));12 });13 });14});

Full Screen

Using AI Code Generation

copy

Full Screen

1const Mocha = require('mocha');2const mocha = new Mocha();3mocha.addFile('./test/test.js');4mocha.runMochaAsync()5 .then((failures) => {6 if (failures) {7 process.exitCode = 1;8 }9 })10 .catch((err) => {11 console.error(err);12 process.exitCode = 1;13 });14const Mocha = require('mocha');15const mocha = new Mocha();16mocha.addFile('./test/test.js');17mocha.runMochaAsync()18 .then((failures) => {19 if (failures) {20 process.exitCode = 1;21 }22 })23 .catch((err) => {24 console.error(err);25 process.exitCode = 1;26 });27const Mocha = require('mocha');28const mocha = new Mocha();29mocha.addFile('./test/test.js');30mocha.runMochaAsync()31 .then((failures) => {32 if (failures) {33 process.exitCode = 1;34 }35 })36 .catch((err) => {37 console.error(err);38 process.exitCode = 1;39 });40const Mocha = require('mocha');41const mocha = new Mocha();42mocha.addFile('./test/test.js');43mocha.runMochaAsync()44 .then((failures) => {45 if (failures) {46 process.exitCode = 1;47 }48 })49 .catch((err) => {50 console.error(err);51 process.exitCode = 1;52 });53const Mocha = require('mocha');54const mocha = new Mocha();55mocha.addFile('./test/test.js');56mocha.runMochaAsync()57 .then((failures) => {58 if (failures) {59 process.exitCode = 1;60 }61 })62 .catch((err) => {63 console.error(err);

Full Screen

Using AI Code Generation

copy

Full Screen

1const Mocha = require('mocha');2const mocha = new Mocha();3mocha.addFile('./test/test.js');4mocha.runMochaAsync().then(function (failures) {5 process.on('exit', function () {6 process.exit(failures);7 });8});

Full Screen

Using AI Code Generation

copy

Full Screen

1const Mocha = require('mocha');2const mocha = new Mocha();3mocha.addFile('./test/functional/test.js');4mocha.run(failures => {5 process.on('exit', () => {6 });7});

Full Screen

Using AI Code Generation

copy

Full Screen

1const mocha = require('mocha');2const Mocha = new mocha();3const path = require('path');4const testDir = path.join(__dirname, 'test');5Mocha.addFile(path.join(testDir, 'test1.js'));6Mocha.addFile(path.join(testDir, 'test2.js'));7Mocha.run((failures) => {8 process.on('exit', () => {9 });10});11describe('test1', function() {12 it('should pass', function() {13 console.log('test1');14 });15});16describe('test2', function() {17 it('should pass', function() {18 console.log('test2');19 });20});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mocha = new Mocha();2mocha.addFile('./test1.js');3mocha.addFile('./test2.js');4mocha.runMochaAsync().then(function(){5 console.log("All tests completed");6});7it('test1', function() {8 assert.equal(1, 1);9});10it('test2', function() {11 assert.equal(2, 2);12});

Full Screen

Using AI Code Generation

copy

Full Screen

1async function runMochaAsync() {2 try {3 const mocha = new Mocha({4 });5 mocha.addFile('./test.js');6 await mocha.run();7 } catch (err) {8 console.error(err);9 }10}11runMochaAsync();12const assert = require('assert');13describe('Array', () => {14 describe('#indexOf()', () => {15 it('should return -1 when the value is not present', () => {16 assert.equal([1, 2, 3].indexOf(4), -1);17 });18 });19});20{21 "scripts": {22 },23 "devDependencies": {24 }25}

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mocha automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful