question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

typescript unit test with import statement fails

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ochemeryscommented, 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.

0reactions
ochemeryscommented, Dec 15, 2018

temporary workaround is here

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Cannot use import statement outside a module ...
This is a React, Typescript, Webpack project. I am trying to test a module. But Jest won't transform the module to plain javascript...
Read more >
jest cannot use import statement outside a module - You.com
I'm trying to set up unit testing in Vue using Jest. However, I'm getting this error: Test suite failed to run ... SyntaxError:...
Read more >
Cannot use import statement outside a module [React ...
In this article, we talked about the SyntaxError: Cannot use import statement outside a module error in TypeScript and JavaScript. This error ...
Read more >
BabylonJS Typescript imports fail for MochaJS specs - Bugs
SyntaxError: Cannot use import statement outside a module. Tests are running with a dedicated tsconfig.json and a .mocharc.js config. This ...
Read more >
TypeScript Jest: Cannot use import statement outside module
The TypeScript jest error "Cannot use import statement outside module" occurs when we misconfigure jest in a TypeScript project and run test files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found