diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index f385cfeb42..21901b287a 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -754,6 +754,19 @@ mod in_operator { assert_eq!(forward(&mut engine, "bar.b"), "\"b\""); } + #[test] + fn should_type_error_when_new_is_not_constructor() { + let mut engine = Context::new(); + + let scenario = r#" + const a = ""; + new a(); + "#; + + let result = forward(&mut engine, scenario); + assert_eq!(result, "Uncaught \"TypeError\": \"a is not a constructor\""); + } + #[test] fn new_instance_should_point_to_prototype() { // A new instance should point to a prototype object created with the constructor function diff --git a/boa/src/syntax/ast/node/new/mod.rs b/boa/src/syntax/ast/node/new/mod.rs index eeb47dbed2..4a1d57a61f 100644 --- a/boa/src/syntax/ast/node/new/mod.rs +++ b/boa/src/syntax/ast/node/new/mod.rs @@ -55,7 +55,8 @@ impl Executable for New { match func_object { Value::Object(ref object) => object.construct(&v_args, interpreter), - _ => Ok(Value::undefined()), + _ => interpreter + .throw_type_error(format!("{} is not a constructor", self.expr().to_string(),)), } } }