What's the correct way to import and use angular-mocks 'module' with the TS2.0 @types/angular-mocks to prevent TS errors

639 views
Skip to first unread message

james robb

unread,
Jan 26, 2017, 6:05:14 AM1/26/17
to Angular and AngularJS discussion
I've switched an angular 1 typescript project over to TS 2.0 and am using @types/angular-mocks for the type definitions. As it stands, i'm unable to work out how to actually import and use angular-mocks 'module', without getting TS errors.

All three of these ways actually work (as in, my tests run without any errors, after being transpiled and loaded by systemjs), but they all result in TS errors

1)

import {module} from 'angular-mocks';

beforeEach(module('blah'));


systemjs config meta:

meta: {
angular: {
format: 'global',
exports: 'angular'
},
'angular-mocks': {
format: 'global',
deps: ['angular']
}
}


TS error:

error TS2305: Module '"/path/to/node_modules/@types/angular-mocks/index"' has no exported member 'module'.


2)

import * as angular from 'angular-mocks';

beforeEach(angular.mock.module('blah'));


systemjs config meta:

meta: {
angular: {
format: 'global',
exports: 'angular'
},
'angular-mocks': {
format: 'global',
exports: 'angular',
deps: ['angular']
}
}


TS error:

error TS2339: Property 'mock' does not exist on type 'typeof "/path/to/node_modules/@types/angular...'.


3)

import 'angular-mocks';

beforeEach(module('blah'));



systemjs config meta:

meta: {
angular: {
format: 'global',
exports: 'angular'
},
'angular-mocks': {
format: 'global',
deps: ['angular']
}
}


TS Error:

error TS2304: Cannot find name 'module'.


------------------------------------------------------------------------

So i'm wondering what's the correct way to do this to prevent TS errors?

Cheers.

jd6...@gmail.com

unread,
Oct 4, 2017, 9:32:46 PM10/4/17
to Angular and AngularJS discussion
Whatever strategy you use, I strongly recommend you use the full path to bootstrap modules, angular.mock.module

just using the module variable (example 1) will cause collisions if you every have to install @types/node (or if a dependency ever installs it!)

FWIW, angular.mock.module works for me if I import angular like so:

import angular from 'angular';

But that might break for your build. I recommend keeping the import *, but also import angular-mocks:

import * as angular from 'angular';
import 'angular-mocks';

Hope it helps.

Reply all
Reply to author
Forward
0 new messages