
Question bank
Salesforce Certified JavaScript Developer I
43 Salesforce Certified JavaScript Developer I questions with the correct answer and a written explanation, organised across 7 exam domains.
- 43 questions
- 7 domains
- Foundation level
Question index
Jump straight to any question. Answers and explanations are inline.
- Asynchronous Programming · 1, 2, 3, 4, 5, 6
- Browsers and Events · 7, 8, 9, 10, 11, 12, 13
- Debugging and Error Handling · 14, 15, 16
- Objects, Functions, and Classes · 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
- Server-side JavaScript · 27, 28, 29, 30
- Testing · 31, 32, 33
- Variables, Types, and Collections · 34, 35, 36, 37, 38, 39, 40, 41, 42, 43
Asynchronous Programming
6 questions in this domain
- Question 1Single Choice
What is the main purpose of using mocks or stubs in a unit test?
- AThey simulate external behaviors so the test validates only the target function's logic.Correct answer
- BThey replace the assertion library so tests run faster.
- CThey guarantee that every line of code is covered.
- DThey help the runner discover test files faster.
Why this is the answer
Mocks or stubs isolate the unit under test by simulating external dependencies. This ensures the test only validates the specific logic of the target function, not the behavior of real integrations.
- Question 2Multiple Select
Which two statements correctly describe test assertions?
- AAssertions compare the actual outcome of a function with the expected outcome.Correct answer
- BAssertions are optional when the test executes without errors.
- CWithout assertions, a test executes code but does not verify correctness.Correct answer
- DAssertions are used mainly to control the order of test execution.
Why this is the answer
Assertions validate whether the actual result matches the expected result. Running code without assertions may not fail, but it also does not prove that the code is correct.
- Question 3Interactive
In the code block provided, what is the role of the `.toBe(3)` expression?
- AA matcher that defines how the actual value is compared to the expected value.Correct answer
- BA lifecycle hook that runs before each test.
- CA global object provided by the browser.
- DA test-coverage reporter.
Why this is the answer
The `.toBe()` method is a matcher attached to `expect`. Matchers define the comparison logic used to decide whether the test passes or fails.
- Question 4Single Choice
What does test coverage measure in a test suite?
- AThe percentage of the codebase that was executed during the test run.Correct answer
- BThe number of assertions that passed.
- CThe number of external dependencies that were mocked.
- DThe total time required to run the test suite.
Why this is the answer
Coverage tracks which lines or code paths were executed during the tests. It shows what was touched, not whether those lines were verified correctly.
- Question 5Multiple Select
Which two behaviors are considered common traps in unit testing?
- AUsing a real external service in a unit test.Correct answer
- BMocking external dependencies to isolate the target function.
- CBelieving that 100% line coverage guarantees logical correctness.Correct answer
- DGrouping related tests into suites.
Why this is the answer
A unit test should isolate dependencies; using a real service makes it an integration test. Coverage only indicates which lines ran, so it cannot guarantee logical correctness.
- Question 6Interactive
What is the defect in the asynchronous assertion shown in the code block?
- AThe `expect` call is not awaited, so the assertion may never be evaluated.Correct answer
- BThe `resolves` matcher cannot be used with string values.
- CThe test must be synchronous when using `resolves`.
- DThe `fetchData` function must be defined inside the test.
Why this is the answer
`expect(...).resolves` returns a Promise. If it is not awaited, the test runner may complete the test before the asynchronous assertion is actually checked, causing a false positive.
Browsers and Events
7 questions in this domain
- Question 7Single Choice
When writing a JavaScript unit test, why is it essential to include a test assertion in the test case?
- ATo make the test execute faster
- BTo validate that the actual outcome matches the expected outcomeCorrect answer
- CTo remove all external dependencies
- DTo generate a coverage report
Why this is the answer
Assertions are the heart of a test; they verify that the result of the code equals the expected result. Without assertions, a test runs code without confirming correctness.
- Question 8Multiple Select
Which of the following statements correctly describe how mocks and stubs should be used to isolate a unit under test?
- AThey simulate external dependenciesCorrect answer
- BThey help test only the target function's logicCorrect answer
- CThey replace the unit under test with a real service
- DThey prevent network or complex module callsCorrect answer
Why this is the answer
Mocks and stubs simulate dependencies to isolate the code under test. They allow tests to validate only the target logic without making real network calls or invoking complex modules.
- Question 9Interactive
The code block below shows a simple test. Which step of the AAA pattern is missing from this test?
- AArrange step
- BAct step
- CAssert stepCorrect answer
- DCleanup hook
Why this is the answer
The test sets up values and then calls the function, but it never checks that the result is correct. The missing step is Assert.
- Question 10Single Choice
If a unit test fails because a real network request returns an error, why is it considered an integration test rather than a unit test?
- AA failed unit test
- BA failed integration testCorrect answer
- CA failed load test
- DA failed static analysis
Why this is the answer
A true unit test isolates dependencies. If a real network call causes failure, the test is exercising external behavior, making it an integration test.
- Question 11Multiple Select
Which of the following are standard lifecycle hooks that a test runner can execute before and after tests?
- AbeforeEachCorrect answer
- BbeforeAllCorrect answer
- CafterEachCorrect answer
- DbeforeTest
Why this is the answer
Common lifecycle hooks include beforeEach, afterEach, beforeAll, and afterAll. beforeTest is not a standard test runner hook.
- Question 12Interactive
Given the code block, what is the outcome of the toBe assertion and what does that outcome indicate?
- APass, because both objects have the same property
- BFail, because toBe compares object references, not structureCorrect answer
- CPass, because toBe performs a deep comparison
- DFail, because toBe can only compare primitive values
Why this is the answer
toBe checks referential equality, meaning it verifies that both variables point to the same object. Two separate objects with identical properties will fail this matcher; toEqual should be used instead.
- Question 13Single Choice
When comparing two separate objects with identical properties in JavaScript, which matcher should you use to perform a deep structural equality check?
- AtoBe
- BtoEqualCorrect answer
- CtoContain
- DtoThrow
Why this is the answer
toEqual performs a deep equality comparison and verifies that two objects have the same structure and values. toBe checks reference equality.
Debugging and Error Handling
3 questions in this domain
- Question 14Single Choice
According to the playbook, what is the core principle that separates the unit under test from dependencies such as API calls or complex modules?
- AIsolation and mockingCorrect answer
- BTest orchestration
- CEnvironment simulation
- DReporting and output
Why this is the answer
Isolation and mocking is the core unit testing principle that separates the unit under test from its dependencies. Using mocks or stubs simulates external behavior so the test validates only the logic of the target function.
- Question 15Multiple Select
According to the playbook, which two scenarios are identified as common testing traps?
- AConfusing `toBe` with `toEqual` for objects and arraysCorrect answer
- BUsing `toBe()` to check if an array contains an element instead of `toContain()`Correct answer
- CUsing coverage metrics to identify untested code
- DUsing mocks to simulate external dependencies
Why this is the answer
The playbook warns against confusing `toBe` with `toEqual` for objects and arrays, and against using `toBe()` to check whether an array contains an element instead of `toContain()`. It also warns about forgetting to await async assertions and over-relying on coverage metrics, but using coverage to find untested code and using mocks as intended are not traps.
- Question 16Interactive
Review the code block. What purpose does the `beforeEach` hook serve in this test suite?
- AIt sets up a clean slate before each testCorrect answer
- BIt runs only once before all tests
- CIt asserts the expected test output
- DIt discovers test files in the project
Why this is the answer
The `beforeEach` hook is a lifecycle hook that executes before each test. It is used to set up a clean slate, such as resetting a database mock, which prevents side effects from causing flaky tests.
Objects, Functions, and Classes
10 questions in this domain
- Question 17Single Choice
What is the primary purpose of using mocks or stubs in unit testing?
- ATo make the test run faster by skipping assertions
- BTo isolate the unit under test by simulating external dependenciesCorrect answer
- CTo permanently replace the unit being tested with a fake version
- DTo automatically increase test line coverage
Why this is the answer
Mocks and stubs simulate external dependencies so the test validates only the logic of the unit under test without relying on real services.
- Question 18Multiple Select
Which statements about test coverage are true?
- AHigh coverage guarantees the logic is correct.
- BCoverage indicates which lines of code were executed during the test suite.Correct answer
- CCoverage can help identify code that has not been tested.Correct answer
- D100% line coverage means the application has no bugs.
Why this is the answer
Coverage shows which lines were executed and can reveal untested code, but it does not prove logical correctness or absence of bugs.
- Question 19Interactive
In the test code shown, what is the purpose of the assertion line?
- AIt executes the function once.
- BIt validates that the actual result equals the expected result.Correct answer
- CIt creates a mock for the add function.
- DIt groups the test into a suite.
Why this is the answer
The expect function receives the actual value from add(1,2), and toBe compares it to the expected value 3, confirming correctness.
- Question 20Single Choice
In the Arrange, Act, Assert pattern, what does the 'Act' step do?
- ASets up the environment and test data
- BExecutes the function under testCorrect answer
- CVerifies that the result matches the expectation
- DCleans up after the test finishes
Why this is the answer
Arrange sets up state, Act executes the function being tested, and Assert verifies that the outcome matches the expected result.
- Question 21Multiple Select
Which of the following are common traps to avoid when writing unit tests?
- AUsing a real API service in a unit test and blaming network errors for failuresCorrect answer
- BTesting only happy path scenariosCorrect answer
- CIgnoring edge cases like null and undefined inputsCorrect answer
- DRunning the test suite with 100% line coverage
Why this is the answer
Unit tests should isolate dependencies, cover edge cases, and not rely only on happy paths. High coverage alone is not a trap.
- Question 22Interactive
What is the purpose of the lifecycle hook shown in the code block?
- AIt runs before each test, giving the tests a clean state.Correct answer
- BIt replaces the assertion library for the suite.
- CIt generates the coverage report for the test run.
- DIt mocks every network request in the tests.
Why this is the answer
Lifecycle hooks like beforeEach run before every test, allowing developers to reset state and prevent side effects between tests.
- Question 23Single Choice
What does the reporting and output feature of a test runner do?
- ATransforms raw execution data into human-readable or machine-readable formatsCorrect answer
- BProvides global objects like window and document
- CRuns test files in random order
- DResets the database before each test
Why this is the answer
Reporting and output convert raw test results into CLI summaries, JUnit XML, or coverage reports for developers and CI systems.
- Question 24Multiple Select
Which elements are commonly provided by a test runner to simulate the environment where code will run?
- AA global window object for DOM-related testsCorrect answer
- BA Node.js-like environment for backend logicCorrect answer
- CA global expect function that is part of native JavaScript
- DA simulated DOM via JSDOM for tests that reference documentCorrect answer
Why this is the answer
Runners simulate environments with window, Node.js-like globals, or JSDOM. The expect function is runner-injected, not native JavaScript.
- Question 25Interactive
Why is the asynchronous assertion in this code awaited?
- ATo speed up the test execution
- BTo ensure the assertion waits for the Promise to settle before being evaluatedCorrect answer
- CTo transform the test into a synchronous function
- DTo avoid needing a mock for fetchData
Why this is the answer
Await pauses evaluation until the Promise settles, so the assertion does not run too early and produce a false pass.
- Question 26Single Choice
Which matcher should be used to check that an array contains a specific element?
- AtoBe
- BtoEqual
- CtoContainCorrect answer
- DtoThrow
Why this is the answer
toContain is the matcher used to verify that an array or collection contains a specific element.
Server-side JavaScript
4 questions in this domain
- Question 27Single Choice
What is the primary role of a test assertion in a unit test?
- ADiscover test files automatically before execution.
- BValidate that the actual result matches the expected result.Correct answer
- CSimulate dependencies such as network calls.
- DGroup related tests into a logical suite.
Why this is the answer
Assertions are the core validation mechanism of a test. They compare the actual outcome with the expected outcome; without them a test only executes code without proving correctness.
- Question 28Multiple Select
Which of the following are common traps to avoid when writing JavaScript unit tests? (Select all that apply.)
- AIncluding a real service dependency in a unit test and blaming a network failure on the unit test.Correct answer
- BOnly testing happy-path scenarios and ignoring null or thrown-error inputs.Correct answer
- CTreating 100% line coverage as proof that the logic is correct.Correct answer
- DUsing beforeEach to reset mocks before each test.
Why this is the answer
All of the first three are warned against in the playbook: real dependencies can turn a unit test into an integration test, ignoring edge cases hides bugs, and coverage does not prove logical correctness. Resetting mocks in beforeEach is a recommended practice, not a trap.
- Question 29Interactive
Inspect the test snippet in the code block. Why does the test fail even though the object literals appear identical?
- AThe test should use a mock to simulate the object.
- B.toBe() checks referential equality, and each object literal has a different memory reference.Correct answer
- CThe assertion needs to be awaited with await expect(...).resolves.
- D.toBe() should be replaced with .toContain() for this comparison.
Why this is the answer
.toBe() performs referential equality in JavaScript. Each { foo: 'bar' } literal creates a new object in a different memory location, so the assertion fails. Use .toEqual() for deep structural equality.
- Question 30Single Choice
In the Arrange, Act, Assert pattern, what does the Arrange step do?
- AIt executes the function being tested.
- BIt verifies that the result equals the expected value.
- CIt sets up the necessary environment and data before the test.Correct answer
- DIt cleans up mocks after the test finishes.
Why this is the answer
The AAA standard is Arrange, Act, Assert. Arrange sets up the environment, fixtures, and data; Act invokes the logic under test; Assert checks the outcome.
Testing
3 questions in this domain
- Question 31Single Choice
What does the `toBe` matcher verify when comparing two values in a typical JavaScript test?
- AReferential equality between two valuesCorrect answer
- BDeep structural equality between objects
- CWhether an array includes an element
- DWhether a function throws an error
Why this is the answer
`toBe` uses referential equality, so two variables must point to the same object in memory. Use `toEqual` when comparing object structures.
- Question 32Multiple Select
According to the playbook, which of the following are common testing traps that developers should avoid when writing JavaScript tests?
- AUsing `toBe` with objects instead of `toEqual`Correct answer
- BForgetting to `await` asynchronous assertionsCorrect answer
- CAssuming 100% coverage proves correctnessCorrect answer
- DUsing `toContain` to check array membership
Why this is the answer
The correct choices are common traps: confusing `toBe` with `toEqual`, failing to `await` async assertions, and trusting coverage as proof of correctness. Using `toContain` for arrays is a correct practice.
- Question 33Interactive
What matcher should be inserted where the question marks appear in the code block to verify that the array contains the value 3?
- A`toBe`
- B`toEqual`
- C`toContain`Correct answer
- D`toThrow`
Why this is the answer
Use `.toContain()` to verify that an array includes a specific element. `toBe` checks reference equality, `toEqual` checks structural equality, and `toThrow` checks for exceptions.
Variables, Types, and Collections
10 questions in this domain
- Question 34Single Choice
What is the primary purpose of assertions in a unit test?
- ATo validate that the actual output matches the expected outputCorrect answer
- BTo execute the function under test
- CTo simulate external dependencies
- DTo organize test cases into suites
Why this is the answer
Assertions compare actual outcomes to expected outcomes; without them, a test runs but does not verify correctness.
- Question 35Multiple Select
Which dependencies should typically be mocked or stubbed when writing a unit test? Select all that apply.
- AAn external API callCorrect answer
- BA complex internal module that is not the targetCorrect answer
- CThe function being tested itself
- DA simple pure arithmetic helper
Why this is the answer
Mocks simulate external behaviors and isolate the unit from real dependencies. The target function and simple pure helpers do not need mocking.
- Question 36Interactive
Review the code block. Which matcher should replace the blank to assert that two variables refer to the same object?
- AtoBeCorrect answer
- BtoEqual
- CtoContain
- DtoThrow
Why this is the answer
toBe checks referential equality and passes when variables point to the same memory address. toEqual would check deep structure, not identity.
- Question 37Single Choice
What does test coverage measure in a JavaScript project?
- AThe percentage of codebase lines executed during testsCorrect answer
- BThe number of test assertions that pass
- CThe logical correctness of the test suite
- DThe speed at which tests run
Why this is the answer
Coverage shows which lines were touched during test runs. It is not a guarantee of correctness.
- Question 38Multiple Select
Which of the following are lifecycle hooks provided by test runners? Select all that apply.
- AbeforeEachCorrect answer
- BafterEachCorrect answer
- CbeforeAllCorrect answer
- Dsetup
Why this is the answer
Jest-style runners provide beforeEach, afterEach, beforeAll, and afterAll. setup is not one of the lifecycle hooks described.
- Question 39Interactive
Consider the asynchronous test in the code block. Why might the test incorrectly pass even when the Promise rejects?
- AThe test is missing an await before expectCorrect answer
- BThe matcher toBe is wrong for promises
- CThe test lacks an assertion library
- DThe runner executes tests in the wrong order
Why this is the answer
Async assertions must be awaited. Without await, the test completes before the Promise is evaluated, allowing a rejected promise to go unnoticed.
- Question 40Single Choice
What is a test suite?
- AA collection of related test casesCorrect answer
- BA single unit test scenario
- CA simulated browser environment
- DA coverage report generated after tests run
Why this is the answer
A test case is one scenario; a test suite is a structured collection of related test cases.
- Question 41Multiple Select
Which statements about test coverage are correct? Select all that apply.
- ACoverage indicates the percentage of code executed during the test runCorrect answer
- BHigh coverage does not guarantee logical correctnessCorrect answer
- CCoverage reports identify untested codeCorrect answer
- D100% coverage proves all edge cases have been tested
Why this is the answer
Coverage only shows executed lines. It is useful for finding untested code but is not proof of correctness or edge-case completeness.
- Question 42Interactive
The code block contains a test organized with the AAA pattern. Which stage is represented by the line that calls the function and stores the result?
- AArrange
- BActCorrect answer
- CAssert
- DMock
Why this is the answer
The function call is the Act step. Environment setup happens in Arrange, and result verification happens in Assert.
- Question 43Single Choice
How does a test runner handle a failing test in a suite?
- AIt continues running other tests to provide comprehensive error reportingCorrect answer
- BIt stops the entire suite immediately
- CIt deletes the failing test from the report
- DIt automatically patches the failing test
Why this is the answer
A runner keeps executing the suite so all failures can be reported at once, rather than stopping at the first failure.
Frequently asked questions
Everything about the Salesforce Certified JavaScript Developer I question bank and how to use it.