@ayana/test
Testing libraries and config for Ayana projects
Last updated 10 months ago by akio .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install @ayana/test 
SYNC missed versions from official npm registry.

@ayana/test NPM Discord XO code style

Installation

With NPM

npm i @ayana/test typescript --save-dev

With Yarn

yarn add @ayana/test typescript --dev

Basic Usage

  1. Add the following scripts to your package.json:

    {
        "name": "your-project",
        ...
        "scripts": {
    		"test": "ayt test",
    		"cover": "ayt coverage"
        }
    }
    

    By Ayana convention the code lives in the src directory if it's an application and in the lib directory if it's a library. Files outside of these directories will be ignored.

  2. Exclude .spec.ts files in your tsconfig.json (If you are using ayc from @ayana/ts this step can be skipped):

    {
        ...
        "exclude": [
    	    "./lib/**/*.spec.ts"
        ]
    }
    
  3. Create .spec.ts files named after the file you want to test next to the file you want to test:

    Say your file lives under lib/hello/Hello.ts, then you would create a file for the tests at lib/hello/Hello.spec.ts.

    import '@ayana/test';
    
    autoDescribe(function() { // autoDescribe will take the path after the lib or src folder and use it as the descriptor
        describe('#constructor', function() {
    	    it('should do a thing', function() {
    
    	    });
        });
    });
    

    Tests are written with mocha. In addition there's also the global variables sinon and expect (powered by unexpected) available for usage in tests.

Testing with @ayana/di

When using the import @ayana/test/di this package adds another global variable called TestInjector. Using this you can easily write Unit-Tests for components and the TestInjector will automatically create a sinon stub for all required components.

Tested.spec.ts:

import '@ayana/test/di';

import { Tested } from './Tested';

import { SomeDependency } from './SomeDependency';

autoDescribe(function() {
	describe('#doSomething', function() {
		it('should do a thing', function() {
			const injector = TestInjector.test(Tested);
			const tested = injector.getTested();

			tested.doSomething();

			expect(
				injector.getStub(SomeDependency).someMethod.callCount,
				'to be',
				1
			);
		});
	});
});

Tested.ts:

import { Inject } from '@ayana/di';

import { SomeDependency } from './SomeDependency';

export class Tested {
	constructor(@Inject() private someDependency: SomeDependency) {}

	doSomething() {
		this.someDependency.someMethod();
	}
}

SomeDependency.ts:

export class SomeDependency {
	someMethod() {
		console.log('Does Something');
	}
}

Troubleshooting

I'm getting errors because typings could not be found but the regular tsc command works

This might be caused by you having custom .d.ts files in your project. ts-node apperantly resolves custom .d.ts files differently than tsc does.

You can find a way to fix this in the TS-Node README. You want to set the baseUrl and the paths array in the compilerOptions in your tsconfig.json like it's mentioned there.

I'm getting errors about experimental decorator support but I have it enabled in my tsconfig.json

If you get an error like this:

Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning. ts(1219)

It is because you excluded the .spec.ts in your main tsconfig.json like mentioned above so that config doesn't apply to .spec.ts files anymore. If you need to use decorators in your tests, then you currently have to create a second file for the actual build (tsconfig.prod.json is recommended) and move the exclude property for the .spec.ts files there:

{
	"extends": "./tsconfig.json",
	"exclude": [
		"./lib/**/*.spec.ts"
	]
}

Also you need to update your build command to tsc -p tsconfig.prod.json. Running tsc should work just fine, however the output directory will also contain the compiled files of the tests which you might not want in your npm package or deployed application.

Current Tags

  • 2.1.0                                ...           latest (10 months ago)

5 Versions

  • 2.1.0                                ...           10 months ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 5
Last Month 10
Dependencies (8)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |