Best JavaScript code snippet using root
reference-data.js
Source:reference-data.js
1var JsReporters = require('../../dist/js-reporters.js')2var SuiteStart = JsReporters.SuiteStart3var SuiteEnd = JsReporters.SuiteEnd4var TestStart = JsReporters.TestStart5var TestEnd = JsReporters.TestEnd6var noErrors = []7var noAssertions = []8/**9 * All props are undefined, except "passed", because we don't know what they10 * will contain, which is depending from framework to framework. The props are11 * anyway verified one by one, see "adapters.js" file.12 */13var errors = [{14 passed: false,15 actual: undefined,16 expected: undefined,17 message: undefined,18 stack: undefined,19 todo: undefined20}]21var failedAssertions = [{22 passed: false,23 actual: undefined,24 expected: undefined,25 message: undefined,26 stack: undefined,27 todo: undefined28}]29var passedAssertions = [{30 passed: true,31 actual: undefined,32 expected: undefined,33 message: undefined,34 stack: undefined,35 todo: undefined36}]37var globalTestStart = new TestStart('global test', undefined, ['global test'])38var globalTestEnd = new TestEnd('global test', undefined, ['global test'],39 'passed', 0, noErrors, passedAssertions)40var passingTestStart1 = new TestStart('should pass', 'suite with passing test',41 ['suite with passing test', 'should pass'])42var passingTestEnd1 = new TestEnd('should pass', 'suite with passing test',43 ['suite with passing test', 'should pass'], 'passed', 0, noErrors,44 passedAssertions)45var passingTestStart2 = new TestStart('should pass', 'suite with tests',46 ['suite with tests', 'should pass'])47var passingTestEnd2 = new TestEnd('should pass', 'suite with tests',48 ['suite with tests', 'should pass'], 'passed', 0, noErrors,49 passedAssertions)50var skippedTestStart1 = new TestStart('should skip', 'suite with skipped test',51 ['suite with skipped test', 'should skip'])52var skippedTestEnd1 = new TestEnd('should skip', 'suite with skipped test',53 ['suite with skipped test', 'should skip'], 'skipped', undefined, noErrors,54 noAssertions)55var skippedTestStart2 = new TestStart('should skip', 'suite with tests',56 ['suite with tests', 'should skip'])57var skippedTestEnd2 = new TestEnd('should skip', 'suite with tests',58 ['suite with tests', 'should skip'], 'skipped', undefined, noErrors,59 noAssertions)60var failingTestStart1 = new TestStart('should fail', 'suite with failing test',61 ['suite with failing test', 'should fail'])62var failingTestEnd1 = new TestEnd('should fail', 'suite with failing test',63 ['suite with failing test', 'should fail'], 'failed', 0, errors,64 failedAssertions)65var failingTestStart2 = new TestStart('should fail', 'suite with tests',66 ['suite with tests', 'should fail'])67var failingTestEnd2 = new TestEnd('should fail', 'suite with tests',68 ['suite with tests', 'should fail'], 'failed', 0, errors,69 failedAssertions)70var innerTestStart = new TestStart('inner test', 'inner suite',71 ['outter suite', 'inner suite', 'inner test'])72var innerTestEnd = new TestEnd('inner test', 'inner suite', ['outter suite',73 'inner suite', 'inner test'], 'passed', 0, noErrors, passedAssertions)74var outterTestStart = new TestStart('outter test', 'outter suite',75 ['outter suite', 'outter test'])76var outterTestEnd = new TestEnd('outter test', 'outter suite', ['outter suite',77 'outter test'], 'passed', 0, noErrors, passedAssertions)78var passingSuiteStart = new SuiteStart('suite with passing test',79 ['suite with passing test'], [passingTestStart1], [])80var passingSuiteEnd = new SuiteEnd('suite with passing test',81 ['suite with passing test'], [passingTestEnd1], [])82var skippedSuiteStart = new SuiteStart('suite with skipped test',83 ['suite with skipped test'], [skippedTestStart1], [])84var skippedSuiteEnd = new SuiteEnd('suite with skipped test',85 ['suite with skipped test'], [skippedTestEnd1], [])86var failingSuiteStart = new SuiteStart('suite with failing test',87 ['suite with failing test'], [failingTestStart1], [])88var failingSuiteEnd = new SuiteEnd('suite with failing test',89 ['suite with failing test'], [failingTestEnd1], [])90var testSuiteStart = new SuiteStart('suite with tests', ['suite with tests'], [91 passingTestStart2,92 skippedTestStart2,93 failingTestStart294], [])95var testSuiteEnd = new SuiteEnd('suite with tests', ['suite with tests'], [96 passingTestEnd2,97 skippedTestEnd2,98 failingTestEnd299], [])100var innerSuiteStart = new SuiteStart('inner suite',101 ['outter suite', 'inner suite'], [innerTestStart], [])102var innerSuiteEnd = new SuiteEnd('inner suite', ['outter suite', 'inner suite'],103 [innerTestEnd], [])104var outterSuiteStart = new SuiteStart('outter suite', ['outter suite'],105 [outterTestStart], [innerSuiteStart])106var outterSuiteEnd = new SuiteEnd('outter suite', ['outter suite'],107 [outterTestEnd], [innerSuiteEnd])108var globalSuiteStart = new SuiteStart(undefined, [], [globalTestStart], [109 passingSuiteStart,110 skippedSuiteStart,111 failingSuiteStart,112 testSuiteStart,113 outterSuiteStart114])115var globalSuiteEnd = new SuiteEnd(undefined, [], [globalTestEnd], [116 passingSuiteEnd,117 skippedSuiteEnd,118 failingSuiteEnd,119 testSuiteEnd,120 outterSuiteEnd121])122module.exports = [123 ['runStart', globalSuiteStart, 'global suite starts'],124 ['testStart', globalTestStart, 'global test starts'],125 ['testEnd', globalTestEnd, 'global test ends'],126 ['suiteStart', passingSuiteStart, 'suite with one passing test starts'],127 ['testStart', passingTestStart1, 'passing test starts'],128 ['testEnd', passingTestEnd1, 'passing test ends'],129 ['suiteEnd', passingSuiteEnd, 'suite with one passing test ends', 'passed'],130 ['suiteStart', skippedSuiteStart, 'suite with one skipped test starts'],131 ['testStart', skippedTestStart1, 'skipped test starts'],132 ['testEnd', skippedTestEnd1, 'skipped test ends'],133 ['suiteEnd', skippedSuiteEnd, 'suite with one skipped test ends', 'skipped'],134 ['suiteStart', failingSuiteStart, 'suite with one failing tests'],135 ['testStart', failingTestStart1, 'failing test starts'],136 ['testEnd', failingTestEnd1, 'failing test ends'],137 ['suiteEnd', failingSuiteEnd, 'suite with one failing test ends', 'failed'],138 ['suiteStart', testSuiteStart, 'suite with multiple tests starts'],139 ['testStart', passingTestStart2, 'passing test starts'],140 ['testEnd', passingTestEnd2, 'passing test ends'],141 ['testStart', skippedTestStart2, 'skipped test starts'],142 ['testEnd', skippedTestEnd2, 'skipped test ends'],143 ['testStart', failingTestStart2, 'failing test starts'],144 ['testEnd', failingTestEnd2, 'failing test ends'],145 ['suiteEnd', testSuiteEnd, 'suite with multiple tests ends', 'failed'],146 ['suiteStart', outterSuiteStart, 'outter suite starts'],147 ['testStart', outterTestStart, 'outter test starts'],148 ['testEnd', outterTestEnd, 'outter test ends'],149 ['suiteStart', innerSuiteStart, 'inner suite starts'],150 ['testStart', innerTestStart, 'inner test starts'],151 ['testEnd', innerTestEnd, 'inner test ends'],152 ['suiteEnd', innerSuiteEnd, 'inner suite ends', 'passed'],153 ['suiteEnd', outterSuiteEnd, 'outter suite ends', 'passed'],154 ['runEnd', globalSuiteEnd, 'global suite ends', 'failed']...
adapters.js
Source:adapters.js
1/* eslint-env qunit */2/* eslint-disable no-unused-expressions */3const { test } = QUnit;4const runAdapters = require('./adapters-run.js');5function rerequire (file) {6 const resolved = require.resolve(file);7 delete require.cache[resolved];8 return require(resolved);9}10/**11 * Collect data from an (adapted) runner.12 */13function collectDataFromRunner (collectedData, done, runner) {14 runner.on('runStart', (runStart) => {15 collectedData.push(['runStart', runStart]);16 });17 runner.on('suiteStart', (suiteStart) => {18 collectedData.push(['suiteStart', suiteStart]);19 });20 runner.on('suiteEnd', (suiteEnd) => {21 normalizeSuiteEnd(suiteEnd);22 collectedData.push(['suiteEnd', suiteEnd]);23 });24 runner.on('testStart', (testStart) => {25 collectedData.push(['testStart', testStart]);26 });27 runner.on('testEnd', (testEnd) => {28 normalizeTestEnd(testEnd);29 collectedData.push(['testEnd', testEnd]);30 });31 runner.on('runEnd', (runEnd) => {32 normalizeRunEnd(runEnd);33 collectedData.push(['runEnd', runEnd]);34 // Notify the integration test to continue, and validate the collected data.35 done();36 });37}38function normalizeTestEnd (test) {39 // Replace any actual assertion runtime with hardcoded 42s.40 // Preserve absence or other weird values as-is.41 if (Number.isFinite(test.runtime)) {42 test.runtime = 42;43 }44 // Only check the "passed" property.45 // Throw away the rest of the actual assertion objects as being framework-specific.46 if (test.assertions) {47 test.assertions.forEach(assertion => {48 Object.keys(assertion).forEach(key => {49 if (key !== 'passed') delete assertion[key];50 });51 });52 }53 if (test.errors) {54 test.errors.forEach(assertion => {55 Object.keys(assertion).forEach(key => {56 if (key !== 'passed') delete assertion[key];57 });58 });59 }60}61function normalizeSuiteEnd (suiteEnd) {62 if (Number.isFinite(suiteEnd.runtime)) {63 suiteEnd.runtime = 42;64 }65}66function normalizeRunEnd (runEnd) {67 if (Number.isFinite(runEnd.runtime)) {68 runEnd.runtime = 42;69 }70}71function fixExpectedData (adapter, expectedData) {72 expectedData.forEach(([eventName, data]) => {73 if (eventName === 'testEnd') {74 // Don't expect passed assertion for testing frameworks75 // that don't record all assertions.76 if (adapter === 'Mocha' && data.status === 'passed') {77 data.assertions = [];78 }79 }80 if (eventName === 'testEnd' || eventName === 'suiteEnd' || eventName === 'runEnd') {81 if (Number.isFinite(data.runtime)) {82 data.runtime = 42;83 }84 }85 });86}87const integrations = [88 { key: 'main', name: 'Adapters main integration', referenceFile: './reference-data.js' },89 // This is only implemented by QUnit currently90 { key: 'todo', name: 'Adapters todo integration', referenceFile: './reference-data-todo.js' }91];92integrations.forEach(function (integration) {93 QUnit.module(integration.name, function () {94 Object.keys(runAdapters[integration.key]).forEach(function (adapter) {95 QUnit.module(adapter + ' adapter', hooks => {96 // Re-require for each adapter because we mutate the expected data.97 const expectedData = rerequire(integration.referenceFile);98 fixExpectedData(adapter, expectedData);99 const collectedData = [];100 hooks.before(assert => {101 const done = assert.async();102 runAdapters[integration.key][adapter](103 collectDataFromRunner.bind(null, collectedData, done)104 );105 });106 // Fist check that, overall, all expected events were emitted and in order.107 test('Emitted events names', assert => {108 assert.propEqual(109 collectedData.map(pair => pair[0]),110 expectedData.map(pair => pair[0]),111 'Event names'112 );113 });114 test('Event "testStart" data', assert => {115 const actuals = collectedData.filter(pair => pair[0] === 'testStart');116 const expecteds = expectedData.filter(pair => pair[0] === 'testStart');117 assert.propEqual(118 actuals.map(expected => expected[1].name),119 expecteds.map(pair => pair[1].name),120 'Test names'121 );122 expecteds.forEach((expected, i) => {123 assert.propEqual(124 actuals[i][1],125 expected[1],126 `Event data for testStart#${i}`127 );128 });129 });130 test('Event "testEnd" data', assert => {131 const actuals = collectedData.filter(pair => pair[0] === 'testEnd');132 const expecteds = expectedData.filter(pair => pair[0] === 'testEnd');133 assert.propEqual(134 actuals.map(expected => expected[1].name),135 expecteds.map(pair => pair[1].name),136 'Test names'137 );138 expecteds.forEach((expected, i) => {139 assert.propEqual(140 actuals[i][1],141 expected[1],142 `Event data for testEnd#${i}`143 );144 });145 });146 test('Event "suiteStart" data', assert => {147 const actuals = collectedData.filter(pair => pair[0] === 'suiteStart');148 const expecteds = expectedData.filter(pair => pair[0] === 'suiteStart');149 assert.propEqual(150 actuals.map(expected => expected[1].name),151 expecteds.map(pair => pair[1].name),152 'Suite names'153 );154 expecteds.forEach((expected, i) => {155 assert.propEqual(156 actuals[i][1],157 expected[1],158 `Event data for suiteStart#${i}`159 );160 });161 });162 test('Event "suiteEnd" data', assert => {163 const actuals = collectedData.filter(pair => pair[0] === 'suiteEnd');164 const expecteds = expectedData.filter(pair => pair[0] === 'suiteEnd');165 assert.propEqual(166 actuals.map(expected => expected[1].name),167 expecteds.map(pair => pair[1].name),168 'Suite names'169 );170 expecteds.forEach((expected, i) => {171 assert.propEqual(172 actuals[i][1],173 expected[1],174 `Event data for suiteEnd#${i}`175 );176 });177 });178 test('Event "runStart" data', assert => {179 const actuals = collectedData.filter(pair => pair[0] === 'runStart');180 expectedData.filter(pair => pair[0] === 'runStart').forEach((expected, i) => {181 assert.propEqual(182 actuals[i][1],183 expected[1],184 `Event data for runStart#${i}`185 );186 });187 });188 test('Event "runEnd" data', assert => {189 const actuals = collectedData.filter(pair => pair[0] === 'runEnd');190 expectedData.filter(pair => pair[0] === 'runEnd').forEach((expected, i) => {191 assert.propEqual(192 actuals[i][1],193 expected[1],194 `Event data for runEnd#${i}`195 );196 });197 });198 });199 });200 });...
Channel.ts
Source:Channel.ts
1import { Task } from '@theintern/common';2import _Channel, { ChannelOptions } from 'src/lib/Channel';3const mockRequire = intern.getPlugin<mocking.MockRequire>('mockRequire');4let Channel: typeof _Channel;5let messages: string[];6let websocketError: 'construct' | 'send' | null;7let removeMocks: () => void;8registerSuite('lib/Channel', {9 before() {10 return mockRequire(require, 'src/lib/Channel', {11 'src/lib/channels/WebSocket': MockWebSocket,12 'src/lib/channels/Http': MockHttp,13 }).then((handle) => {14 removeMocks = handle.remove;15 Channel = handle.module.default;16 });17 },18 after() {19 removeMocks();20 },21 beforeEach() {22 messages = [];23 websocketError = null;24 },25 tests: {26 '#sendMessage': {27 http() {28 const channel = new Channel(<ChannelOptions>{});29 return channel.sendMessage('suiteStart', null).then(() => {30 assert.deepEqual(messages, [31 'constructing http',32 'sending http suiteStart',33 ]);34 });35 },36 websocket() {37 const channel = new Channel(<ChannelOptions>{ port: 1 });38 return channel.sendMessage('suiteStart', null).then(() => {39 assert.deepEqual(messages, [40 'constructing websocket',41 'sending websocket remoteStatus',42 'sending websocket suiteStart',43 ]);44 });45 },46 'http fallback': {47 'websocket error'() {48 const channel = new Channel(<ChannelOptions>{ port: 1 });49 websocketError = 'send';50 return channel.sendMessage('suiteStart', null).then(() => {51 assert.deepEqual(messages, [52 'constructing websocket',53 'sending websocket remoteStatus',54 'constructing http',55 'sending http suiteStart',56 ]);57 });58 },59 'websocket construction error'() {60 const channel = new Channel(<ChannelOptions>{ port: 1 });61 websocketError = 'construct';62 return channel.sendMessage('suiteStart', null).then(() => {63 assert.deepEqual(messages, [64 'constructing websocket',65 'constructing http',66 'sending http suiteStart',67 ]);68 });69 },70 },71 },72 },73});74class MockWebSocket {75 constructor() {76 messages.push('constructing websocket');77 if (websocketError === 'construct') {78 throw new Error('Error constructing');79 }80 }81 sendMessage(event: string) {82 messages.push(`sending websocket ${event}`);83 return websocketError === 'send' ? Task.reject() : Task.resolve();84 }85}86class MockHttp {87 constructor() {88 messages.push('constructing http');89 }90 sendMessage(event: string) {91 messages.push(`sending http ${event}`);92 return Task.resolve();93 }...
Using AI Code Generation
1describe('suite1', function() {2 before(function() {3 console.log('suite1 before');4 });5 after(function() {6 console.log('suite1 after');7 });8 beforeEach(function() {9 console.log('suite1 beforeEach');10 });11 afterEach(function() {12 console.log('suite1 afterEach');13 });14 it('test1', function() {15 console.log('test1');16 });17 it('test2', function() {18 console.log('test2');19 });20 describe('suite2', function() {21 before(function() {22 console.log('suite2 before');23 });24 after(function() {25 console.log('suite2 after');26 });27 beforeEach(function() {28 console.log('suite2 beforeEach');29 });30 afterEach(function() {31 console.log('suite2 afterEach');32 });33 it('test1', function() {34 console.log('test1');35 });36 it('test2', function() {37 console.log('test2');38 });39 });40});
Using AI Code Generation
1describe('Suite 1', function() {2 it('Test 1', function() {3 console.log('Test 1');4 });5});6describe('Suite 2', function() {7 it('Test 2', function() {8 console.log('Test 2');9 });10});11describe('Suite 3', function() {12 it('Test 3', function() {13 console.log('Test 3');14 });15});16before(function() {17 console.log('Before all tests');18});19after(function() {20 console.log('After all tests');21});22beforeEach(function() {23 console.log('Before each suite');24});25afterEach(function() {26 console.log('After each suite');27});28describe('Suite 4', function() {29 it('Test 4', function() {30 console.log('Test 4');31 });32});33describe('Suite 5', function() {34 it('Test 5', function() {35 console.log('Test 5');36 });37});38describe('Suite 6', function() {39 it('Test 6', function() {40 console.log('Test 6');41 });42});
Using AI Code Generation
1describe('Test Suite', function() {2 before(function() {3 console.log('before');4 });5 after(function() {6 console.log('after');7 });8 beforeEach(function() {9 console.log('beforeEach');10 });11 afterEach(function() {12 console.log('afterEach');13 });14 describe('Test Case', function() {15 it('Test Case 1', function() {16 console.log('Test Case 1');17 });18 it('Test Case 2', function() {19 console.log('Test Case 2');20 });21 });22});23describe('Test Suite', function() {24 before(function() {25 console.log('before');26 });27 after(function() {28 console.log('after');29 });30 beforeEach(function() {31 console.log('beforeEach');32 });33 afterEach(function() {34 console.log('afterEach');35 });36 describe('Test Case', function() {37 it('Test Case 1', function() {38 console.log('Test Case 1');39 });40 it('Test Case 2', function() {41 console.log('Test Case 2');42 });43 });44});45describe('Test Suite', function() {46 before(function() {47 console.log('before');48 });49 after(function() {50 console.log('after');51 });52 beforeEach(function() {53 console.log('beforeEach');54 });55 afterEach(function() {56 console.log('afterEach');57 });
Using AI Code Generation
1describe('suite 1', function() {2 before(function() {3 console.log("before hook");4 });5 after(function() {6 console.log("after hook");7 });8 beforeEach(function() {9 console.log("beforeEach hook");10 });11 afterEach(function() {12 console.log("afterEach hook");13 });14 it('test 1', function() {15 console.log("test 1");16 });17 it('test 2', function() {18 console.log("test 2");19 });20});
Using AI Code Generation
1describe('suite', function() {2 this.suiteStart = function() {3 console.log('suiteStart');4 };5 it('test', function() {6 console.log('test');7 });8});9describe('suite', function() {10 describe('child suite', function() {11 this.suiteStart = function() {12 console.log('suiteStart');13 };14 it('test', function() {15 console.log('test');16 });17 });18});19describe('suite', function() {20 describe('child suite', function() {21 describe('grand-child suite', function() {22 this.suiteStart = function() {23 console.log('suiteStart');24 };25 it('test', function() {26 console.log('test');27 });28 });29 });30});31describe('suite', function() {32 this.suiteStart = function() {33 console.log('suiteStart');34 };35 it('test', function() {36 console.log('test');37 });38});39describe('suite', function() {40 describe('child suite', function() {41 this.suiteStart = function() {42 console.log('suiteStart');43 };44 it('test', function() {45 console.log('test');46 });47 });48});49describe('suite', function() {50 describe('child suite', function() {51 describe('grand-child suite', function() {52 this.suiteStart = function() {53 console.log('suiteStart');54 };55 it('test', function() {56 console.log('test');57 });58 });59 });60});61describe('suite', function() {62 this.suiteStart = function() {63 console.log('suiteStart');64 };65 it('test', function() {66 console.log('test');67 });68});69describe('suite', function() {70 describe('child suite', function
Using AI Code Generation
1describe("suite 1", function() {2 it("test case 1", function() {3 });4 it("test case 2", function() {5 });6 describe("suite 2", function() {7 it("test case 3", function() {8 });9 it("test case 4", function() {10 });11 });12});13describe("suite 1", function() {14 it("test case 1", function() {15 });16 it("test case 2", function() {17 });18 describe("suite 2", function() {19 it("test case 3", function() {20 });21 it("test case 4", function() {22 });23 });24});25describe("suite 1", function() {26 it("test case 1", function() {27 });28 it("test case 2", function() {29 });30 describe("suite 2", function() {31 it("test case 3", function() {32 });33 it("test case 4", function() {34 });35 });36});
Using AI Code Generation
1describe('Suite 1', function() {2 this.suiteStart = function(){3 console.log('Suite 1 started');4 }5 it('Test 1', function() {6 console.log('Test 1 executed');7 });8 it('Test 2', function() {9 console.log('Test 2 executed');10 });11});12describe('Suite 2', function() {13 it('Test 3', function() {14 console.log('Test 3 executed');15 });16 it('Test 4', function() {17 console.log('Test 4 executed');18 });19});
Using AI Code Generation
1describe("Root Suite", function() {2 this.suiteStart = function() {3 console.log("Root Suite Started");4 };5 it("Test case 1", function() {6 console.log("Test case 1");7 });8 it("Test case 2", function() {9 console.log("Test case 2");10 });11 describe("Sub Suite", function() {12 it("Test case 3", function() {13 console.log("Test case 3");14 });15 it("Test case 4", function() {16 console.log("Test case 4");17 });18 });19});
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!