# Unit testing a class that requires dependencies

**URL:** <https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217>\
**Category:** Help Requests\
**Created:** [May 14, 2018, 1:35pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217 "2018-05-14T13:35:07Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![timfish](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/timfish/32/1566_2.png) [@timfish](https://discourse.aurelia.io/u/timfish)\
**Post date:** [May 22, 2018, 12:47pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/22 "2018-05-22T12:47:06Z")

</div>

@elitemike You’re actually running your tests in node.js context or in a browser instance using something like `karma-chrome-launcher`?

---

<div class="post-metadata">

**Author:** ![elitemike](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitemike/32/690_2.png) [@elitemike](https://discourse.aurelia.io/u/elitemike)\
**Post date:** [May 22, 2018, 12:52pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/23 "2018-05-22T12:52:24Z")

</div>

I’ve tried both. I believe I can get the Aurelia context working, but that isn’t doing me much good.

I’m getting these errors  
`  
Unable to parse accessor function:  
function © {  
/\* istanbul ignore next \*/cov\_eexd7hncx.f[9]++;cov\_eexd7hncx.s[44]++;return c.value;  
}

at getAccessorExpression (node\_modules/aurelia-validation/dist/commonjs/property-accessor-parser.js:32:15)  
at PropertyAccessorParser.Object..PropertyAccessorParser.parse (node\_modules/aurelia-validation/dist/commonjs/property-accessor-parser.js:13:28)  
at FluentEnsure.Object..FluentEnsure.ensure (node\_modules/aurelia-validation/dist/commonjs/implementation/validation-rules.js:332:42)  
at Function.Object..ValidationRules.ensure (node\_modules/aurelia-validation/dist/commonjs/implementation/validation-rules.js:399:58)  
`

while trying to do this

```auto
        ValidationRules.ensure((c: StringControlConfiguration) => c.value)           
            .required().when(() => control.required && control.isVisible && !control.isDisabled).withMessage(control.errorMessage !== null ? control.errorMessage : control.label + ' is required')
            .then()   
            .maxLength(control.maxLength).when(() => control.maxLength !== null)
            .matches(control.regEx).when(() => control.regEx !== null)        
            .minLength(control.minLength).when(() => control.minLength !== null)            
            .on(control);

        control.controlFactory.validationController.addObject(control);      

```

---

<div class="post-metadata">

**Author:** ![timfish](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/timfish/32/1566_2.png) [@timfish](https://discourse.aurelia.io/u/timfish)\
**Post date:** [May 22, 2018, 12:53pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/24 "2018-05-22T12:53:35Z")

</div>

To run the tests on node do you have to use `aurelia-pal-nodejs`?

---

<div class="post-metadata">

**Author:** ![elitemike](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/elitemike/32/690_2.png) [@elitemike](https://discourse.aurelia.io/u/elitemike)\
**Post date:** [May 22, 2018, 12:57pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/25 "2018-05-22T12:57:26Z")

</div>

I have it included.

This is what the test in the plugin does

```auto
import 'aurelia-polyfills';
import { initialize } from 'aurelia-pal-nodejs';
import { Container } from 'aurelia-dependency-injection';
import { configure as configureBindingLanguage } from 'aurelia-templating-binding';
import {
  configure as configureValidation,
  ValidationRules,
  Validator
} from '../src/aurelia-validation';
import * as assert from 'assert';

initialize();
const container = new Container();
configureBindingLanguage({ container });
configureValidation({ container });

```

I am doing something very similar

---

<div class="post-metadata">

**Author:** ![saqibn](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@saqibn](https://discourse.aurelia.io/u/saqibn)\
**Post date:** [February 8, 2020, 2:39pm UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/26 "2020-02-08T14:39:40Z")

</div>

The issue is with the regex in getAccesorExpression in aurelia-validation.js,Replace the regex used in parsing the “classic” function with:  
`/^function\s*\([$_\w\d]+\)\s*\{(?:\s*"use strict";)?(?:[$_\s\w\d\/\*.['"\]+;]+)?\s*return\s+[$_\w\d]+\.([$_\w\d]+)\s*;?\s*\}$/`

alternatively u can specify a single property in ValidationRules like  
ValidationRules.ensure((this.message)).required()

---

<div class="post-metadata">

**Author:** ![saqibn](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@saqibn](https://discourse.aurelia.io/u/saqibn)\
**Post date:** [February 9, 2020, 7:33am UTC](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217/27 "2020-02-09T07:33:46Z")

</div>

or add  
/\* istanbul ignore next \*/  
ValidationRules.ensure((c: StringControlConfiguration) =\> c.value)

[Previous page](https://discourse.aurelia.io/t/unit-testing-a-class-that-requires-dependencies/1217.md?page=1)
