Docsity

OpenJS Foundation Mocha Practice Exam, Exams of Technology

Toggle sidebar
bg1
OpenJS Foundation Mocha Practice Exam
**Question 1. Which npm command installs Mocha as a development‑only dependency?**
A) npm install mocha –save
B) npm install mocha –global
C) npm install mocha –save-dev
D) npm install mocha –production
Answer: C
Explanation: The --save-dev flag adds the package to devDependencies so it is only required for
development and testing.
**Question 2. After installing Mocha locally, which script entry runs the test suite with the
default configuration?**
A) "test": "mocha"
B) "test": "node mocha"
C) "test": "npm run mocha"
D) "test": "mocha --install"
Answer: A
Explanation: Adding "test": "mocha" to package.json lets npm test invoke the Mocha CLI with
default settings.
**Question 3. In a typical project layout, which directory name is conventionally used for
Mocha test files?**
A) /src
B) /specs
C) /test
D) /lib
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60

Partial preview of the text

Download OpenJS Foundation Mocha Practice Exam and more Exams Technology in PDF only on Docsity!

Question 1. Which npm command installs Mocha as a development‑only dependency? A) npm install mocha – save B) npm install mocha – global C) npm install mocha – save-dev D) npm install mocha – production Answer: C Explanation: The --save-dev flag adds the package to devDependencies so it is only required for development and testing. Question 2. After installing Mocha locally, which script entry runs the test suite with the default configuration? A) "test": "mocha" B) "test": "node mocha" C) "test": "npm run mocha" D) "test": "mocha --install" Answer: A Explanation: Adding "test": "mocha" to package.json lets npm test invoke the Mocha CLI with default settings. Question 3. In a typical project layout, which directory name is conventionally used for Mocha test files? A) /src B) /specs C) /test D) /lib

Answer: C Explanation: Mocha’s default file‑search pattern looks for files under a test directory. Question 4. Which file extension pattern does Mocha automatically recognize for test files? A) *.spec.js B) *.test.ts C) *.spec.tsx D) *.spec.jsx Answer: A Explanation: By default Mocha includes files ending in *.spec.js or *.test.js when searching the test folder. Question 5. Which configuration file allows you to set Mocha options in JSON format? A) .mocharc.yml B) .mocharc.js C) .mocharc.json D) mocha.config.js Answer: C Explanation: .mocharc.json stores Mocha configuration as a JSON object. Question 6. Which of the following statements about global Mocha installation is true? A) Global installs guarantee the same version across all projects. B) Global installs make the mocha command available without npx or npm scripts.

Question 9. Which function defines an individual test case in the BDD interface? A) it() B) test() C) spec() D) case() Answer: A Explanation: it() contains the actual test logic and description. Question 10. In the TDD interface, which function replaces describe()? A) suite() B) spec() C) context() D) group() Answer: A Explanation: The TDD interface uses suite() to create a test group. Question 11. Which TDD interface function is equivalent to it()? A) test() B) case() C) spec() D) should() Answer: A Explanation: test() defines a single test case in the TDD style.

Question 12. How can you mark a test as pending without providing a callback? A) it.only('test') B) it.skip('test') C) it('test') D) it('test', null) Answer: C Explanation: Omitting the callback function makes the test pending (it will be reported as “pending”). Question 13. Which hook runs once before all tests in a suite? A) beforeEach() B) after() C) before() D) afterEach() Answer: C Explanation: before() executes a single time before any test in the suite runs. Question 14. In nested suites, which hook runs first? A) outer beforeEach() B) inner beforeEach() C) outer before() D) inner before()

C) it('test', () => { fetch().then(done); }); D) it('test', (done) => { fetch().then(() => done).catch(done); }); Answer: A Explanation: Calling done() inside the test signals Mocha that the async operation finished; the test must accept the done parameter. Question 18. When returning a Promise from an it() block, how does Mocha determine test success? A) It treats any returned value as success. B) It waits for the Promise to resolve; rejection marks failure. C) It ignores the Promise and runs synchronously. D) It only checks for thrown exceptions. Answer: B Explanation: Mocha automatically resolves returned Promises; a rejection triggers a test failure. Question 19. Which syntax correctly uses async/await inside an it() test? A) it('test', async () => { const data = await fetch(); expect(data).to.exist; }); B) it('test', () => { const data = await fetch(); }); C) it('test', async (done) => { const data = await fetch(); done(); }); D) it('test', async function() { return await fetch(); }); Answer: A Explanation: The test function is marked async, allowing await; Mocha waits for the returned promise.

Question 20. What is Mocha’s default test timeout value? A) 1000 ms B) 2000 ms C) 5000 ms D) No timeout Answer: B Explanation: Mocha defaults to a 2000 ms timeout for each test unless overridden. Question 21. How can you increase the timeout for a specific test to 10 seconds? A) it('test', { timeout: 10000 }, () => {}); B) it('test', function() { this.timeout(10000); /* test body */ }); C) it('test', () => { this.timeout(10000); }); D) it('test', async () => { this.timeout(10000); }); Answer: B Explanation: Inside a regular function (not arrow), this.timeout(10000) sets the timeout for that test. Question 22. Which CLI flag runs only tests whose titles match the given pattern? A) --only B) --grep C) --focus D) --run Answer: B

Answer: A Explanation: The spec reporter displays suites and tests in a nested, colored format with ✓ for passed tests. Question 26. Which built‑in reporter outputs a single dot for each test? A) spec B) dot C) list D) nyan Answer: B Explanation: The dot reporter prints a dot per test, useful for large suites. Question 27. What exit code does Mocha return when all tests pass? A) 0 B) 1 C) 2 D) 100 Answer: A Explanation: A zero exit code signals success to the shell and CI pipelines. Question 28. Which exit code indicates that at least one test failed? A) 0 B) 1

C) 2

D) 255

Answer: B Explanation: Mocha exits with 1 when there are test failures. Question 29. How can you enable watch mode to re‑run tests on file changes? A) mocha --watch B) mocha --auto C) mocha --reload D) mocha --monitor Answer: A Explanation: The --watch flag activates file‑system watching and re‑executes tests on changes. Question 30. Which flag enables parallel execution of test files in Mocha 8+? A) --parallel B) --concurrent C) --threads D) --multi Answer: A Explanation: --parallel runs test files in separate processes for faster execution. Question 31. When using --parallel, which of the following is NOT allowed? A) beforeEach hooks

Question 34. Which assertion style does Chai’s expect interface provide? A) function‑call syntax B) chainable BDD syntax C) object‑oriented syntax D) plain string comparison Answer: B Explanation: expect allows natural‑language chains like expect(value).to.equal(5). Question 35. Which of the following correctly asserts that a promise is rejected using Chai as promised? A) return expect(promise).to.be.rejected; B) expect(promise).rejects(); C) await expect(promise).to.be.rejected; D) expect(promise).to.be.rejected(); Answer: A Explanation: With chai‑as‑promised, returning the expectation lets Mocha wait for the promise rejection. Question 36. Which Node.js built‑in module can be used for simple assertions without external libraries? A) assert B) test C) expect D) should

Answer: A Explanation: The assert module provides basic assertion functions like assert.strictEqual. Question 37. How does Mocha differentiate between an assertion failure and an uncaught exception? A) Both are shown as “Error” with the same stack trace. B) Assertion failures are labeled “AssertionError” while uncaught exceptions retain their original type. C) Mocha only reports uncaught exceptions. D) Assertion failures abort the test suite immediately. Answer: B Explanation: Assertion libraries throw an AssertionError; Mocha reports it distinctly from other exceptions. Question 38. Which reporter outputs test results as a JSON object suitable for programmatic consumption? A) json B) spec C) list D) nyan Answer: A Explanation: The json reporter prints a JSON representation of the test run. Question 39. Which command runs Mocha with the spec reporter explicitly?

Question 42. In a test file, which of the following correctly shares a variable across multiple tests using beforeEach()? A) let count; beforeEach(() => { count = 0; }); it('test', () => { count++; }); B) var count; beforeEach(function() { this.count = 0; }); it('test', function() { this.count++; }); C) const count = 0; beforeEach(() => { count = 0; }); D) let count = 0; before(() => { count = 0; }); Answer: A Explanation: Declaring let count in the outer scope and resetting it in beforeEach makes it fresh for each test. Question 43. Which of the following is the correct way to skip an entire suite? A) describe.skip('suite', () => {}); B) describe('suite', () => {}).skip; C) it.skip('suite', () => {}); D) suite.skip('suite', () => {}); Answer: A Explanation: describe.skip marks the whole suite as pending. Question 44. Which of these statements about Mocha’s --require option is true? A) It loads a module before any test files are executed. B) It only loads modules after the first test runs. C) It replaces the need for import statements inside test files. D) It disables all hooks. Answer: A

Explanation: --require pre‑loads the specified module, useful for setting up globals or transpilers. Question 45. When using Sinon, which method creates a spy for an existing function? A) sinon.stub(fn) B) sinon.spy(fn) C) sinon.mock(fn) D) sinon.fake(fn) Answer: B Explanation: sinon.spy(fn) wraps the original function while recording calls. Question 46. Which Sinon method replaces a function with a stub that returns a specific value? A) sinon.spy().returns(value) B) sinon.stub().returns(value) C) sinon.mock().returns(value) D) sinon.fake().returns(value) Answer: B Explanation: sinon.stub().returns(value) creates a stub that always returns the given value. Question 47. What does the --exit flag do in Mocha? A) Forces the process to exit after tests complete, even if there are pending async operations. B) Prevents Mocha from exiting on failure. C) Exits immediately without running any tests. D) Exits after the first failing test.

C) mocha --no-colors D) mocha --plain Answer: A Explanation: --no-color turns off ANSI color codes in the output. Question 51. Which of the following is NOT a built‑in Mocha reporter? A) spec B) dot C) nyan D) html Answer: D Explanation: Mocha does not ship with an HTML reporter; external packages provide that functionality. Question 52. In a test suite, which hook would you use to open a database connection once for all tests? A) beforeEach() B) after() C) before() D) afterEach() Answer: C Explanation: before() runs a single time before any test, ideal for one‑time setup like DB connections.

Question 53. Which syntax correctly asserts that an array includes the value 42 using Chai’s should style? A) array.should.include(42); B) should.include(array, 42); C) expect(array).to.have(42); D) assert.includes(array, 42); Answer: A Explanation: The should interface adds methods to objects; array.should.include(42) checks inclusion. Question 54. Which of the following statements about Mocha’s --parallel mode is correct? A) Tests within the same file are run concurrently. B) Each test file runs in its own worker process. C) Only the first test file is executed. D) Parallel mode disables all hooks. Answer: B Explanation: --parallel spawns a separate process per test file, allowing file‑level concurrency. Question 55. Which method allows you to programmatically add a custom CLI option to Mocha? A) mocha.addOption() B) --require custom-options.js C) mocha --extend options.js D) Mocha.addListener()