Skip to content

typescript unit test with import statement fails #4172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ochemerys opened this issue Nov 25, 2018 · 7 comments
Closed

typescript unit test with import statement fails #4172

ochemerys opened this issue Nov 25, 2018 · 7 comments

Comments

@ochemerys
Copy link

ochemerys commented Nov 25, 2018

Environment
Provide version numbers for the following components (information can be retrieved by running tns info in your project folder or by inspecting the package.json of the project):
-- tns doctor returns:

  • Component nativescript has 5.0.1 version and is up to date.
  • Component tns-core-modules has 5.0.3 version and is up to date.
  • Component tns-android has 5.0.0 version and is up to date.
  • Component tns-ios has 5.0.0 version and is up to date.
    -- package file:
    --- dependencies
    "nativescript-angular": "~7.0.0",
    "nativescript-theme-core": "~1.0.4",
    "nativescript-unit-test-runner": "^0.3.4",
    "tns-core-modules": "~5.0.2",
--- devDependencies 
    "@angular/compiler-cli": "~7.0.0",
    "@nativescript/schematics": "~0.4.0",
    "@ngtools/webpack": "~7.0.0",
    "@types/chai": "^4.1.7",
    "@types/mocha": "^5.2.5",
    "chai": "4.2.0",
    "karma": "3.1.1",
    "karma-chai": "0.1.0",
    "karma-mocha": "1.3.0",
    "karma-nativescript-launcher": "0.4.0",
    "mocha": "5.2.0",
    "nativescript-dev-typescript": "~0.7.0",
    "nativescript-dev-webpack": "~0.18.0",
    "typescript": "^3.1.6"

Describe the bug
angular application typescript unit test with import statement fails with error:
ReferenceError: Can't find variable: exports

To Reproduce

  1. follow document https://docs.nativescript.org/tooling/testing.
tns test init 

and select mocha. Run test

tns test ios --emulator

Test runs successfully.

  1. add typing
npm i @types/chai --save-dev
  1. create example.spec.ts with content:
import { assert } from 'chai'

describe('Array', function () {
	describe('#indexOf()', function () {
		it('should return -1 when the value is not present', function () {
			assert.equal(-1, [1,2,3].indexOf(5));
			assert.equal(-1, [1,2,3].indexOf(0));
		});
	});
});
tns test ios --emulator

Test fails

  1. modify example.spec.ts to remove import statement:
describe('Array', function () {
	describe('#indexOf()', function () {
		it('should return -1 when the value is not present', function () {
			chai.assert.equal(-1, [1,2,3].indexOf(5));
			chai.assert.equal(-1, [1,2,3].indexOf(0));
		});
	});
});
run test 
tns test ios --emulator

Test is successful.

  1. modify example.spec.ts to add import statement:
import { AppComponent } from '../app/app.component'
describe('Array', function () {
	describe('#indexOf()', function () {
		it('should return -1 when the value is not present', function () {
			chai.assert.equal(-1, [1,2,3].indexOf(5));
			chai.assert.equal(-1, [1,2,3].indexOf(0));
		});
	});
});
tns test ios --emulator

Test fails again with error:

25 11 2018 12:33:01.031:WARN [NativeScript / 12.1 (12.1; iPhone)]: Adapter did not report total number of specs.
NativeScript / 12.1 (12.1; iPhone)  /base/src/tests/example.spec.js?60ad106197c3f92cbc2e05ab96de86321ec139ae at line 0 FAILED
        ReferenceError: Can't find variable: exports
NativeScript / 12.1 (12.1; iPhone): Executed 1 of null (1 FAILED) ERROR (0.008 secs / 0 secs)
@NickIliev
Copy link
Contributor

NickIliev commented Nov 26, 2018

@ochemerys it is possible that the assert from chai is clashing with other TypeScript declared variables.

Try craete a custom config file and remove the import { assert } from 'chai' the following:

import * as chai from "chai";
export const assert: typeof chai.assert = (<any>global).chai.assert;

like done here

@ochemerys
Copy link
Author

ochemerys commented Nov 26, 2018

Thank you for your response,
I was able to run test without import statement

describe('Array', function () {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
chai.assert.equal(-1, [1,2,3].indexOf(5));
chai.assert.equal(-1, [1,2,3].indexOf(0));
});
});
});

but when I added there import statement:
import { AppComponent } from '../app/app.component'

test fails again with the same error.

so for me problem is not chai, but import statement itself
Does it mean that I have declare every module as global?

Thank you.

@NickIliev
Copy link
Contributor

No need to import all modules as global ones - only the assert from chai is enough.

@ochemerys
Copy link
Author

Sorry I was out of my place and was not able to test. I followed your instructions and still have the same issue:
8 11 2018 20:41:32.441:WARN [NativeScript / 12.1 (12.1; iPhone)]: Adapter did not report total number of specs.
CONSOLE LOG file:///app/tns_modules/nativescript-unit-test-runner/main-view-model.js:254:20: NSUTR: completed test run.
NativeScript / 12.1 (12.1; iPhone) /base/src/tests/example.spec.js?4166a63b8fa14f25cec0cebae16daf46c08e4115 at line 0 FAILED
ReferenceError: Can't find variable: exports
NativeScript / 12.1 (12.1; iPhone): Executed 1 of null (1 FAILED) ERROR (0.005 secs / 0 secs)

@ochemerys
Copy link
Author

It is very easy to reproduce:

tns create ng-mocha-testdemo --ng
cd ng-mocha-testdemo
npm install
tns test init (and select mocha)
npm i @types/mocha --save-dev

go to tests folder
add test-config.ts with the content you provided
rename existing example.js into example.ts
add first line to example.ts
import { assert } from './test-config';

then run test

tns test ios

@ochemerys
Copy link
Author

I think the problem is in a new angular project structure
In my project with old structure tests work perfectly well, but when I generated a new structure , tests ends with posted above error.
new generated project structure structure
screen shot 2018-11-28 at 9 07 46 pm

Thank you.

@ochemerys
Copy link
Author

temporary workaround is here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants