@@ -105,39 +105,54 @@ set -e 1
105105
106106# Install vitest and testing util
107107cd /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
109109mkdir -p /home/damner/code/.labtests
110110
111111# Move test file
112112mv $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
117120cat > /home/damner/code/.labtests/config.js << EOF
118121import { defineConfig } from 'vite'
122+
123+ // https://vitejs.dev/config/
119124export 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})
125132EOF
126133
127134# process.js file
128135cat > /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') )
131138const answers = payload.testResults[0].assertionResults.map(test => test.status === 'passed')
139+
132140fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(answers))
133141EOF
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
143158Let'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
178193describe (' 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