Skip to content

Commit 40347ef

Browse files
feat: vue lab docs added (#22)
- added docs for vue labs as well - docs links updated in `/docs/technologies` page --------- Co-authored-by: Pranav <pranav.techiegeek@gmail.com>
1 parent d0ea5c8 commit 40347ef

5 files changed

Lines changed: 191 additions & 0 deletions

File tree

.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export default defineConfig({
9797
text: 'React.js',
9898
link: '/docs/technologies/react'
9999
},
100+
{
101+
text: 'Vue.js',
102+
link: '/docs/technologies/vue'
103+
},
100104
{
101105
text: 'Node.js',
102106
link: '/docs/technologies/node'

docs/technologies/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ Supported technologies (as visible on the left sidebar menu):
2121

2222
- [HTML/CSS](/docs/technologies/html-css)
2323
- [React](/docs/technologies/react)
24+
- [Vue](/docs/technologies/vue)
2425
- [Node.js](/docs/technologies/node)
2526
- [Python](/docs/technologies/python-pytest)
27+
- [SQL (SQLite)](/docs/technologies/sqlite)
28+
- [Go](/docs/technologies/go)
2629
- [Solidity](/docs/technologies/solidity-hardhat)
2730
- [Java](/docs/technologies/java-junit)
31+
- [AWS Labs](/docs/technologies/aws)

docs/technologies/vue.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# How to create an interactive Vue.js lab with Vitest?
2+
3+
<!--@include: ./../_components/TechnologyIntro.md-->
4+
5+
We'll divide this part into 5 sections:
6+
7+
1. Creating lab metadata
8+
2. Setting up lab defaults
9+
3. Setting up lab challenges
10+
4. Setting up test file
11+
5. Setting up evaluation script
12+
13+
## Introduction
14+
15+
This guide would assume that you already have created an interactive course from your instructor panel. If not, [go here and set it up first](https://codedamn.com/instructor/interactive-courses)
16+
17+
## Step 1 - Creating lab metadata
18+
19+
<!--@include: ./../_components/LabMetadata.md-->
20+
21+
### Lab Details
22+
23+
Lab details is the tab where you add two important things:
24+
25+
- Lab title
26+
- Lab description
27+
28+
Once both of them are filled, it would appear as following:
29+
30+
![](/images/html-css/lab-details.png)
31+
32+
Let's move to the next tab now.
33+
34+
### Lab Layout
35+
36+
<!--@include: ./../_components/LabLayout.md-->
37+
38+
## Step 2 - Lab Defaults
39+
40+
Lab defaults section include how your lab environment boots. It is one of the most important parts because a wrong default environment might confuse your students. Therefore it is important to set it up properly.
41+
42+
When a codedamn playground boots, it can setup a filesystem for user by default. You can specify what the starting files could be, by specifying a git repository and a branch name:
43+
44+
![Lab default repository](/images/vue/lab-default-repo.png)
45+
46+
**Important note:** For Vue playground, we recommend you to fork the following repository and use it as a starter template: [Vue vite playground starter - codedamn](https://github.com/codedamn-projects/vue3-vite-playground)
47+
48+
**Note:** You can setup a Vue project in other ways as well - for example with `create-vue`. However, it is highly recommended to use Vite for Vue because it sets up the project extremely fast for the user to work with.
49+
50+
You will find a `.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](/docs/concepts/cdmrc) to understand what `.cdmrc` file exactly is. Once you understand how to work with `.cdmrc` come back to this area.
51+
52+
## Step 3 - Lab challenges
53+
54+
<!--@include: ./../_components/LabChallenges.md-->
55+
56+
## Step 4 - Test file
57+
58+
Once you save the lab, you will see a button named `Edit Test File` in the `Evaluation` tab. Click on it.
59+
60+
![](/images/common/lab-edit-test.png)
61+
62+
When you click on it, a new window will open. This is a test file area.
63+
64+
You can write anything here. Whatever script you write here, can be executed from the `Test command to run section` inside the evaluation tab we were in earlier.
65+
66+
The point of having a file like this to provide you with a place where you can write your test.
67+
68+
**For Vue.js labs, you can continue with the 'Custom Template' selected by default:**
69+
70+
![](/images/vue/lab-edit-test-file.png)
71+
72+
Paste the following code in the editor as a starting point:
73+
74+
```jsx
75+
import { mount } from '@vue/test-utils'
76+
77+
test('Test 1', async () => {
78+
try {
79+
const { default: HelloWorld } = await import('../code/src/components/HelloWorld.vue')
80+
81+
const wrapper = mount(HelloWorld)
82+
83+
expect(wrapper.text()).toContain('Hello Vue.js!')
84+
} catch (e) {
85+
expect(false).toBe(true)
86+
}
87+
})
88+
```
89+
90+
Let us understand what is happening here exactly:
91+
92+
- Remember that we can code anything in this file and then execute it later. In this example, we're writing a Vue.js Vitest test script from scratch. Check out [vitest docs](https://vitest.dev) if you're new to vitest.
93+
- Remember that we will install vitest and other required utilities in the evaluation script section below. Therefore, you can try to import and use anything and everything here you want.
94+
- The rest of the code is just importing the default user code, and testing it through standard unit testing procedures.
95+
96+
![](/images/html-css/playground-tests.png)
97+
98+
- The number of `test(...)` blocks inside your `describe` suite should match the number of challenges added in the creator area.
99+
100+
- **Note:** If your number of `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.
101+
102+
- We then also add jQuery and chai for assisting with testing. Although it is not required as long as you can populate the `results` array properly.
103+
104+
This completes your evaluation script for the lab. Your lab is now almost ready for users.
105+
106+
## Step 5 - Evaluation Script
107+
108+
Evaluation script is actually what runs when the user on the codedamn playground clicks on "Run Tests" button.
109+
110+
![](/images/common/lab-run-tests.png)
111+
112+
Remember that we're using Vue Vite playground setup. This means we can assume that we already have vite installed.
113+
114+
However, we still need to setup a lot of things: `jsdom`, `vitest`, and `@vue/test-utils`. Therefore, we can write our evaluation bash script to install all of this and run our tests. Here's how the Vue vitest script looks like:
115+
116+
```sh
117+
#!/bin/bash
118+
set -e 1
119+
120+
# Assumes you are running a vue vite playground on codedamn
121+
122+
# Install vitest and testing util
123+
cd /home/damner/code
124+
bun add vitest@0.29.7 jsdom@21.1.1 @vue/test-utils@2.3.2 --dev
125+
mkdir -p /home/damner/code/.labtests
126+
127+
# Move test file
128+
mv $TEST_FILE_NAME /home/damner/code/.labtests/all.test.js
129+
130+
# vitest config file
131+
cat > /home/damner/code/.labtests/config.js << EOF
132+
import { defineConfig } from 'vite'
133+
import Vue from '@vitejs/plugin-vue'
134+
135+
export default defineConfig({
136+
plugins: [
137+
Vue(),
138+
],
139+
test: {
140+
globals: true,
141+
environment: 'jsdom',
142+
},
143+
})
144+
EOF
145+
146+
# process.js file
147+
cat > /home/damner/code/.labtests/process.js << EOF
148+
import fs from 'fs'
149+
const payload = JSON.parse(fs.readFileSync('./.labtests/payload.json'));
150+
const answers = payload.testResults[0].assertionResults.map(test => test.status === 'passed')
151+
fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(answers))
152+
fs.rmdirSync('./.labtests', { recursive: true, force: true })
153+
EOF
154+
155+
# run test
156+
bun vitest run --config=/home/damner/code/.labtests/config.js --threads=false --reporter=json --outputFile=/home/damner/code/.labtests/payload.json || true
157+
158+
# Write results to UNIT_TEST_OUTPUT_FILE to communicate to frontend
159+
node /home/damner/code/.labtests/process.js
160+
```
161+
162+
You might need to have a little understanding of bash scripting. Let us understand how the evaluation bash script is working:
163+
164+
- With `set -e 1` we effectively say that the script should stop on any errors
165+
- We then navigate to user default directory `/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 vue setup (like `create-vue`), you might have to install `vite` as well.
166+
- You can install additional packages here if you want. They would only be installed the first time user runs the test. On subsequent runs, it can reuse the installed packages (since they are not removed at the end of testing)
167+
- Then we create a `.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.
168+
- We move the test file you wrote earlier (in last step) to `/home/damner/code/.labtests/all.test.js`.
169+
- We then also create a custom vite config file as `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](https://vitest.dev/config/#globals).
170+
- We then create a `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`
171+
- For example, once the test run succeeds, and if you write `[true,false,true,true]` inside `$UNIT_TEST_OUTPUT_FILE`, it would reflect as PASS, FAIL, PASS for 3 challenges available inside codedamn playground UI (as shown below)
172+
173+
![](/images/html-css/playground-tests-2.png)
174+
175+
- Then we run the actual test using `bun vitest run` command, specifying the output as JSON (read by `process.js`) and in a single thread (as we want ordered results).
176+
177+
- Finally we run the `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.
178+
179+
**Note:** You can setup a full testing environment in this block of evaluation script (installing more packages, etc. if you want). However, your bash script test file will be timed out **after 30 seconds**. Therefore, make sure, all of your testing can happen within 30 seconds.
180+
181+
## Setup Verified Solution (Recommended)
182+
183+
<!--@include: ./../_components/LabVerifiedSolution.md-->
186 KB
Loading
68.3 KB
Loading

0 commit comments

Comments
 (0)