1

So, I'm testing a TypeScript file with Mocha, I access a dependency in the file via import, and the function to test is in the same file, like below:

import { Foo } from 'foo-library';

// I'm trying to test this
export const myHelperFunction = (a, b) => {
  return a + b;
  // Foo not used
};

export class BigClass {
  public doStuff() {
    // uses Foo
  }
}

Then I have my test file:

import { myHelperFunction } from './my-file';

it('does the right thing', () => {
    expect(myHelperFunction(2, 3)).to.equal(5);
});

Now, Mocha tries to parse foo-library contents while attempting to execute tests, and throws an error that says "unexpected token import", even though the import is not used in myHelperFunction. The file is in ES6 format and Mocha/Node is unable to parse it correctly.

Should the dependency files be transpiled to ES5 somehow? Is it possible to skip the imports altogether while testing? I have tried to mock the imports with several libraries (Sinon etc.) and that hasn't worked either.

I'd greatly appreciate any ideas.

mkkekkonen
  • 1,704
  • 2
  • 18
  • 35
  • It's likely that the error is in your test file, not in `Foo` or `myHelperFunction`. Node simply cannot use import. You need to either transpile everything to ES5, or use [babel loader](http://jamesknelson.com/testing-in-es6-with-mocha-and-babel-6/), or [enable experimental import support](https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node). – zeh Sep 26 '18 at 19:26
  • @zeh How would I do that? My test command now looks like this: `"test": "mocha --require babel-register src/**/test.ts"` ...but I still get the same error, i.e.: `/home/maija/ionic/mobiilikasvio2/node_modules/@ionic-native/sqlite/index.js:20 import { Injectable } from '@angular/core'; ^^^^^^ SyntaxError: Unexpected token import` – mkkekkonen Sep 26 '18 at 19:59

2 Answers2

2

To answer the original question, try mocha -r ts-node/register src/**/test.ts. Mocha won't run TS files right off the bat. You have to use ts-node/register to run them as TS files.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • You might have to install [ts-node](https://github.com/TypeStrong/ts-node) if you haven't already, that was the case for me. – mneumann Apr 09 '20 at 21:59
0

Problem solved, I switched to Jest.

Actually I had some problems with it as well. The issue was that JavaScript files in node_modules weren't transpiled. The working configuration is below.

package.json

{
  "name": "myApp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "test": "jest"
  },
  "dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "~4.12.0",
    "@ionic-native/splash-screen": "~4.12.0",
    "@ionic-native/sqlite": "^4.15.0",
    "@ionic-native/status-bar": "~4.12.0",
    "@ionic/storage": "2.2.0",
    "cordova-sqlite-storage": "^2.4.0",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "moment": "^2.22.2",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@ionic/app-scripts": "3.2.0",
    "@types/jest": "^23.3.2",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "regenerator-runtime": "^0.12.1",
    "ts-jest": "^23.10.2",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-sqlite-storage": {}
    }
  }
}

babel.config.js

'use strict';

module.exports = {
    presets: ['@babel/preset-env'],
}

jest.config.js

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.js?$": "babel-jest" // had to add this
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  "moduleDirectories": [
    "node_modules",
    "src"
  ],
  "transformIgnorePatterns": [
    "node_modules/(?!(@ionic-native)/)" // had to add this
  ]
}
mkkekkonen
  • 1,704
  • 2
  • 18
  • 35