fix: indentation error reporting (#62182)

This commit is contained in:
Oliver Eyton-Williams
2025-09-16 16:36:42 +02:00
committed by GitHub
parent dc0d39c548
commit b80b849058
2 changed files with 52 additions and 9 deletions
@@ -184,7 +184,7 @@ function* buildChallengeData(challengeData, options) {
} }
} }
function* executeTests(testRunner, tests, testTimeout = 5000) { export function* executeTests(testRunner, tests, testTimeout = 5000) {
const testStrings = tests.map(test => test.testString); const testStrings = tests.map(test => test.testString);
const rawResults = yield call(testRunner, testStrings, testTimeout); const rawResults = yield call(testRunner, testStrings, testTimeout);
@@ -216,16 +216,18 @@ function* executeTests(testRunner, tests, testTimeout = 5000) {
if (err === 'timeout') { if (err === 'timeout') {
newTest.err = 'Test timed out'; newTest.err = 'Test timed out';
newTest.message = `${newTest.message} (${newTest.err})`; newTest.message = `${newTest.message} (${newTest.err})`;
} else if (type === 'IndentationError' || type === 'SyntaxError') { } else {
const { message, stack } = err;
newTest.err = message + '\n' + stack;
newTest.stack = stack;
}
if (type === 'IndentationError' || type === 'SyntaxError') {
const msgKey = const msgKey =
type === 'IndentationError' type === 'IndentationError'
? 'learn.indentation-error' ? 'learn.indentation-error'
: 'learn.syntax-error'; : 'learn.syntax-error';
newTest.message = `<p>${i18next.t(msgKey)}</p>`; newTest.message = `<p>${i18next.t(msgKey)}</p>`;
} else {
const { message, stack } = err;
newTest.err = message + '\n' + stack;
newTest.stack = stack;
} }
const withIndex = newTest.message.replace(/<p>/, `<p>${i + 1}. `); const withIndex = newTest.message.replace(/<p>/, `<p>${i + 1}. `);
@@ -1,6 +1,8 @@
import { expectSaga } from 'redux-saga-test-plan'; import { expectSaga } from 'redux-saga-test-plan';
import { describe, it, vi } from 'vitest'; import { describe, it, vi } from 'vitest';
import { previewChallengeSaga, executeTests } from './execute-challenge-saga';
vi.mock('redux-saga/effects', async importOriginal => { vi.mock('redux-saga/effects', async importOriginal => {
const actual = await importOriginal(); const actual = await importOriginal();
return { return {
@@ -9,6 +11,12 @@ vi.mock('redux-saga/effects', async importOriginal => {
}; };
}); });
vi.mock('i18next', async () => ({
default: {
t: key => key
}
}));
const initialState = { const initialState = {
challenge: { isBuildEnabled: true, isExecuting: false, challengeMeta: {} } challenge: { isBuildEnabled: true, isExecuting: false, challengeMeta: {} }
}; };
@@ -18,13 +26,11 @@ function reducer(state = initialState) {
return state; return state;
} }
import { previewChallengeSaga } from './execute-challenge-saga';
const challengeMounted = { type: 'challenge.challengeMounted' }; const challengeMounted = { type: 'challenge.challengeMounted' };
const previewMounted = { type: 'challenge.previewMounted' }; const previewMounted = { type: 'challenge.previewMounted' };
const resetChallenge = { type: 'challenge.resetChallenge' }; const resetChallenge = { type: 'challenge.resetChallenge' };
describe('execute-challenge-saga', () => { describe('previewChallengeSaga', () => {
it('flushes logs on challengeMounted', () => { it('flushes logs on challengeMounted', () => {
return expectSaga(previewChallengeSaga, challengeMounted) return expectSaga(previewChallengeSaga, challengeMounted)
.withReducer(reducer) .withReducer(reducer)
@@ -46,3 +52,38 @@ describe('execute-challenge-saga', () => {
.silentRun(); .silentRun();
}); });
}); });
describe('executeTests generator', () => {
it('sets a special message for IndentationErrors', () => {
const mockTestRunner = () => {
return [
{
err: {
type: 'IndentationError',
message: 'Unexpected token',
stack: '...'
}
}
];
};
const tests = [{ testString: 'assert(true);', text: 'Test 1' }];
return expectSaga(executeTests, mockTestRunner, tests)
.put({
type: 'challenge.updateConsole',
payload: '<p>1. learn.indentation-error</p>'
})
.returns([
{
err: 'Unexpected token\n...',
text: 'Test 1',
testString: 'assert(true);',
running: false,
message: '<p>learn.indentation-error</p>',
stack: '...'
}
])
.run();
});
});