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):
✔ Getting NativeScript components versions information...
✔ Component nativescript has 7.0.8 version and is up to date.
✔ Component @nativescript/core has 7.0.3 version and is up to date.
✔ Component @nativescript/ios has 7.0.0 version and is up to date.
✖ Component @nativescript/android is not installed.
Describe the bug
Trying to run unit tests fails with the following error:
Error: Cannot use import statement outside a module
at Errors.failWithOptions (/usr/local/lib/node_modules/nativescript/lib/common/errors.js:157:27)
at Errors.fail (/usr/local/lib/node_modules/nativescript/lib/common/errors.js:130:21)
at DevicesService.<anonymous> (/usr/local/lib/node_modules/nativescript/lib/common/mobile/mobile-core/devices-service.js:425:30)
at Generator.throw (<anonymous>)
at rejected (/usr/local/lib/node_modules/nativescript/lib/common/mobile/mobile-core/devices-service.js:12:65)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
name: 'Exception',
errorCode: 127,
suggestCommandHelp: false,
proxyAuthenticationRequired: false,
printOnStdout: undefined
}
To Reproduce
tns create demo-test --tsc
tns test init #select mocha
tns test ios
Expected behavior
Tests to run.
Sample project
Additional context
Not only that but after you run tns test init the project is left in a buggy state and you can't even run your project as it throws the same error (even after using ns clean):
➜ demo-test ns run android
Searching for devices...
Preparing project...
File change detected. Starting incremental webpack compilation...
webpack is watching the files…
Hash: 86c6aa4a5faf27c81052
Version: webpack 4.44.2
Time: 8598ms
Built at: 09/24/2020 11:56:41 PM
Asset Size Chunks Chunk Names
bundle.js 28.2 KiB bundle [emitted] bundle
package.json 1.31 KiB [emitted]
runtime.js 77.1 KiB runtime [emitted] runtime
tns-java-classes.js 0 bytes [emitted]
vendor.js 5.98 MiB vendor [emitted] vendor
Entrypoint bundle = runtime.js vendor.js bundle.js
[../package.json] 1.19 KiB {bundle} [optional] [built]
[./ sync ^\.\/app\.(css|scss|less|sass)$] . sync nonrecursive ^\.\/app\.(css|scss|less|sass)$ 174 bytes {bundle} [built]
[./ sync recursive (?<!\bApp_Resources\b.*)(?<!\.\/\btests\b\/.*?)\.(xml|css|js|(?<!\.d\.)ts|(?<!\b_[\w-]*\.)scss)$] . sync (?<!\bApp_Resources\b.*)(?<!\.\/\btests\b\/.*?)\.(xml|css|js|(?<!\.d\.)ts|(?<!\b_[\w-]*\.)scss)$ 271 bytes {bundle} [built]
[./app-root.xml] 309 bytes {bundle} [optional] [built]
[./app.css] 1.84 KiB {bundle} [optional] [built]
[./main-page.ts] 1.36 KiB {bundle} [optional] [built]
[./main-page.xml] 1.87 KiB {bundle} [optional] [built]
[./main-view-model.ts] 1.04 KiB {bundle} [built]
[./main.ts] 2.19 KiB {bundle} [built]
+ 331 hidden modules
Webpack compilation complete. Watching for file changes.
Webpack build done!
Updating runtime package.json with configuration values...
Project successfully prepared (android)
Cannot use import statement outside a module
Seems the problem is with @nativescript/unit-test-runner after prepare hook, which has import keyword and it fails. Replacing it with require seems to fix the problem:
const path = require("path");
const fs = require("fs");
// import * as path from "path";
// import * as fs from "fs";
Thanks for this @PeterStaev we're shooting to wrap this up by Friday and will be running some tests with all latest changes.
Most helpful comment
Thanks for this @PeterStaev we're shooting to wrap this up by Friday and will be running some tests with all latest changes.