in Education by
I have this array of objects. let links = [ { url: 'some url 1', status: 200 }, { url: 'some url 2', status: 200 } ] Which is the result of calling LinkFunction asyncronously inside before: before(async () => { try { links = await LinkFunction(); } catch (err) { assert.fail(err); } }); I would like to check if the url and status properties exist and if their types are correspondingly string and number. Note: The specified object is just a sample of a big response. So the loop is required for iteration in any case. I've done this iteration: it('Array to contain some props', () => { links.map(property => { expect(property).to.have.property('url').to.be.a('string'); expect(property).to.have.property('status').to.be.a('number'); }); }); But I would like to have something like this: it('Array to contain some props', () => {//or describe would be better here links.map(property => { it('link array to contain url string', () => { expect(property).to.have.property('url').to.be.a('string'); }); it('link array to contain status number', () => { expect(property).to.have.property('status').to.be.a('number'); }); }); }); Unfortunately it statements are ignored inside map. Maybe it's because of the several nested it statements. So How can I implement a similar logic? Update: My full code: JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You might want to use forEach instead of map. Also, "Passing arrow functions (aka "lambdas") to Mocha is discouraged" so you will probably want to change those to normal functions. Having said that, it works fine if links is defined as mocha initially runs the test file and collects the individual it tests: const expect = require('chai').expect; describe('links', function() { let links = [ { url: 'some url 1', status: 200 }, { url: 'some url 2', status: 200 } ] links.forEach(function(property) { it('link array to contain url string', function() { expect(property).to.have.property('url').to.be.a('string'); }); it('link array to contain status number', function() { expect(property).to.have.property('status').to.be.a('number'); }); }); }); ..results in: > mocha links √ link array to contain url string √ link array to contain status number √ link array to contain url string √ link array to contain status number 4 passing (14ms) Update As you have found, it only works at the top level or with a describe: before(function() { it('will NOT work here', function() { }); }); it('will work here', function() { it('will NOT work here', function() { }); }); Also, links must be available while the test is first running and the it tests are being collected by mocha so this doesn't work either: describe('links', function() { let links = []; before(function() { links = [ { url: 'some url 1', status: 200 }, { url: 'some url 2', status: 200 } ]; }); // This won't work... links.forEach(function(property) { // .. since links is still an empty array when this runs it('should...', function() { /* ... */ }); }); }); From your question update it looks like your code retrieves links from an async function call in before. Because of this, there is no way to have links populated at the time the test is first running and the it tests are collected. So it looks like you won't be able to map across the items in links to create your it tests, and will instead need to take the approach you described by mapping across the items in links within a single test.

Related questions

0 votes
    Problem I have several tests that do the same thing in mocha. This for me, it's duplication, and is the ... ) you have dataProviders. In phpUnit a dataProvider works this way:...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I asked a question yesterday and none of the answers are working so I decided to start a fresh one ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    const navLink = document.querySelectorAll(".nav-link"); navLink.forEach((link) => link.addEventListener(" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    const navLink = document.querySelectorAll(".nav-link"); navLink.forEach((link) => link.addEventListener(" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    const navLink = document.querySelectorAll(".nav-link"); navLink.forEach((link) => link.addEventListener(" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    ____ allows you to loop through a block of code as long as the specified condition is true. 1. Tag 2. While 3. For...
asked Feb 24, 2021 by JackTerrance
0 votes
    Multiple variables can be created and initialized in a single JavaScript statement 1. False 2. True...
asked Feb 23, 2021 by JackTerrance
0 votes
    Select the statement that has correct JavaScript syntax 1. document.write("text") 2. alert stop ; 3. console.log("text");...
asked Feb 23, 2021 by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    Why is it not a good idea to use SOAP for communicating with the front end? For example, a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    If I toss a coin 3 times and get head each time, should I expect a tail to have a higher change in the ... in support of your answer. Select the correct answer from above options...
asked Nov 21, 2021 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I am looking for a way to close a component when there it a click outisde of the element. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
...