From a080ac959bcf1fc676136afbcd2b7a2818db96b4 Mon Sep 17 00:00:00 2001 From: Juro Oravec Date: Wed, 8 Oct 2025 18:48:58 +0200 Subject: [PATCH] refactor: add tests to ensure we add component path on template errors (#1448) --- tests/test_component.py | 44 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/test_component.py b/tests/test_component.py index d9c472e0..a567c795 100644 --- a/tests/test_component.py +++ b/tests/test_component.py @@ -10,7 +10,7 @@ from typing import Any, List, Literal, NamedTuple, Optional import pytest from django.conf import settings from django.http import HttpRequest, HttpResponse -from django.template import Context, RequestContext, Template +from django.template import Context, RequestContext, Template, TemplateSyntaxError from django.template.base import TextNode from django.test import Client from django.urls import path @@ -1471,6 +1471,48 @@ class TestComponentRender: ): Root.render() + @djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR) + def test_prepends_exceptions_on_template_compile_error(self, components_settings): + @register("simple_component") + class SimpleComponent(Component): + template = "hello" + + class Other(Component): + template = """ + {% load component_tags %} + {% component "simple_component" %} + {% endif %} + """ + + with pytest.raises( + TemplateSyntaxError, + match=re.escape( + "An error occured while rendering components Other:\n" + "Invalid block tag on line 4: 'endif', expected 'endcomponent'", + ), + ): + Other.render() + + @djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR) + def test_prepends_exceptions_on_template_compile_error2(self, components_settings): + @register("simple_component") + class SimpleComponent(Component): + template = "hello" + + class Other(Component): + template = """ + {% load component_tags %} + {% component "simple_component" %} + """ + + with pytest.raises( + TemplateSyntaxError, + match=re.escape( + "An error occured while rendering components Other:\nUnclosed tag on line 3: 'component'", + ), + ): + Other.render() + @djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR) def test_pydantic_exception(self, components_settings): from pydantic import BaseModel, ValidationError