How to create an interactive React.js lab with Vitest?
Attach to course item
button. Once that is done, click the three dots again and click on “Edit” to edit the lab.Edit
button a new page will open. On this page you need to setup instructions for lab. These instructions would be visible to the user when they’re attempting the lab. Therefore, include all the helper material, lab setup instructions here.
.cdmrc
file in the repository given to you above. It is highly recommend, at this point, that you go through the .cdmrc guide and how to use .cdmrc in playgrounds to understand what .cdmrc
file exactly is. Once you understand how to work with .cdmrc
come back to this area.
jsdom
, vitest
, and react-testing-library
. Therefore, we can write our evaluation bash script to install all of this and run our tests. Here’s how the React vitest script looks like:
set -e 1
we effectively say that the script should stop on any errors/home/damner/code
and then install the required NPM packages. Note that this assumes we already have vite
installed. If you’re using a different react setup (like create-react-app
), you might have to install vite
as well..labtests
folder inside of the /home/damner/code
user code directory. Note that .labtests
is a special folder that can be used to place your test code. This folder will not be visible in the file explorer user sees, and the files placed in this folder are not “backed up to cloud” for user./home/damner/code/.labtests/reactcheck.test.jsx
. Note that it is important to give it an extension of .test.jsx
for vitest to pick it as a JSX test file./home/damner/code/.labtests/setup.js
with just jsdom
as the import. This is because vitest can then use JSDOM to parse the DOM without browser. More information about this setup file can be found in vitest docs here.config.js
. This is because we don’t want to override your (or users’) custom vite.config.js
file if present. This file only loads jsdom
and marks the globals: true
hence importing describe
, test
, etc. automatically available without importing. More information about the configuration can be found here in vitest docs.process.js
file that can be used to process our results into a single file of boolean values. This is important because on the playground page, the way challenges work, is that they get green or red based on a JSON boolean array written inside the file in environment variable: $UNIT_TEST_OUTPUT_FILE
[true,false,true,true]
inside $UNIT_TEST_OUTPUT_FILE
, it would reflect as PASS, FAIL, PASS, PASS for 4 challenges available inside playground UI (as shown below)yarn vitest run
command, specifying the output as JSON (read by process.js
) and in a single thread (as we want ordered results).
process.js
file that writes the correct JSON boolean array on $UNIT_TEST_OUTPUT_FILE
which is then read by the playground UI and marks the lab challenges as pass or fail.
Evaluation
tab, you’ll see another section called “Custom test file”. You can use this test file to add custom code for testing user work.
Test command to run section
inside the evaluation tab we were in earlier.
The point of having a file like this to provide you with a place where you can write your evaluation script.
For React.js labs, you can use the React (Vitest) evaluation script:
test(...)
blocks inside your describe
suite must match the number of challenges added in the creator area.
test
blocks are less than challenges added back in the UI, the “extra” UI challenges would automatically stay as “false”. If you add more challenges in test file, the results would be ignored. Therefore, it is important that the results.length
is same as the number of challenges you added in the challenges UI.
results
array properly.