Skip to content

Commit f3f938c

Browse files
committed
chore: update node.js vitest docs
1 parent ed6b364 commit f3f938c

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

docs/technologies/node.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,39 +105,54 @@ set -e 1
105105

106106
# Install vitest and testing util
107107
cd /home/damner/code
108-
yarn add vite@3 vitest@0.22.1 --dev
108+
yarn add vitest@0.32.2 jsdom@22.1.0 @testing-library/jest-dom@5.16.5 --dev
109109
mkdir -p /home/damner/code/.labtests
110110

111111
# Move test file
112112
mv $TEST_FILE_NAME /home/damner/code/.labtests/nodecheck.test.js
113113

114114
# setup file
115+
cat > /home/damner/code/.labtests/setup.js << EOF
116+
import '@testing-library/jest-dom'
117+
EOF
115118

116119
# vitest config file
117120
cat > /home/damner/code/.labtests/config.js << EOF
118121
import { defineConfig } from 'vite'
122+
123+
// https://vitejs.dev/config/
119124
export default defineConfig({
120125
plugins: [],
121126
test: {
122-
globals: true
127+
globals: true,
128+
environment: 'jsdom',
129+
setupFiles: '/home/damner/code/.labtests/setup.js',
123130
}
124131
})
125132
EOF
126133

127134
# process.js file
128135
cat > /home/damner/code/.labtests/process.js << EOF
129-
const fs = require('fs')
130-
const payload = require('./payload.json')
136+
import fs from 'node:fs'
137+
const payload = JSON.parse(fs.readFileSync('./payload.json', 'utf8'))
131138
const answers = payload.testResults[0].assertionResults.map(test => test.status === 'passed')
139+
132140
fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(answers))
133141
EOF
134142

135-
# run test
136-
yarn vitest run --config=/home/damner/code/.labtests/config.js --threads=false --reporter=json --outputFile=/home/damner/code/.labtests/payload.json || true
143+
# package.json
144+
cat > /home/damner/code/.labtests/package.json << EOF
145+
{
146+
"type": "module"
147+
}
148+
EOF
137149

150+
# run test
151+
(yarn vitest run --config=/home/damner/code/.labtests/config.js --threads=false --reporter=json --outputFile=/home/damner/code/.labtests/payload.json || true) | tee /home/damner/code/.labtests/evaluationscript.log
138152

139153
# Write results to UNIT_TEST_OUTPUT_FILE to communicate to frontend
140-
node /home/damner/code/.labtests/process.js
154+
cd /home/damner/code/.labtests
155+
node process.js
141156
```
142157

143158
Let's understand what the above evaluation script is doing:
@@ -177,17 +192,13 @@ The moment you select the Node.js (Vitest), the following code should appear in
177192
```js
178193
describe('Test runner suite', () => {
179194
test('Variable should be exported', async () => {
180-
const userVariable = await import('/home/damner/code/index').then(
181-
(t) => t.default
182-
)
195+
const userVariable = await import('/home/damner/code/index').then(t => t.default)
183196

184197
expect(typeof userVariable === 'undefined').to.be.false
185198
})
186199

187200
test('Variable should have correct value', async () => {
188-
const userVariable = await import('/home/damner/code/index').then(
189-
(t) => t.default
190-
)
201+
const userVariable = await import('/home/damner/code/index').then(t => t.default)
191202
expect(userVariable === 'Hello World').to.be.true
192203
})
193204
})

0 commit comments

Comments
 (0)