Skip to content

Async #7486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Async #7486

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ export function useConstructRunCode({
* This function is used to run the code in the editor
*/
const runCode = useCallback(
(studentCode: string, editorView: EditorView | null) => {
async (studentCode: string, editorView: EditorView | null) => {
if (!tasks) {
console.error('tasks are missing in useRunCode')
return
@@ -87,7 +87,7 @@ export function useConstructRunCode({
return
}

const testResults = generateAndRunTestSuite({
const testResults = await generateAndRunTestSuite({
studentCode,
tasks,
config,
Original file line number Diff line number Diff line change
@@ -6,9 +6,13 @@ import { generateExpects } from './generateExpects'
import { TestRunnerOptions } from '@/components/bootcamp/types/TestRunner'
import { filteredStdLibFunctions } from '@/interpreter/stdlib'

/**
This is of type TestCallback
*/
export function execProjectTestAsync(
project: Project,
testData: TaskTest,
options: TestRunnerOptions
) {
return Promise.resolve(execProjectTest(project, testData, options))
}
export function execProjectTest(
Project: Project,
testData: TaskTest,
Original file line number Diff line number Diff line change
@@ -2,42 +2,46 @@ import { describe } from '..'
import exerciseMap, {
type Project,
} from '@/components/bootcamp/SolveExercisePage/utils/exerciseMap'
import { execProjectTest } from './execProjectTest'
import { execProjectTest, execProjectTestAsync } from './execProjectTest'
import { execGenericTest } from './execGenericTest'
import { type TestRunnerOptions } from '@/components/bootcamp/types/TestRunner'

export default (options: TestRunnerOptions) => {
return describe(options.config.title, (test) => {
return describe(options.config.title, async (test) => {
let project: Project | null = null
if (options.config.projectType) {
project = exerciseMap.get(options.config.projectType)
}
options.tasks.map((taskData) => {
taskData.tests.map((testData) => {
test(testData.name, () => {
let result: ReturnType<TestCallback>
if (project) {
result = execProjectTest(project, testData, options)
} else {
result = execGenericTest(testData, options)
}
await mapTasks(test, options, project)
})
}

const { frames } = result
let { expects } = result
const mapTasks = async (test, options, project) => {
options.tasks.map((taskData) => {
taskData.tests.map((testData) => {
test(testData.name, async () => {
let result: ReturnType<TestCallback>
if (project) {
result = await execProjectTestAsync(project, testData, options)
} else {
result = execGenericTest(testData, options)
}

// make sure a test is only successful if all frames are successful
expects.push({
actual: 'running',
errorHtml: 'Your code has an error in it.',
name: 'Code passes',
expected: true,
pass: frames.every((frame) => frame.status === 'SUCCESS'),
slug: 'code-passes',
testsType: 'state',
})
const { frames } = result
let { expects } = result

return { ...result, expects }
// make sure a test is only successful if all frames are successful
expects.push({
actual: 'running',
errorHtml: 'Your code has an error in it.',
name: 'Code passes',
expected: true,
pass: frames.every((frame) => frame.status === 'SUCCESS'),
slug: 'code-passes',
testsType: 'state',
})

return { ...result, expects }
})
})
})
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export function describe(
export async function describe(
suiteName: string,
callback: (
test: (testName: string, testCallback: TestCallback) => void
) => void
): TestSuiteResult<NewTestResult> {
) => Promise<void>
): Promise<TestSuiteResult<NewTestResult>> {
// test results are collected in one shared array
const tests: NewTestResult[] = []

const test = createTestCallback(tests)

// invokes the test callbacks, which mutate the tests array by pushing the new results to it
callback(test)
await callback(test)

return {
suiteName: suiteName,
@@ -20,8 +20,9 @@ export function describe(
}

function createTestCallback(tests: NewTestResult[]) {
return function (testName: string, testCallback: TestCallback): void {
const testCallbackResult = testCallback()
return async function (testName: string, testCallback: TestCallback): void {
const testCallbackResult = await testCallback()
console.log(testCallbackResult)
tests.push({
// we need testIndex, so we can retrieve quickly the test that we are currently inspecting/working on
testIndex: tests.length,
Loading
Oops, something went wrong.