Unit testing a class that requires dependencies

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

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

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


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

I have it included.

This is what the test in the plugin does

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,
} from '../src/aurelia-validation';
import * as assert from 'assert';

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

I am doing something very similar

1 Like

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

1 Like

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

1 Like